12-21-2023, 03:54 AM
(This post was last modified: 12-21-2023, 03:59 AM by TerryRitchie.)
The following code from lesson 21 of the tutorial works on my system to detect interaction between the mouse, keyboard, and a dpad controller I have connected. Look at line 22 of the code. You need to get the latest _DEVICEINPUT information in the same way you need to get _MOUSEINPUT, using a loop.
Code: (Select All)
DIM DeviceCount AS INTEGER ' number of controllers (simply used to activate controller statements)
DIM DeviceInput AS INTEGER ' indicates a controller has been interacted with
PRINT
PRINT " Interact with an input controller and it will highlight."
PRINT
PRINT " Press ESC to exit program."
PRINT
DeviceCount = _DEVICES ' activate controller statements
DO ' begin controller interaction loop
_LIMIT 10 ' slow things down a bit for viewing
LOCATE 6, 1 ' position cursor
COLOR 7, 0 ' gray on black text
PRINT " Keyboard" ' list controllers
PRINT " Mouse"
PRINT " Controller 1"
PRINT " Controller 2"
PRINT " Controller 3"
PRINT " Controller 4"
DeviceInput = _DEVICEINPUT ' get any controller interaction
IF DeviceInput THEN ' was a controller interacted with?
WHILE _DEVICEINPUT(DeviceInput): WEND ' yes, get the latest controller information
COLOR 14, 1 ' yellow on blue text
IF DeviceInput = 1 THEN ' was the keyboard interacted with?
LOCATE 6, 1 ' yes, position cursor
PRINT " Keyboard" ' highlight controller
ELSEIF DeviceInput = 2 THEN ' no, was the mouse interacted with?
LOCATE 7, 1 ' yes, position cursor
PRINT " Mouse" ' highlight controller
ELSE ' no, a joystick/game pad interacted with
LOCATE 5 + DeviceInput, 1 ' position cursor
PRINT " Controller "; _TRIM$(STR$(DeviceInput - 2)) ' highlight controller
END IF
END IF
LOOP UNTIL _KEYDOWN(27) ' leave when ESC key pressed
SYSTEM ' return to OS