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()
my = MOUSEY()

 

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)
    '... Someone's pressing a mouse button
ENDIF

 

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 */
CONST DIK_DOWN = 0xD0 /* DownArrow on arrow keypad */
...
IF KEYDOWN(DIK_UP) AND py > 0
    py -= speed
ENDIF
IF KEYDOWN(DIK_DOWN) AND py < (height-10)
    py += speed
ENDIF

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$ = ""
a$ = GETKEY
IF a$ <> "" THEN b$ = a$
WRITETEXT 0,20,"You Pressed: " + 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
WAITKEY
'Pause and wait for the ESC key
WAITKEY 0x01

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
CONST DIK_UP = 0xC8 /* UpArrow on arrow keypad */
CONST DIK_LEFT = 0xCB /* LeftArrow on arrow keypad */
CONST DIK_RIGHT = 0xCD /* RightArrow on arrow keypad */
CONST DIK_DOWN = 0xD0 /* DownArrow on arrow keypad */
 
width = 640
height = 480
fps = 0
CREATESCREEN width,height,16
 
px = 0.0f
py = 0.0f
speed = 1
b$ = ""
DO
    FILLSCREEN RGB(0,255,0)
    WRITETEXT 0,0,"Press escape to close " + STR$(fps)
    DRAWFILLEDRECT px,py,10,10,RGB(255,0,0)
    IF KEYDOWN(DIK_UP) AND py > 0
        py -= speed
    ENDIF
    IF KEYDOWN(DIK_DOWN) AND py < (height-10)
        py += speed
    ENDIF
    IKEYDOWN(DIK_LEFT) AND px > 0
        px -= speed
    ENDIF
    IF KEYDOWN(DIK_RIGHT) AND px < (width-10)
        px += speed
    ENDIF
 
    a$ = GETKEY
    IF a$ <> "" THEN b$ = a$
    WRITETEXT 0,20,"You Pressed: " + b$
    fps = FLIP 1
UNTIL KEYDOWN(0x01)
CLOSESCREEN
END