Any way to check if any key was pressed?

BlitzMax Forums/BlitzMax Programming/Any way to check if any key was pressed?

orgos(Posted 2008) [#1]
There are some way to check if any key, "a", F1", "Ctrl" or "Alt" etc.. keys are pressed and know the KEYs VALUEs of it?


plash(Posted 2008) [#2]
If KeyHit(KEY_A) Then 'Do something!
If KeyHit(KEY_F1) Then 'Do something!
If KeyHit(MODIFIER_CONTROL) Then 'Do something!
If KeyHit(MODIFIER_ALT) Then 'Do something!


etc..

If you go to the documentation for KeyDown() or KeyHit() there should be a link to 'key codes' (a list of valid key codes).


Perturbatio(Posted 2008) [#3]
Graphics 640, 480, 0 ,0

While True
	For key:Int = 1 To 183
		If KeyDown(key) Then
			Print key
			End
		EndIf
	Next
Wend




orgos(Posted 2008) [#4]
Yes I know that.

But I want to check on my main loop if anykey are pressed to execute some function asociated with some behavior of my sprite and pass the value of keys to the function.


plash(Posted 2008) [#5]
Checking every key state every time through a loop is very inefficient.


Perturbatio(Posted 2008) [#6]
But I want to check on my main loop if anykey are pressed to execute some function asociated with some behavior of my sprite and pass the value of keys to the function.


So you want to check for specific keys, not any key?


orgos(Posted 2008) [#7]
In the main loop want to check for any key then pass the getted key_coded to each sprite behavior and inside of behavior scheck what is the key to make the action.


plash(Posted 2008) [#8]
In the main loop want to check for any key then pass the getted key_coded to each sprite behavior
So you want to check for any key to be pressed then send it off to a function, fair enough. What does that function do?


Perturbatio(Posted 2008) [#9]
Graphics 640, 480, 0, 0

AddHook EmitEventHook,KeyHook,Null,0

Function KeyHook:Object( id,data:Object,context:Object )

	Local event:TEvent=TEvent(data)
	Select event.id
		Case EVENT_KEYDOWN
			Print event.data
	End Select
	Return data

End Function


While Not KeyDown(KEY_ESCAPE)
	Cls
	Flip
Wend
End


Swap the Print event.data for a Select..Case with the keycodes you want to check.


orgos(Posted 2008) [#10]
Thanks Perturbatio for that, work great.