Function to Remap Joystick Button

Post your working scripts, libraries and tools
lord_ne
Posts: 2
Joined: 24 Dec 2016, 20:57

Function to Remap Joystick Button

28 Dec 2017, 22:31

Remapping joystick buttons is a pain, due to the fact that neither Autohotkey's built-in remapping feature not the Up/Down hotkey modifiers support them. The official documentation has a way to do it, but it's tedious to set up for every single joystick button:

Code: Select all

Joy3::
Send {LButton down}   ; Hold down the left mouse button.
SetTimer, WaitForButtonUp3, 10
return

WaitForButtonUp3:
if GetKeyState("Joy3")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {LButton up}  ; Release the left mouse button.
SetTimer, WaitForButtonUp3, off
return
I created a function that will automatically set up all of the necessary hotkeys and timers. Since SetTimer cannot call functions with parameters, I used BoundFunc objects.
It accepts four parameters:
• ButtonNumber: Number of the joystick button used to trigger the hotkey.
• KeyName: Name of the key to be triggered by hotkey.
• Mode: If this value is set to 1, the key will be held down as long as the joystick button is held down. If it is set to 0, the key will be held down for half a second, and then will begin rapidly pressing and releasing as long as the joystick button is held down. This is to emulate the auto-repeat behavior found on most computers.
• JoyNumber: Number of joystick to use (in case more than one is connected).

Code: Select all

CreateJoyHotkey(ButtonNumber, KeyName, Mode = 0, JoyNumber = 1) {
  global
  local JoyPrefix := JoyNumber "Joy"
  if(Mode = "1") {
    JoyButtonObj%ButtonNumber% := Func("JoyButtonW").bind(ButtonNumber, KeyName, JoyPrefix)
    JoyWaitObj%ButtonNumber% := Func("JoyWait").bind(ButtonNumber, KeyName, JoyPrefix)
  }
  else {
    JoyButtonObj%ButtonNumber% := Func("JoyButtonR").bind(ButtonNumber, KeyName, JoyPrefix)
    JoyDelayObj%ButtonNumber% := Func("JoyDelay").bind(ButtonNumber, KeyName, JoyPrefix)
    JoyRepeatObj%ButtonNumber% := Func("JoyRepeat").bind(ButtonNumber, KeyName, JoyPrefix)
  }
  Hotkey, %JoyPrefix%%ButtonNumber%, % JoyButtonObj%ButtonNumber%
}

JoyButtonR(ButtonNumber, KeyName, JoyPrefix) {
  Send {%KeyName% down}
  SetTimer, % JoyDelayObj%ButtonNumber%, -500
}

JoyDelay(ButtonNumber, KeyName, JoyPrefix) {
  if GetKeyState(JoyPrefix . ButtonNumber) {
    SetTimer, % JoyRepeatObj%ButtonNumber%, 25
  }
  else {
    Send {%KeyName% up}
  }
}

JoyRepeat(ButtonNumber, KeyName, JoyPrefix) {
  Send {%KeyName% up}
  if GetKeyState(JoyPrefix . ButtonNumber) {
    Send {%KeyName% down}
  }
  else {
  SetTimer, % JoyRepeatObj%ButtonNumber%, off
  }
}

JoyButtonW(ButtonNumber, KeyName, JoyPrefix) {
  Send {%KeyName% down}
  SetTimer, % JoyWaitObj%ButtonNumber%, 25
}

JoyWait(ButtonNumber, KeyName, JoyPrefix) {
  if not GetKeyState(JoyPrefix . ButtonNumber) {
    SetTimer, % JoyWaitObj%ButtonNumber%, off
    Send {%KeyName% up}
  }
}
A_AhkUser
Posts: 1118
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Function to Remap Joystick Button

29 Dec 2017, 23:00

Hi lord_ne,

This function actually comes in handy and works as expected. Thanks for sharing. I especially like how you split it in two modes. When I see the code I I thought it could be nice that the function can emulate not only a remap like the following:

Code: Select all

1Joy3 Down:: ; [sic]
send, b
return
1Joy3 Up:: ; [sic]
send, b
return
but also something like that:

Code: Select all

1Joy3 Down:: ; [sic]
f()
return
1Joy3 Up:: ; [sic]
g()
return
f() {
; some code
}
g() {
; some code
}
Hope it makes sense. In that respect, this inspired me and I decided to write a class mainly based on your source (I'm not an aficionado of oop but here one can also prefer pass 'this' to multiple method instead of many parameter to multiple functions). This is simply a rewriting oop of your function except from the fact that I attempted to substitute KeyName by such callbacks as in the second example. Also, It could be nice to have GUI as a front end for this function, especially for a remap like your function actually does.
Once again thanks for sharing.;)

Cheers

Edit: I merged sources and accordingly update the link to a more general class Hotkey implementing the fonctionality
my scripts

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Leli196 and 24 guests