Improved Joystick to Keyboard remap script

Post your working scripts, libraries and tools
User avatar
evilC
Posts: 4181
Joined: 27 Feb 2014, 12:30

Improved Joystick to Keyboard remap script

17 Jan 2016, 13:43

I noticed that the joystick remap script sample snippets were very basic. Whilst they showed how to get information from sticks, they were not very functional (eg No support for diagonals, not easy to configure, etc), not very elegant (Different code to process X and Y etc) and not very efficient (Repeat concatenations etc).

So I wrote my own.
This one supports remapping of axes, buttons and hats to keyboard (or mouse) buttons, and is fairly easy to configure, you just have to learn how to edit an Indexed Array.

Techniques used in this script show how to:
  • Handle X and Y input of a stick with the same code, through the use of Arrays and Loops.
  • Extract axis state from the POV's degree-based values through the use of pre-built Associative Arrays.
  • Emulate the up event for joystick buttons.
  • Make Joystick axis values easier to work with by shifting the scale from 0->100 to -50->+50, and use of the math function abs() to get magnitude of deflection.
  • Use "function binding" to cause a function to get called when a button is pressed, and have that function be passed the number of the button that was pressed.
  • Create an array of pre-built strings for use with GetKeyState(), to reduce CPU load.

The default configuration remaps:
X and Y axes to the Arrow Keys (With the "AxisRepeat" option on, so keys get repeatedly hit when you hold the joystick) with a 10% deadzone.
Buttons 1-8 to number keys 1-8.
POV hat to WSAD keys.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Edited:
Fixed bug with HatKeys and AxisKeys being mixed up
Fixed commented out send strings. Whoops!
Removed maths from hat direction calculations, used associative array.
Last edited by evilC on 21 Feb 2018, 10:56, edited 1 time in total.
User avatar
evilC
Posts: 4181
Joined: 27 Feb 2014, 12:30

Re: Improved Joystick to Keyboard remap script

17 Jan 2016, 15:43

Or if you want to avoid all string concatenation while processing input:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by evilC on 21 Feb 2018, 10:56, edited 1 time in total.
User avatar
SnowFlake
Posts: 368
Joined: 28 Apr 2015, 05:41
Google: floowsnaake
GitHub: floowsnaake
Contact:

Re: Improved Joystick to Keyboard remap script

17 Jan 2016, 15:58

nice script thank you for sharing!
:yawn:
nwalker83
Posts: 2
Joined: 25 Jan 2016, 19:11

Re: Improved Joystick to Keyboard remap script

25 Jan 2016, 19:20

Thank you for the post. I'd like to modify the script so that the remap only works with certain application being the foreground process. I tried to add an #IfWinActive line but that didn't seem to work (all the applications are always affected still). Ultimately I would like to apply different remaps for different applications...
nwalker83
Posts: 2
Joined: 25 Jan 2016, 19:11

Re: Improved Joystick to Keyboard remap script

26 Jan 2016, 16:47

Thank you, evilC! I see from your GitHub page you're actually developing a URC program that will cover my use case also. Do you think it is ready for general consumption?
tetsuya_tsurugi

Re: Improved Joystick to Keyboard remap script

20 Feb 2018, 19:49

Can you write a scripts for the triggers (I have a xbox360 controller)?. I try to modify the script to include LT and RT but no success.

thanks
User avatar
evilC
Posts: 4181
Joined: 27 Feb 2014, 12:30

Re: Improved Joystick to Keyboard remap script

21 Feb 2018, 05:44

nwalker83 wrote:Thank you, evilC! I see from your GitHub page you're actually developing a URC program that will cover my use case also. Do you think it is ready for general consumption?

Both the old AHK UCR (See link in signature below) and the new C# UCR do joystick remappings.

tetsuya_tsurugi wrote:Can you write a scripts for the triggers (I have a xbox360 controller)?. I try to modify the script to include LT and RT but no success.thanks

Can you be more specific? What did not work? Bear in mind that if you want to read an xbox controller via AHK, when you read the triggers you can only read them as one axis. Using the built-in AHK commands, it is impossible to tell the difference between no triggers pressed and both triggers pressed.
If you want to write code to support triggers, you can use Lexikos' XInput Library.
If you just want to do straight remappings, look at my UCR-C# project linked above
tetsuya_tsurugi

Re: Improved Joystick to Keyboard remap script

21 Feb 2018, 08:13

Can you be more specific? What did not work? Bear in mind that if you want to read an xbox controller via AHK, when you read the triggers you can only read them as one axis. Using the built-in AHK commands, it is impossible to tell the difference between no triggers pressed and both triggers pressed.
If you want to write code to support triggers, you can use Lexikos' XInput Library.
If you just want to do straight remappings, look at my UCR-C# project linked above


Well, The triggers are axis Z. In your code, this axis don't exist. I tried with no success add this axis in the code, but I'm not a programmer, I try to do so just for logic and reading. One of my problem is in triggers, "null state" is 50, no 0.
User avatar
evilC
Posts: 4181
Joined: 27 Feb 2014, 12:30

Re: Improved Joystick to Keyboard remap script

21 Feb 2018, 10:58

Hmm, looking back at the code, if you want to use axes other than X and Y, you need to edit the section under the comment ; === Internal vars - do not edit ===, so I removed that comment.
tetsuya_tsurugi

Re: Improved Joystick to Keyboard remap script

21 Feb 2018, 19:00

Well... I did some modifications and finaly I can config triggers buttons.

This is my code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Thanks for your help and thanks for your amazing code :D
anti3d
Posts: 10
Joined: 31 Aug 2016, 05:48

Re: Improved Joystick to Keyboard remap script

18 Mar 2018, 10:18

tetsuya_tsurugi wrote:Well... I did some modifications and finaly I can config triggers buttons.

This works for me, thanks! Maybe you can help? I need analog turbo on gamepad. I don't understand how can i loop only one key?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Lu3d and 20 guests