VirtualKey for Gamepad/Joystick input

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Tue Jan 09, 2024 9:36 am

I'm trying to make an "extra" keyboard for our workshop users so that they can make inputs using their feet. For this I plan to use a gamepad DIY module (usually made to build arcade machines). I just need to find a way to use the input from joystick.

https://learn.microsoft.com/en-us/uwp/a ... inrt-22621
https://learn.microsoft.com/en-us/windo ... _keystroke
https://learn.microsoft.com/en-us/windo ... -key-codes

Looking at these links it looks like it similar to how the mouse and keyboard is using virtual keys. It's possible to address individual joysticks/gamepads but I'm only interested in any joystick's/gamepad's button being clicked.

Will MS allow for gamepad input for the OnEvent?

It's a good complement for the use of keyboard input because it would not ever be a conflict with other app's keyboard commands.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Tue Jan 09, 2024 9:52 am

If we look in the help: https://help.mjtnet.com/article/262-virtual-key-codes we see that these VK codes are reserved:

195 GamepadA
196 GamepadB
197 GamepadX
198 GamepadY
199 GamepadRightShoulder
200 GamepadLeftShoulder
201 GamepadLeftTrigger
202 GamepadRightTrigger
203 GamepadDPadUp
204 GamepadDPadDown
205 GamepadDPadLeft
206 GamepadDPadRight
207 GamepadMenu
208 GamepadView
209 GamepadLeftThumbstickButton
210 GamepadRightThumbstickButton
211 GamepadLeftThumbstickUp
212 GamepadLeftThumbstickDown
213 GamepadLeftThumbstickRight
214 GamepadLeftThumbstickLeft
215 GamepadRightThumbstickUp
216 GamepadRightThumbstickDown
217 GamepadRightThumbstickRight
218 GamepadRightThumbstickLeft

But in this link they are Gamepad VK's. https://learn.microsoft.com/en-us/uwp/a ... inrt-22621
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Marcus Tettmar » Tue Jan 09, 2024 10:39 am

You can specify any virtual key code in OnEvent with KEY_DOWN, KEY_UP, by preceding the code with VK. So, yes, if these are the VK codes that your custom keyboard triggers, you should be able to respond to them with OnEvent.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Tue Jan 09, 2024 11:33 am

I guess it wasn't that easy... this is not working:

Code: Select all

//button A = 195
OnEvent>KEY_DOWN,VK195,0,TEST

Label>START
Wait>1
Goto>START

SRT>TEST
  MDL>195 GamepadA - pressed
  Exit>
END>TEST
I found some more info here: https://learn.microsoft.com/sv-se/windo ... dfrom=MSDN
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Marcus Tettmar » Tue Jan 09, 2024 12:39 pm

Use a dialog to see what key code IS being sent.

Debug this code. Make sure the dialog is active. Hit your game pad key and see what the value of SUBTEST_KEY is:

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 607
  Top = 264
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 211
  ClientWidth = 476
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  KeyPreview = True
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,,OnKeyPress,subTest

Show>Dialog1,r

SRT>subTest
**BREAKPOINT** 
// see what value of SUBTEST_KEY is  - that is the VK code
END>subTest
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Tue Jan 09, 2024 1:24 pm

Great little code there. Thanks for that! :)

But, the gamepad isn't triggering anything so I guess that the gamepad isn't acting like a normal input after all. Would you consider looking into this and see if it's possible to add this feature to MS? :oops:
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Thu Feb 08, 2024 7:31 pm

I'm wondering if this would be considered to be added?
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: VirtualKey for Gamepad/Joystick input

Post by JRL » Thu Feb 08, 2024 9:54 pm

Hi Grovkillen,
Wondering if the gamepad is a serial device. Are you able to use it as input for standard windows software? For example, does it have any effect in notepad? Does it show up under Device Manager? If yes, what?

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Fri Feb 09, 2024 9:46 am

They are not serial as in COM, they are part of the USB standard and are identified as XInput devices. As long as a controller/gamepad/joystick comply to this standard no drivers are needed. It's been this way since the Xbox 360 got introduced.

I just happened to see that the VK enums contained the gamepad input as well and was hoping that this would mean that it worked out of the box. But, it's not...

My hope is that Macro Scheduler would find use of Xinput support since it's a great way of having input using other hardware than mouse and keyboard. XInput modules are easy to come by and thus I could make foot pedals and other breakers input to scripts without the risk of having them crash with keyboard inputs (JoyToKey.exe emulates a keyboard/mouse using a gamepad).
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Fri Feb 09, 2024 9:55 am

Here's some AutoHotKey discussions regarding XInput: https://www.autohotkey.com/board/topic/ ... oller-api/
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VirtualKey for Gamepad/Joystick input

Post by Grovkillen » Fri Feb 09, 2024 6:10 pm

Maybe a Delphi library could be used? https://blogs.embarcadero.com/add-insta ... n-windows/
Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts