RemapJoystick.htm joystick-axes sample fails

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
bobh_slo
Posts: 1
Joined: 25 Mar 2023, 18:45

RemapJoystick.htm joystick-axes sample fails

Post by bobh_slo » 25 Mar 2023, 19:39

I'm trying to use the sample script RemapJoystick.htm joystick-axes but it throws an error:

This variable has not been assigned a value.
Specifically: local KeyToHoldDown
https://www.autohotkey.com/docs/v2/misc/RemapJoystick.htm#joystick-axes

This is my first attempt using AHK - what am I doing wrong?

I want to change it to use JoyZ to send steering keys to SnowRunner. I can see values in GetKeyState("JoyY") so I think I'm close.

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: RemapJoystick.htm joystick-axes sample fails

Post by neogna2 » 26 Mar 2023, 10:47

(Answer edited, apologies for any confusion caused by earlier versions)

The error you describe is because the line KeyToHoldDownPrev := KeyToHoldDown tries to read from a variable before that variable is declared. To fix that add static KeyToHoldDown := "" at the start of the function.

If that fixes all issues for you then be happy and feel no need to read the rest below.

If you in addition get Error: Expected a Number but got an empty string then keep reading. I think this second problem is caused by a few things in combination.

1. Windows may have recognized your plugged in joystick as another number than 1.
The help page https://www.autohotkey.com/docs/v2/KeyList.htm#Joystick documents that this can happen
Note: If you have trouble getting a script to recognize your joystick, one person reported needing to specify a joystick number other than 1 even though only a single joystick was present. It is unclear how this situation arises or whether it is normal, but experimenting with the joystick number in the joystick test script can help determine if this applies to your system.
I can confirm that this sometimes happens.

2. The RemapJoystick script presupposes that the joystick is number 1 in the line JoyX := GetKeyState("JoyX")

3. When line JoyX := GetKeyState("JoyX") is executed and no joystick with number 1 is found GetKeyState returns an empty string.

That return value in this situation is not documented. But that an empty string (and not an integer) is in fact returned can be confirmed by running this oneliner script MsgBox Type(GetKeyState("Joy1")) with no joystick plugged in. That is either a bug or a documentation gap.

4. When the variable JoyX has an empty string the line if JoyX > 70 throws an error because > requires numeric comparison values.

To use GetKeyState on joystick number 2 use 2JoyX and similarly 3JoyX for joystick number 3 and so on. For example JoyX := GetKeyState("2JoyX")

To figure out which number your joystick is recognized as you can use this script

Code: Select all

Loop 6
    if GetKeyState(A_Index "JoyName")
        MsgBox A_Index
There is also a Joystick Test Script that automatically detects the number of the plugged in joystick at
https://www.autohotkey.com/docs/v2/scripts/index.htm#JoystickTest

However if you use an Xbox controller from 2013 or newer (anything newer than an Xbox360 controller) AutoHotkey's built in joystick hotkeys and GetKeyState for joystick button and stick states only works when an AutoHotkey GUI is active.
That is not yet documented in the v2 AutoHotkey help file.

So to get the whole joystick test script working with an Xbox controller add these lines before the line that starts with ; Auto-detect

Code: Select all

MyGui := Gui(, "TestMe")
MyGui.Show("w500 h500")
MyGui.OnEvent("Close", (*) => ExitApp() )
and then test the joystick buttons while the TestMe GUI is active.

To make v2 AutoHotkey detect Xbox controllers states and buttons also when other windows are active use the v2 Xinput.ahk library.

Post Reply

Return to “Ask for Help (v2)”