Checking for 'Any Key'
BlitzMax Forums/BlitzMax Beginners Area/Checking for 'Any Key'
Yes its that damn 'any' key again :) Well - I need to check for a keypress of any key on the keyboard in a main loop - but I can't use waitkey() because it would halt the program whilst waiting for input. Is there a command that checks for any kepress or would I have to write my own?
I suppose if I had to write my own - I would have to do an individual check for every key on the keyboard - Is this correct?
Thank you.
GetChar()?
This is how I do it in my framework:
' -----------------------------------------------------------------------------
' ccAnyKeyPressed: Tests if any key has been hit
' -----------------------------------------------------------------------------
Function ccAnyKeyPressed%()
For Local i% = 0 To 255
If KeyHit(i) Then Return 1
Next
Return 0
End Function
Isn't that about 3x longer than it needs to be, given that a keyboard only has 90-odd keys? (and roughly the same number of key constants that you can use with KeyHit() anyway)
GetChar() will do it for you, viz:
...
If GetChar()
'a key was pressed
Endif
...
Seems like you're doing too much work there, GA. :)
Will GetChar() get just the ascii keys, or escape, f1-f12, etc.. ?
Is it possible to set up an event hook which looks at some piece of data from the event object to determine that it relates to the keyboard being pressed (in general) and not a specific key?
Is it possible to set up an event hook which looks at some piece of data from the event object to determine that it relates to the keyboard being pressed (in general) and not a specific key?
This code is adapted from BRL.PolledInput, i take no credit:
Graphics 640,480,0
EnableAnyKey( "you", anykey)
Function anykey()
Notify "any-key pressed"
EndFunction
Repeat
DrawText "Pointer", MouseX(),MouseY()
Flip
Cls
Until KeyHit(KEY_ESCAPE) Or AppTerminate()
End
Private
Global Enabled:Int
Global InputSource:Object
Global AnyCallback()
Public
Function EnableAnyKey( source:Object, callback())
If Enabled Then Return
AnySource = source
AnyCallback = callback
FlushKeys()
AddHook EmitEventHook,AnyKeyHook, Null, 0
Enabled = True
EndFunction
Function DisableAnyKey()
If Not Enabled Then Return
RemoveHook EmitEventHook, AnyKeyHook
FlushKeys()
InputSource = Null
Enabled = False
AnyCallback = Null
EndFunction
Function AnyKeyHook:Object( id:Int, data:Object, context:Object)
Local ev:TEvent=TEvent(data)
If Not ev Return data
If InputSource And InputSource <> ev.source Then Return data
Select ev.id
Case EVENT_KEYUP, EVENT_KEYCHAR
If AnyCallback Then AnyCallback()
EndSelect
Return data
EndFunction
grable - that's very good - works very nicely. You should take some credit for thinking to adapt it in the first place :) (+Human too)
Thanks to everyone else too. GetChar() does work but not for the function keys or escape - but its a good comprimise.
Grey Alien's code is good because it does check for every key on the keyboard. Thank you - but stop abducting people - we need our sleep.
Just checked the blitzmax sourcecode. Grey Alien's example isn't as big a waste of resources as I thought it was since the returned key codes can range between 8 and 226. But there's lots of gaps; i.e. no keycode 223, 224 or 225. Plus loads of others missing, and a bunch of 30 or so are commented out.
You could make an array of the useful codes and iterate through that, but a 0-255 loop is going to be very fast anyway.
just say
'any key' not found please upgrade your keyboard
I figured out a different way, which works great, while it's not cross platform (I'm afraid). You can however check for event IDs on the others. This one is for Windows... (XP 64 to be percise)
Local Event:TEvent = CurrentEvent
event.id = PollEvent()
'If event.id <> 0 Then Print event.id 'check id for different os
If event.id=513 Then Print "anykey was hit!"
By the way, for windows XP the event.ids go:
keyhit = 513
keydown = 515-516 (alternating, why ever?!)
keyup = 514
@Taron: Alternative (using pretty much the same method),
here.
It should be cross-platform.. maybe the 'EVENT_KEY---' constants change from OS to OS.
By the way, is there anyone else, who can report that (MODIFIER_...) is not responding? I can only get (KEY_LSHIFT) or the likes, but never any Modifier...
Ehehehe, Plash, looking at that thread makes me even prouder! Guess I've found the simplest way. ;)
MERRY CHRISTMAS ...and happy holidays... by the way! 8)
Why can we get the backspace key using GetChar but not the delete key? It would be realllly useful if I could get all text-input keys with the key-repeat delay built-in..