GetKey?

BlitzMax Forums/BlitzMax Beginners Area/GetKey?

MRaven(Posted 2004) [#1]
I saw that the GetKey() comand is no more... how can I check what keyvalue has been pressed?

So far I also used the GetKey() comand to wait until the user pressed any key. I know there is the WaitKey() comand, but I usually want to exit a loop.

Repeat

Until GetKey() <> 0

How can I do this in BMax??


Barnabius(Posted 2004) [#2]
This is from the html manual:


KeyHit( keycode )   Check for key hit

Returns: Number of times key has been hit.

KeyHit also resets the count for the key specified to zero.
See the KeyCodes docs for a list of valid keycodes.

Example:

' keyhit.bmx

' the following code draws a circle every time the
' program detects the spacebar has been pressed
' and exits when it detects the ESCAPE key has
' been pressed

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)
	Cls
	If KeyHit(KEY_SPACE) DrawOval 0,0,640,480
	Flip
Wend


Barney


MRaven(Posted 2004) [#3]
Well, yes.. but this does not explain, how I can get the value of a unknown key that has been pressed by the user, it returns the number of times a KNOWN key has been hit.


BlitzSupport(Posted 2004) [#4]
There's no equivalent right now. Will have to be a feature request, I guess!


ImaginaryHuman(Posted 2004) [#5]
I was trying to think how to do this also but I don't think there's a token as yet.

However, you can use the preset identifiers to check for a specific key ie

If KeyHit(KEY_ESCAPE) Then End

Sooooooo... obviously KEY_ESCAPE has some kind of mask bits or numerical value that gets compared to the key code of the key that's been hit... SO I wonder.... Is there a mask you can submit like $FFFF or something, that will return true if ANY key has been hit?

That'd be at least half of the problem solved (if it even works) ... but I don't know how you could just get a raw key code and play with it.


Robert(Posted 2004) [#6]
You could just test KeyHit for keycode=0 to keycode=255.

Looking at the system.mod source, it seems that every time a key is hit, Blitz increments one of the slots in a 255-byte array. KeyHit just retrieves key_hit[keycode], so a check for all 255 keys would be very fast.

Edit: Looking at the MacOS .m code for system, it seems that a fixed lookup table to convert from virtual key codes to ASCII values - isn't this going to cause a problem for non NZ-layout keyboards?


Warren(Posted 2004) [#7]
There's no equivalent right now. Will have to be a feature request, I guess!

Please put it HIGH on the list. :) This is sort of important for people typing in their names for high score lists and things like that...


fredborg(Posted 2004) [#8]
This is almost similar to the B3D/B+ variant. With a bit of extra typing you can get it to return Ascii values instead of scancodes...
Function GetKey()
	Repeat
		For Local n=1 To 255
			If KeyHit(n) Return n
		Next
	Forever
End Function
Or perhaps it's better to add it to the system.xxx.bmx mods?
Something along these lines:
Method GetKey( )
	For Local keycode = 1 to 255
     		Local n=bbSystemKeyHit[keycode]
		if n return n
	Next
End Method
Don't know if it'll work...