WaitKey_or_Mouse()

BlitzMax Forums/BlitzMax Beginners Area/WaitKey_or_Mouse()

LeeFbx(Posted 2008) [#1]
I wanted a function that would combine Waitkey and Waitmouse.
After looking at the source code, I put this together and to my complete astonishment it seems to work.

Is this a good place to post this?
What about making it a regular part of BlitzMax?


' Function that waits for a key press or mouse click
' Mouse keys are returned as 255 + mouse key code

Function WaitKey_Or_Mouse()
Local n%
FlushKeys ; FlushMouse
Repeat
WaitSystem
For n=1 To 255
If KeyHit(n) Return n
Next
For n=1 To 3
If MouseHit(n) Return n + 255
Next
Forever
End Function


Czar Flavius(Posted 2008) [#2]
:)

If you don't want the know what key or mouse button has been pressed you can use,

While Not KeyHit() Or MouseHit(); Wend



Dreamora(Posted 2008) [#3]
While Not KeyHit() Or MouseHit(); delay(1); Wend


Thats better, the above is a pure overkill as the point of wait is to halt the execution


LeeFbx(Posted 2008) [#4]
The point is I DO care which key or button was pressed.
WaitKey and WaitMouse each issue their own "WaitSystem" and you can't satisfy both at once.

KeyHit() and MouseHit() don't seem to work without a specific key code between the parentheses.
This is the idea of what I want, but it doesn't work either:

While Not (kc = WaitKey())|(kc = WaitMouse()) ; Delay(1); Wend

WaitKey_Or_Mouse() gets around the two waitsystem bind.


GfK(Posted 2008) [#5]
Graphics 640,480

Repeat
	quit:Int = 0
	If MouseHit(1) Then quit = 1001
	If MouseHit(2) Then quit = 1002
        If Not quit
        	quit = GetChar()
        EndIf
	Delay 1	
Until quit <> 0

Print quit



LeeFbx(Posted 2008) [#6]
That does the job nicely too. Thanks GfK.


JazzieB(Posted 2008) [#7]
The only problem with using GetChar instead of looping through all the key codes with KeyHit/Down is that each return different codes.

KeyHit/Down returns BlitzMax's scan/key code, whereas GetChar returns the ASCII code of the key being pressed. Therefore GetChar can return two different values for the letter "a" depending on whether Caps Lock is on, or whether the Shift key is being held down at the time.

I have the option of redefining controls in my game, and as all keyboard checks are done using scan codes during the game, I have to scan all codes during this set-up to ensure that I'm checking the right keys in game. Of course, I could set-up some kind of look-up table to convert ASCII codes to scan codes, but then KeyHit/Down can check keys that I can't detect with GetChar.

It's a shame that there isn't a GetChar equivalent for scan codes, as the looping method does show some significant slow-down on my lower spec'd iBook (I have a demo of the game running in the background at all times).