# 重映射操纵杆为键盘或鼠标

## 目录

- 重要事项
- 让操纵杆按钮发送键击或鼠标点击
  - 不同的方法
  - 自动重复键击
  - 上下文相关的操纵杆按钮
  - 用操纵杆作为鼠标
- 让其他操纵杆控制器发送按键或鼠标点击
  - 操纵杆轴
  - 操纵杆 POV 帽
  - 自动重复按键
- 备注
- 相关话题

## 重要事项

- Although a joystick button or axis can be remapped to become a key or mouse button, it cannot be remapped to some other joystick button or axis. That would be possible only with the help of a joystick emulator such as vJoy.
- AutoHotkey identifies each button on a joystick with a unique number between 1 and 32. To determine these numbers, use the joystick test script.

## Making a Joystick Button Send Keystrokes or Mouse Clicks

### Different Approaches

Below are three approaches, starting at the simplest and ending with the most complex. The most complex method works in the broadest variety of circumstances (such as games that require a key or mouse button to be held down).

#### 方法 #1

This method sends simple keystrokes and mouse clicks. 例如:

    Joy1::Send "{Left}"  ; Have button #1 send a left-arrow keystroke.
    Joy2::Click  ; Have button #2 send a click of left mouse button.
    Joy3::Send "a{Esc}{Space}{Enter}"  ; Have button #3 send the letter "a" followed by Escape, Space, and Enter.
    Joy4::Send "Sincerely,{Enter}John Smith"  ; Have button #4 send a two-line signature.

To have a button perform more than one function, put the first function beneath the button name and make the last line a return. 例如:

    Joy5::
    {
        Run "notepad"
        WinWait "Untitled - Notepad"
        WinActivate
        Send "This is the text that will appear in Notepad.{Enter}"
    }

See the Key List for the complete list of keys and mouse/joystick buttons.

#### 方法 #2

This method is necessary in cases where a key or mouse button must be held down for the entire time that you're holding down a joystick button. The following example makes the joystick's second button become the left-arrow key:

    Joy2::
    {
        Send "{Left down}"  ; Hold down the left-arrow key.
        KeyWait "Joy2"  ; Wait for the user to release the joystick button.
        Send "{Left up}"  ; Release the left-arrow key.
    }

#### 方法 #3

This method is necessary in cases where you have more than one joystick hotkey of the type described in Method #2, and you sometimes press and release such hotkeys simultaneously. The following example makes the joystick's third button become the left mouse button:

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

    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, 0
    }

### Auto-repeating a Keystroke

Some programs or games might require a key to be sent repeatedly (as though you are holding it down on the keyboard). The following example achieves this by sending spacebar keystrokes repeatedly while you hold down the joystick's second button:

    Joy2::
    {
        Send "{Space down}"   ; Press the spacebar down.
        SetTimer WaitForJoy2, 30  ; Reduce the number 30 to 20 or 10 to send keys faster. Increase it to send slower.
    }

    WaitForJoy2()
    {
        if not GetKeyState("Joy2")  ; The button has been released.
        {
            Send "{Space up}"  ; Release the spacebar.
            SetTimer, 0  ; Stop monitoring the button.
            return
        }
        ; Since above didn't "return", the button is still being held down.
        Send "{Space down}"  ; Send another Spacebar keystroke.
    }

### Context-sensitive Joystick Buttons

The #HotIf directive can be used to make selected joystick buttons perform a different action (or none at all) depending on any condition, such as the type of window that is active.

### Using a Joystick as a Mouse

The Joystick-To-Mouse script converts a joystick into a mouse by remapping its buttons and axis control.

## Making Other Joystick Controls Send Keystrokes or Mouse Clicks

To have a script respond to movement of a joystick's axis or POV hat, use SetTimer and GetKeyState.

### Joystick Axes

The following example makes the joystick's X and Y axes behave like the arrow key cluster on a keyboard (left, right, up, and down):

    SetTimer "WatchAxis", 5

    WatchAxis()
    {
        JoyX := GetKeyState("JoyX")  ; Get position of X axis.
        JoyY := GetKeyState("JoyY")  ; Get position of Y axis.
        KeyToHoldDownPrev := KeyToHoldDown  ; Prev now holds the key that was down before (if any).

        if JoyX > 70
            KeyToHoldDown := "Right"
        else if JoyX < 30
            KeyToHoldDown := "Left"
        else if JoyY > 70
            KeyToHoldDown := "Down"
        else if JoyY < 30
            KeyToHoldDown := "Up"
        else
            KeyToHoldDown := ""

        if KeyToHoldDown = KeyToHoldDownPrev  ; The correct key is already down (or no key is needed).
            return  ; Do nothing.

        ; Otherwise, release the previous key and press down the new key:
        SetKeyDelay -1  ; Avoid delays between keystrokes.
        if KeyToHoldDownPrev   ; There is a previous key to release.
            Send "{" KeyToHoldDownPrev " up}"  ; Release it.
        if KeyToHoldDown   ; There is a key to press down.
            Send "{" KeyToHoldDown " down}"  ; Press it down.
    }

### Joystick POV Hat

The following example makes the joystick's POV hat behave like the arrow key cluster on a keyboard; that is, the POV hat will send arrow keystrokes (left, right, up, and down):

    SetTimer WatchPOV, 5

    WatchPOV()
    {
        static KeyToHoldDown := ""
        POV := GetKeyState("JoyPOV")  ; Get position of the POV control.
        KeyToHoldDownPrev := KeyToHoldDown  ; Prev now holds the key that was down before (if any).

        ; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
        ; To support them all, use a range:
        if POV < 0   ; No angle to report
            KeyToHoldDown := ""
        else if POV > 31500                ; 315 to 360 degrees: Forward
            KeyToHoldDown := "Up"
        else if POV >= 0 and POV <= 4500      ; 0 to 45 degrees: Forward
            KeyToHoldDown := "Up"
        else if POV >= 4501 and POV <= 13500  ; 45 to 135 degrees: Right
            KeyToHoldDown := "Right"
        else if POV >= 13501 and POV <= 22500 ; 135 to 225 degrees: Down
            KeyToHoldDown := "Down"
        else                                  ; 225 to 315 degrees: Left
            KeyToHoldDown := "Left"

        if KeyToHoldDown = KeyToHoldDownPrev  ; The correct key is already down (or no key is needed).
            return  ; Do nothing.

        ; Otherwise, release the previous key and press down the new key:
        SetKeyDelay -1  ; Avoid delays between keystrokes.
        if KeyToHoldDownPrev   ; There is a previous key to release.
            Send "{" KeyToHoldDownPrev " up}"  ; Release it.
        if KeyToHoldDown   ; There is a key to press down.
            Send "{" KeyToHoldDown " down}"  ; Press it down.
    }

### Auto-repeating a Keystroke

Both examples above can be modified to send the key repeatedly rather than merely holding it down (that is, they can mimic physically holding down a key on the keyboard). To do this, replace the following line:

    return  ; Do nothing.

With the following:

    {
        if KeyToHoldDown
            Send "{" KeyToHoldDown " down}"  ; Auto-repeat the keystroke.
        return
    }

## 备注

A joystick other than first may be used by preceding the button or axis name with the number of the joystick. 例如, `2Joy1` would be the second joystick's first button.

To find other useful joystick scripts, visit the AutoHotkey forum. A keyword search such as Joystick and GetKeyState and Send is likely to produce topics of interest.

## 相关话题

- Joystick-To-Mouse script (using a joystick as a mouse)
- List of joystick buttons, axes, and controls
- GetKeyState
- Remapping the keyboard and mouse

dftg