Any way to check if any key was pressed?
BlitzMax Forums/BlitzMax Programming/Any way to check if any key was pressed?
| ||
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? |
| ||
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). |
| ||
Graphics 640, 480, 0 ,0 While True For key:Int = 1 To 183 If KeyDown(key) Then Print key End EndIf Next Wend |
| ||
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. |
| ||
Checking every key state every time through a loop is very inefficient. |
| ||
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? |
| ||
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. |
| ||
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? |
| ||
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. |
| ||
Thanks Perturbatio for that, work great. |