#1 2024-09-23 18:00:10

Serebro
Reputation: +1067
Location: Moldova
Registered: 2014-03-28
Posts: 2,671
Windows 10 Chrome 129.0

Joysticks and AutoHotkey Automation

Hi.
I have recently got myself a joystick and wanted to share some settings.
This script is for AutoHotkey 2.0 program that allows automation.

What does it do?

1) Activates/maps hat switch function to mouselook.
The way it works is it determines your screen size and divides these values by 2. For example if your monitor is 1920x1080 it sets the x axis to 1920/2 = 960 and y axis = 1080/2 = 540 (determines middle of the screen or initial position) and when you hold the hat switch it activates the mouse view button (by pressing LShift by default) and moves to certain direction. When you release the hat switch the cursor returns to middle of the screen (initial coordinates 960x540). If you have a bigger monitor, the code will determine the middle of the scree automatically, so it should work on 4k whatever you got there smile

2) Sets individual speeds for cursor movement for up, down, left, right when hat switch is pressed.

3) Parachute function.
Joystick3 button when pressed sends a signal that numpad7 button on keyboard is pressed ( I have it mapped on parachute).

4) Spawn menu/map function.
Joystick4 button when pressed sends a signal that ENTER button on the keyboard is pressed ( opens map/spawn menu). This is useful since when an empty apc is on the map you can check if it's friendly by seing if a spawn point is indicated in it.


Script:

#Requires AutoHotkey 2.0
#SingleInstance

; Set individual movement speeds for each direction of the hat switch
MoveSpeedUp := 0.5
MoveSpeedDown := 0.5
MoveSpeedLeft := 1
MoveSpeedRight := 1

; Automatically determine monitor size and set cursor default position to mid screen
ScreenWidth := SysGet(78)
ScreenHeight := SysGet(79)
MidX := ScreenWidth // 2
MidY := ScreenHeight // 2
IsMoving := false
IsHatSwitchActive := false

; Initial cursor coordinates
CoordX := MidX
CoordY := MidY

Loop 16
    if GetKeyState(A_Index "JoyPOV") = -1 {
        JoystickNumber := A_Index
        break
    }

SetTimer(MouseLook, 2)
SetTimer(CheckJoystickButtons, 1) ; Check joystick buttons every 1 ms
SendMode "Event"

MouseLook() {
    global IsMoving, MidX, MidY, JoystickNumber, IsHatSwitchActive
    global CoordX, CoordY
    global MoveSpeedUp, MoveSpeedDown, MoveSpeedLeft, MoveSpeedRight

    ; Get the current state of the HAT switch
    JoyPOV := GetKeyState(JoystickNumber "JoyPOV")

    ; Check if the HAT switch is neutral (released)
    if JoyPOV = -1 {
        if IsHatSwitchActive {
            if GetKeyState("LShift", "P") {
                Send("{LShift up}")
            }
            IsHatSwitchActive := false
            CoordX := MidX
            CoordY := MidY
            MouseMove(CoordX, CoordY, 1, "R")
        }
        IsMoving := false
        return
    }

    if !IsHatSwitchActive {
        if !GetKeyState("LShift", "P") {
            Send("{LShift down}")
        }
        IsHatSwitchActive := true
    }

    MoveX := 0
    MoveY := 0

    switch JoyPOV {
        case 0:
            MoveY := -MoveSpeedUp
        case 4500:
            MoveX := MoveSpeedRight, MoveY := -MoveSpeedUp
        case 9000:
            MoveX := MoveSpeedRight
        case 13500:
            MoveX := MoveSpeedRight, MoveY := MoveSpeedDown
        case 18000:
            MoveY := MoveSpeedDown
        case 22500:
            MoveX := -MoveSpeedLeft, MoveY := MoveSpeedDown
        case 27000:
            MoveX := -MoveSpeedLeft
        case 31500:
            MoveX := -MoveSpeedLeft, MoveY := -MoveSpeedUp
    }

    if MoveX != 0 || MoveY != 0 {
        CoordX += MoveX
        CoordY += MoveY
        MouseMove(CoordX, CoordY, 1, "R")
        IsMoving := true
    }
}

CheckJoystickButtons() {
    ; Handle button 3 (Numpad7)
    if GetKeyState("Joy3", "P") {
        static isPressed3 := false
        if !isPressed3 {
            Send("{Numpad7 down}") ; Press num7
            isPressed3 := true
        }
    } else {
        static isPressed3 := true
        if isPressed3 {
            Send("{Numpad7 up}") ; Release num7
            isPressed3 := false
        }
    }

    ; Handle button 4 (Enter)
    if GetKeyState("Joy4", "P") {
        static isPressed4 := false
        if !isPressed4 {
            Send("{Enter down}") ; Press Enter
            isPressed4 := true
        }
    } else {
        static isPressed4 := true
        if isPressed4 {
            Send("{Enter up}") ; Release Enter
            isPressed4 := false
        }
    }
}


How to use:
Install AutoHotKey 2.0 program.
Create a new empty script file in it. It should be in .ahk format by default. Put it on desktop.
Open it with a text editor, such as notepad++ and put my code and save it.
Right click the script on destop and Run as Administrator.
Test by pressing joystick buttons, hat switch moves mouse cursor, joy3 is numpad7, joy4 is enter.

* You can edit the script and remap the buttons if needed.

Last edited by Serebro (2024-09-23 18:16:47)

Offline

    Positive reputation 4   Negative reputation 0

Board footer