Hi All,
I put a good deal of work into a simple, consolidated game input API that allows for simple detection and testing of input from keyboard, mouse or game controller devices and thought I would share as there have been recent discussions on this topic.
With this approach device input can be detected with the following:
GXDeviceInputDetect diMoveLeft
The program will wait until a keyboard key, mouse button, or game controller button or axis movement has been detected. Then later in your code when you want to test for that device input, simply do this:
If GXDeviceInputTest
(diMoveLeft
) Then ' Do your move left logic here
I've added specific logic for linux and maxos to handle the fact that keyboard button presses are not returned by the _DEVICEINPUT method on those environments so that the behavior can be consistent cross-platform. This is actually related to my very first post on the forum:
https://qb64forum.alephc.xyz/index.php?topic=4448.msg138691#msg138691.
I've included a few demonstration programs in the attached zip.
1) The detect.bas program will display the last detected input and print information about the device input:
'$Include:'gx/gx.bi'
Print "Waiting for input..." GXDeviceInputDetect di
Print "Device Name: " + GXDeviceName
(di.deviceId
) Print "Device Type: " + GXDeviceTypeName
(di.deviceType
) Print "Input Type: " + GXInputTypeName
(di.inputType
) If di.deviceType
= GXDEVICE_KEYBOARD
Then Print "Key Name: " + GXKeyButtonName
(di.inputId
)
'$Include:'gx/gx.bm'
2) The input.bas demonstrates detecting input for certain actions and then testing for those inputs in a game loop:
'$Include:'gx/gx.bi'
GXSceneCreate 500, 300
GXFrameRate 10
GXDeviceInputDetect dleft
PrintDeviceInput dleft
GXDeviceInputDetect dright
PrintDeviceInput dright
GXDeviceInputDetect djump
PrintDeviceInput djump
GXSceneStart
Sub GXOnGameEvent
(e
As GXEvent
) If GXKeyDown
(GXKEY_ESC
) Then GXSceneStop
GXDrawText GXFONT_DEFAULT, 10, 10, "Press one or more mapped inputs:"
If GXDeviceInputTest
(dleft
) Then GXDrawText GXFONT_DEFAULT, 100, 100, "Left"
If GXDeviceInputTest
(dright
) Then GXDrawText GXFONT_DEFAULT, 150, 100, "Right"
If GXDeviceInputTest
(djump
) Then GXDrawText GXFONT_DEFAULT, 125, 125, "Jump"
Sub PrintDeviceInput
(di
As GXDeviceInput
) Print GXDeviceName
(di.deviceId
) + " : ";
Print GXInputTypeName
(di.inputType
) + " : ";
If di.deviceType
= GXDEVICE_KEYBOARD
Then Print " [ "; GXKeyButtonName
(di.inputId
);
" ]";
Print " : "; di.inputValue
'$Include:'gx/gx.bm'
3) The nes-config.bas program is an example of using the API in a device setup screen with the ability to save the config:
'$INCLUDE:'gx/gx.bi'
'$INCLUDE:'control-config.bi'
GXSceneCreate 256, 240
GXSceneScale 2
ControlConfigInit
ControlConfigShow
GXSceneStart
Sub GXOnGameEvent
(e
As GXEvent
) OnControlConfigEvent e
'$INCLUDE:'gx/gx.bm'
'$INCLUDE:'control-config.bm'
Feel free to use any of the examples in your own projects. I would be very interested in any feedback you have.