QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The _DEVICEINPUT function returns the device number when a controller device button, wheel or axis event occurs.

Syntax

device% = _DEVICEINPUT device_active% = _DEVICEINPUT(device_number%)

Parameter(s)

Description

Example(s)

Checking device controller interfaces and finding out what devices are being used.


FOR i = 1 TO _DEVICES
  PRINT STR$(i) + ") " + _DEVICE$(i)
  PRINT "Button:"; _LASTBUTTON(i); ",Axis:"; _LASTAXIS(i); ",Wheel:"; _LASTWHEEL(i)
NEXT

PRINT
DO
  x = _DEVICEINPUT
  IF x THEN PRINT "Device ="; x;
LOOP UNTIL INKEY$ = CHR$(27) 'escape key exit

END 


[KEYBOARD][BUTTON]
Buttons: 512 Axis: 0 Wheels: 0
[MOUSE][BUTTON][AXIS][WHEEL]
Buttons: 3 Axis: 2 Wheels: 3
[CONTROLLER][NAME][Microsoft Sidewinder Precision Pro (USB)](NAME][Microsoft Sidewinder Precision Pro (USB))[BUTTON][AXIS]
Buttons: 9 Axis: 6 Wheels: 0

Device = 2 Device = 2

Note: Mouse events must be within the program screen area. Keyboard presses are registered only when program is in focus.

Why does a mouse have 3 wheels? Relative x and y movements can be read using the first 2 _WHEEL reads.


ignore = _MOUSEMOVEMENTX 'dummy call to put mouse into relative movement mode

PRINT "Move your mouse and/or your mouse wheel (ESC to exit)"

d = _DEVICES '  always read number of devices to enable device input
DO: _LIMIT 30  'main loop
  DO WHILE _DEVICEINPUT(2) 'loop only runs during a device 2 mouse event
        PRINT _WHEEL(1), _WHEEL(2), _WHEEL(3)
  LOOP 
LOOP UNTIL INKEY$ = CHR$(27) 

Explanation: Referencing the _MOUSEMOVEMENTX function hides the mouse and sets the mouse to a relative movement mode which can be read by _WHEEL. _DEVICEINPUT(2) returns -1 (true) only when the mouse is moved, scrolled or clicked.

Using ON…GOSUB with the _DEVICEINPUT number to add keyboard, mouse and game controller event procedures.


n = _DEVICES 'required when reading devices
PRINT "Number of devices found ="; n
FOR i = 1 TO n
    PRINT i; _DEVICE$(i) ' 1 = keyboard, 2 = mouse, 3 = other controller, etc.
NEXT
PRINT

DO: device = _DEVICEINPUT
    ON device GOSUB keyboard, mouse, controller  'must be inside program loop
LOOP UNTIL INKEY$ = CHR$(27)
END

keyboard:
PRINT device; "Keyboard";
RETURN

mouse:
PRINT device; "Mouse ";
RETURN

controller:
PRINT device; "Game control ";
RETURN 

Note: ON…GOSUB and ON…GOTO events require numerical values to match the order of line labels listed in the event used inside loops.

See Also