Mouse and Keyboard Input |
Top Previous Next |
Getting the mouse position The current position of the mouse can be read with the MOUSEX() and MOUSEY() commands. Both commands return the position in screen coordinates. mx = MOUSEX()
When working with windowed mode Windows sends the current client positions of the mouse with the message @IDMOUSEMOVE and the positions are contained in the @MOUSEX and @MOUSEY variables. See the Emergence BASIC users guide Messages and message loops for more details. Reading mouse buttons The current up/down state of the mouse buttons can be determined with the MOUSEDOWN function. The MOUSEDOWN function returns TRUE if the specified button is down or FALSE if up. For the argument use 1 for the left button, 2 for the right and 3 for the middle mouse button: IF MOUSEDOWN(1)
Reading the keyboard The keyboard is read using DirectInput which is initialized whenever a screen is created or attached. DirectInput allows real time polling of the keyboard for quick response to user input by bypassing the Windows messaging system. To read one or more keys in raw format, meaning testing whether a specific key is currently down on the keyboard, use the KEYDOWN function. The KEYDOWN function returns TRUE if the key is currently being pressed or FALSE otherwise. KEYDOWN expects one input parameter, the DirectInput scan code of the key to test. Scan codes can be found in Appendix A. Example fragment: CONST DIK_UP = 0xC8 /* UpArrow on arrow keypad */ To retrieve the ASCII value of the currently pressed key use the GETKEY function. GETKEY has no parameters and returns the currently pressed key as a string. If no key is being pressed then an empty string is returned. GETKEY is equivalent to INKEY$ for console programs. Example fragment: b$ = "" To pause your program and wait for a key press use the WAITKEY statement. WAITKEY takes an optional argument that will pause until a specific key is pressed or by itself waits for any key to be pressed. The key to wait for is specified as a scan code. 'Pause and wait for any key Flushing the keyboard buffer Flushing the current keyboard buffer is accomplished using the FLUSHKEYS command. FLUSHKEYS is used when you want to ignore any keyboard input that might have occurred before a WAITKEY or GETKEY function. Keyboard input example: 'some convenient scancodes |