DEVICEINPUT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(20 intermediate revisions by the same user not shown)
Line 8: Line 8:




{{Parameters}}
{{PageParameters}}
* Use the _DEVICEINPUT  {{Parameter|device%}} [[INTEGER]] returned to find the number of the controller device being used.
* Use the {{Parameter|device%}} [[INTEGER]] returned to find the number of the controller device being used.
* A literal specific {{Parameter|device_number%}} parameter can be used to return -1 if active or 0 if inactive. {{text|EX: '''WHILE _DEVICEINPUT(2)'''|green}}
* A literal specific {{Parameter|device_number%}} parameter can be used to return -1 if active or 0 if inactive, e.g. {{InlineCode}}{{Cl|WHILE}} {{Cl|_DEVICEINPUT}}({{Text|2|#F580B1}}){{InlineCodeEnd}}.




Line 18: Line 18:
* When a device button is pressed or a scroll wheel or axis is moved, the device number will be returned.
* When a device button is pressed or a scroll wheel or axis is moved, the device number will be returned.
* Devices are numbered as 1 for keyboard and 2 for mouse. Other controller devices will be numbered 3 or higher if installed.
* Devices are numbered as 1 for keyboard and 2 for mouse. Other controller devices will be numbered 3 or higher if installed.
* [[_LASTBUTTON]], [[_LASTAXIS]], or [[_LASTWHEEL]] will indicate the number of functions available with the specified ''device'' number.
* [[_LASTBUTTON]], [[_LASTAXIS]], or [[_LASTWHEEL]] will indicate the number of functions available with the specified {{Parameter|device%}} number.
* User input events can be monitored reading valid numbered [[_AXIS]], [[_BUTTON]], [[_BUTTONCHANGE]] or [[_WHEEL]] functions.
* User input events can be monitored reading valid numbered [[_AXIS]], [[_BUTTON]], [[_BUTTONCHANGE]] or [[_WHEEL]] functions.
* ''Note:'' [[ON...GOSUB|ON _DEVICEINPUT GOSUB]] keyboard, mouse, gamecontrol can be used to control the devices 1,2 and 3, etc.
* {{InlineCode}}{{Cl|ON...GOSUB|ON _DEVICEINPUT GOSUB}} keyboard, mouse, gamecontrol{{InlineCodeEnd}} could be used to easily branch to device specific handler routines (see Example 3 below).




{{PageExamples}}
{{PageExamples}}
''Example 1:'' Checking device controller interfaces and finding out what devices are being used.
; Example 1: Checking device controller interfaces and finding out what devices are being used.
{{CodeStart}}
{{CodeStart}}
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} {{Cl|_DEVICES}}
{{Cl|FOR}} i% = {{Text|1|#F580B1}} {{Cl|TO}} {{Cl|_DEVICES}}
  {{Cl|PRINT}} {{Cl|STR$}}(i) + ") " + {{Cl|_DEVICE$}}(i)
    {{Cl|PRINT}} {{Cl|STR$}}(i%) + {{Text|<nowiki>") "</nowiki>|#FFB100}} + {{Cl|_DEVICE$}}(i%)
  {{Cl|PRINT}} "Button:"; {{Cl|_LASTBUTTON}}(i); ",Axis:"; {{Cl|_LASTAXIS}}(i); ",Wheel:"; {{Cl|_LASTWHEEL}}(i)
    {{Cl|PRINT}} {{Text|<nowiki>"Button:"</nowiki>|#FFB100}}; {{Cl|_LASTBUTTON}}(i%); {{Text|<nowiki>",Axis:"</nowiki>|#FFB100}}; {{Cl|_LASTAXIS}}(i%); {{Text|<nowiki>",Wheel:"</nowiki>|#FFB100}}; {{Cl|_LASTWHEEL}}(i%)
{{Cl|NEXT}}
{{Cl|NEXT}} i%


{{Cl|PRINT}}
{{Cl|PRINT}}
DO
{{Cl|DO}}
  x = {{Cl|_DEVICEINPUT}}
    x% = {{Cl|_DEVICEINPUT}}
  {{Cl|IF...THEN|IF}} x {{Cl|THEN}} {{Cl|PRINT}} "Device ="; x;
    {{Cl|IF}} x% {{Cl|THEN}} {{Cl|PRINT}} {{Text|<nowiki>"Device ="</nowiki>|#FFB100}}; x%;
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) 'escape key exit
{{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}({{Text|27|#F580B1}})


{{Cl|END}}
{{Cl|END}}
Line 48: Line 48:
Device = 2 Device = 2
Device = 2 Device = 2
{{OutputEnd}}
{{OutputEnd}}
: ''Note:'' Mouse events must be within the program screen area. Keyboard presses are registered only when program is in focus.
{{PreStart}}
'''Note'''
Mouse events must be within the program screen area. Keyboard presses
are registered only when program is in focus.
{{PreEnd}}


----


''Example 2:'' Why does a mouse have 3 wheels? Relative x and y movements can be read using the first 2 [[_WHEEL]] reads.
; Example 2: Why does a mouse have 3 wheels? Relative x and y movements can be read using the first 2 [[_WHEEL]] reads.
{{CodeStart}}
{{CodeStart}}
ignore = {{Cl|_MOUSEMOVEMENTX}} 'dummy call to put mouse into relative movement mode
ignore% = {{Cl|_MOUSEMOVEMENTX}} {{Text|<nowiki>'dummy call to put mouse into relative movement mode</nowiki>|#919191}}


{{Cl|PRINT}} "Move your mouse and/or your mouse wheel (ESC to exit)"
{{Cl|PRINT}} {{Text|<nowiki>"Move your mouse and/or your mouse wheel (ESC to exit)"</nowiki>|#FFB100}}


d = {{Cl|_DEVICES}} ' always read number of devices to enable device input
d% = {{Cl|_DEVICES}} {{Text|<nowiki>'always read number of devices to enable device input</nowiki>|#919191}}
DO: {{Cl|_LIMIT}} 30 'main loop
{{Cl|DO}}
  {{Cl|DO...LOOP|DO}} {{Cl|WHILE}} {{Cl|_DEVICEINPUT}}(2) 'loop only runs during a device 2 mouse event
    {{Cl|_LIMIT}} {{Text|30|#F580B1}} {{Text|<nowiki>'main loop</nowiki>|#919191}}
         {{Cl|PRINT}} {{Cl|_WHEEL}}(1), {{Cl|_WHEEL}}(2), {{Cl|_WHEEL}}(3)
    {{Cl|DO...LOOP|DO WHILE}} {{Cl|_DEVICEINPUT}}({{Text|2|#F580B1}}) {{Text|<nowiki>'loop only runs during a device 2 mouse event</nowiki>|#919191}}
  {{Cl|LOOP}}
         {{Cl|PRINT}} {{Cl|_WHEEL}}({{Text|1|#F580B1}}), {{Cl|_WHEEL}}({{Text|2|#F580B1}}), {{Cl|_WHEEL}}({{Text|3|#F580B1}})
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
    {{Cl|LOOP}}
{{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}({{Text|27|#F580B1}})
 
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
: ''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.
{{PreStart}}
'''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 ''true(-1)'' only when the mouse is moved,
scrolled or clicked.
{{PreEnd}}


----


''Example 3:'' Using [[ON...GOSUB]] with the [[_DEVICEINPUT]] number to add keyboard, mouse and game controller event procedures.
; Example 3: Using [[ON...GOSUB]] with the [[_DEVICEINPUT]] number to add keyboard, mouse and game controller event procedures.
{{CodeStart}}
{{CodeStart}}
n = {{Cl|_DEVICES}} 'required when reading devices
n% = {{Cl|_DEVICES}} {{Text|<nowiki>'required when reading devices</nowiki>|#919191}}
{{Cl|PRINT}} "Number of devices found ="; n
{{Cl|PRINT}} {{Text|<nowiki>"Number of devices found ="</nowiki>|#FFB100}}; n%
{{Cl|FOR...NEXT|FOR}} i = 1 TO n
{{Cl|FOR}} i% = {{Text|1|#F580B1}} {{Cl|TO}} n%
     PRINT i; _DEVICE$(i) ' 1 = keyboard, 2 = mouse, 3 = other controller, etc.
     {{Cl|PRINT}} i%; {{Cl|_DEVICE$}}(i%) {{Text|<nowiki>'1 = keyboard, 2 = mouse, 3 = other controller, etc.</nowiki>|#919191}}
{{Cl|NEXT}}
{{Cl|NEXT}} i%
 
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|DO}}
    device% = {{Cl|_DEVICEINPUT}}
    {{Cl|ON}} device% {{Cl|GOSUB}} keyboard, mouse, controller {{Text|<nowiki>'must be inside program loop</nowiki>|#919191}}
{{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}({{Text|27|#F580B1}})


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


keyboard:
keyboard:
{{Cl|PRINT}} device; "Keyboard";
{{Cl|PRINT}} device%; {{Text|<nowiki>"Keyboard"</nowiki>|#FFB100}};
{{Cl|RETURN}}
{{Cl|RETURN}}


mouse:
mouse:
{{Cl|PRINT}} device; "Mouse ";
{{Cl|PRINT}} device%; {{Text|<nowiki>"Mouse "</nowiki>|#FFB100}};
{{Cl|RETURN}}
{{Cl|RETURN}}


controller:
controller:
{{Cl|PRINT}} device; "Game control ";
{{Cl|PRINT}} device%; {{Text|<nowiki>"Game control "</nowiki>|#FFB100}};
{{Cl|RETURN}}
{{Cl|RETURN}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
: ''Note:'' [[ON...GOSUB]] and [[ON...GOTO]] events require numerical values to match the order of line labels listed in the event used inside loops.
{{PreStart}}
'''Note'''
[[ON...GOSUB]] and [[ON...GOTO]] events require numerical values to match
the order of line labels listed in the event used inside loops.
{{PreEnd}}




Line 102: Line 123:
* [[_BUTTON]], [[_AXIS]], [[_WHEEL]]
* [[_BUTTON]], [[_AXIS]], [[_WHEEL]]
* [[STRIG]], [[STICK]]
* [[STRIG]], [[STICK]]
* [[ON...GOSUB]] {{text|(numerical events)}}
* [[ON...GOSUB]]
* [[Controller Devices]]
* [[Controller Devices]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:33, 18 November 2024

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%)


Parameters

  • Use the device% INTEGER returned to find the number of the controller device being used.
  • A literal specific device_number% parameter can be used to return -1 if active or 0 if inactive, e.g. WHILE _DEVICEINPUT(2).


Description

  • Use _DEVICES to find the number of controller devices available BEFORE using this function.
  • _DEVICE$ can be used to list the device names and control types using valid _DEVICES numbers.
  • When a device button is pressed or a scroll wheel or axis is moved, the device number will be returned.
  • Devices are numbered as 1 for keyboard and 2 for mouse. Other controller devices will be numbered 3 or higher if installed.
  • _LASTBUTTON, _LASTAXIS, or _LASTWHEEL will indicate the number of functions available with the specified device% number.
  • User input events can be monitored reading valid numbered _AXIS, _BUTTON, _BUTTONCHANGE or _WHEEL functions.
  • ON _DEVICEINPUT GOSUB keyboard, mouse, gamecontrol could be used to easily branch to device specific handler routines (see Example 3 below).


Examples

Example 1
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 i%

PRINT
DO
    x% = _DEVICEINPUT
    IF x% THEN PRINT "Device ="; x%;
LOOP UNTIL INKEY$ = CHR$(27)

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)]][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.

Example 2
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)

END
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 true(-1) only when the mouse is moved,
 scrolled or clicked.

Example 3
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 i%

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
Code by Ted Weissgerber
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



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link