Difference between revisions of "Mouse and Keyboard Input"

From QB64.org wiki
Jump to: navigation, search
(Created page with 'This tutorial will show how to use some of the statements and functions that deal with mouse and keyboard input. '''Mouse Input:''' In order to catch all the mouse messages we…')
(No difference)

Revision as of 03:33, 13 March 2011

This tutorial will show how to use some of the statements and functions that deal with mouse and keyboard input.


Mouse Input:

In order to catch all the mouse messages we use a loop that is only run if the mouse has been changed. To know if the mouse has been changed we use _MOUSEINPUT.


So this would be the loop:

DO WHILE _MOUSEINPUT LOOP


It basically means "Do what is inside the loop while there is mouseinput".


Right now there isn't anything inside the loop so we add the functions that read the mouse input:

mouseWheel = 0 DO WHILE _MOUSEINPUT mouseX = _MOUSEX mouseY = _MOUSEY mouseLeftButton = _MOUSEBUTTON (1) mouseRightButton = _MOUSEBUTTON (2) mouseMiddleButton = _MOUSEBUTTON (3) mouseWheel = mouseWheel + _MOUSEWHEEL LOOP


We have to set mouseWheel to 0 in order to reset it every loop (since we add the number of mousewheel clicks to the variable, if it was 0 mouseWheel would still be the number of clicks since last time even though there hasn't been any more clicks).


Let's create a program to display the variables in realtime:

DO CLS 'Clear the screen each loop. mouseWheel = 0 DO WHILE _MOUSEINPUT mouseX = _MOUSEX mouseY = _MOUSEY mouseLeftButton = _MOUSEBUTTON (1) mouseRightButton = _MOUSEBUTTON (2) mouseMiddleButton = _MOUSEBUTTON (3) mouseWheel = mouseWheel + _MOUSEWHEEL LOOP PRINT "mouseX:"; mouseX PRINT "mouseY:"; mouseY PRINT "mouseLeftButton:"; mouseLeftButton PRINT "mouseRightButton:"; mouseRightButton PRINT "mouseMiddleButton:"; mouseMiddleButton PRINT "mouseWheel:"; mouseWheel PRINT PRINT "testvariable for mouseWheel:"; testvariable testvariable = testvariable + mouseWheel _DISPLAY 'display what has been written to the screen each loop. LOOP


We made a testvariable to better see the results of mouseWheel (it often changes to quickly to be seen). As you can see it corresponds perfectly to the number of mouseWheel clicks.


You might have noticed that mouseX and mouseY has fractions of a column/row. This might make it difficult to test the variables against known positions on the screen (which are integer positions). Use CINT to convert mouseX and mouseY to the integer row/column.


Added CINT to convert mouseX and mouseY values to integer, this can of course be made directly in the mouseinput loop:

DO CLS 'Clear the screen each loop. mouseWheel = 0 DO WHILE _MOUSEINPUT mouseX = CINT(_MOUSEX) mouseY = CINT(_MOUSEY) mouseLeftButton = _MOUSEBUTTON (1) mouseRightButton = _MOUSEBUTTON (2) mouseMiddleButton = _MOUSEBUTTON (3) mouseWheel = mouseWheel + _MOUSEWHEEL LOOP PRINT "mouseX:"; mouseX PRINT "mouseY:"; mouseY PRINT "mouseLeftButton:"; mouseLeftButton PRINT "mouseRightButton:"; mouseRightButton PRINT "mouseMiddleButton:"; mouseMiddleButton PRINT "mouseWheel:"; mouseWheel PRINT PRINT "testvariable for mouseWheel:"; testvariable testvariable = testvariable + mouseWheel _DISPLAY 'display what has been written to the screen each loop. LOOP


The values of _MOUSEX and _MOUSEY will be those that are appropriate in the screenmode you are in. In the previous examples we have been in SCREEN 0. Let's see the coordinates in graphic screenmodes (CINT isn't needed now since mouseX and mouseY will be in pixels which are the sensitivity of the mouse - it won't return fractions of a pixel coordinate):

SCREEN 13 DO CLS 'Clear the screen each loop. mouseWheel = 0 DO WHILE _MOUSEINPUT mouseX = _MOUSEX mouseY = _MOUSEY mouseLeftButton = _MOUSEBUTTON (1) mouseRightButton = _MOUSEBUTTON (2) mouseMiddleButton = _MOUSEBUTTON (3) mouseWheel = mouseWheel + _MOUSEWHEEL LOOP PRINT "mouseX:"; mouseX PRINT "mouseY:"; mouseY PRINT "mouseLeftButton:"; mouseLeftButton PRINT "mouseRightButton:"; mouseRightButton PRINT "mouseMiddleButton:"; mouseMiddleButton PRINT "mouseWheel:"; mouseWheel PRINT PRINT "testvariable for mouseWheel:"; testvariable testvariable = testvariable + mouseWheel _DISPLAY 'display what has been written to the screen each loop. LOOP



Keyboard Input:

For simple input you can use INKEY$ but if you want to detect if several keys are pressed at once then INKEY$ will be insufficient.


Consider this example:

DO CLS _LIMIT 30 'limits the rate of the loop to 30 frames per second. kbd$ = INKEY$ PRINT kbd$ _DISPLAY LOOP UNTIL kbd$ = CHR$(27) 'CHR$(27) is the ESC key.


You can see that it only detects one key at a time, furthermore it only detects it at a certain rate, it doesn't detect that it is pressed every loop (which it obviously is as long as you hold it down).


So in order to detect if a key (or several keys) are being pressed each loop, you can use the _KEYDOWN statement as illustrated in this example:


DO CLS _LIMIT 30 PRINT "UP:"; _KEYDOWN(CVI(CHR$(0) + "H")) 'chr$(0) + "H" is the up key PRINT "DOWN:"; _KEYDOWN(CVI(CHR$(0) + "P")) 'chr$(0) + "P" is the down key PRINT "LEFT:"; _KEYDOWN(CVI(CHR$(0) + "K")) 'chr$(0) + "K" is the left key PRINT "RIGHT:"; _KEYDOWN(CVI(CHR$(0) + "M")) 'chr$(0) + "M" is the right key _DISPLAY LOOP UNTIL _KEYDOWN(27) '27 is the ESC key


You may have noticed that _KEYDOWN can only take numerical values, not strings so if you want to test a alphabetical key you can use ASC to get the ASCII value. 2-byte codes has to be converted to a integer using CVI (as seen in the example).

There are also other codes that are QB64 specific, see the _KEYDOWN page for a list of those codes. You can also test what code a key has by using _KEYHIT as illustrated in the example below:

DO CLS _LIMIT 30 keycode = _KEYHIT IF keycode <> 0 THEN displaykeycode = keycode PRINT displaykeycode _DISPLAY LOOP

Since I don't want to display the keycode when no keys are pressed (it would almost render it impossible to read) I use two variables, one that constantly reads the _KEYHIT function and one that is only set when _KEYHIT is something else than 0. I use a variable to test that _KEYHIT isn't 0 to be sure that the _KEYHIT state hasn't been changed since the test.


You can see that when pressed the value is positive and when released the value is negative. Use the positive value for _KEYDOWN tests.




Navigation:

QB64 Tutorials

Go to Keyword Reference - Alphabetical

Go to Keyword Reference - By usage