WaitKey_or_Mouse()
BlitzMax Forums/BlitzMax Beginners Area/WaitKey_or_Mouse()
| ||
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 |
| ||
:) If you don't want the know what key or mouse button has been pressed you can use, While Not KeyHit() Or MouseHit(); Wend |
| ||
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 |
| ||
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. |
| ||
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 |
| ||
That does the job nicely too. Thanks GfK. |
| ||
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). |