Checking for 'Any Key'

BlitzMax Forums/BlitzMax Beginners Area/Checking for 'Any Key'

Rico(Posted 2008) [#1]
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.


GfK(Posted 2008) [#2]
GetChar()?


Grey Alien(Posted 2008) [#3]
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



GfK(Posted 2008) [#4]
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)


Warpy(Posted 2008) [#5]
GetChar() will do it for you, viz:

...
 
  If GetChar()
     'a key was pressed
  Endif

...



big10p(Posted 2008) [#6]
Seems like you're doing too much work there, GA. :)


plash(Posted 2008) [#7]
Will GetChar() get just the ascii keys, or escape, f1-f12, etc.. ?


ImaginaryHuman(Posted 2008) [#8]
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?


grable(Posted 2008) [#9]
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:



Rico(Posted 2008) [#10]
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.


GfK(Posted 2008) [#11]
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.


Grey Alien(Posted 2008) [#12]
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.


Ginger Tea(Posted 2008) [#13]
just say
'any key' not found please upgrade your keyboard



Taron(Posted 2008) [#14]
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)


By the way, for windows XP the event.ids go:
keyhit = 513
keydown = 515-516 (alternating, why ever?!)
keyup = 514


plash(Posted 2008) [#15]
@Taron: Alternative (using pretty much the same method), here.

It should be cross-platform.. maybe the 'EVENT_KEY---' constants change from OS to OS.


Taron(Posted 2008) [#16]
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)


plash(Posted 2009) [#17]
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..