0
votes

I'm trying to create a Hotkey (Win+Shift+Q) that toggles on/off another Hotkey that changes the right Alt key to a left mouse click; however, I can't get it to work.

Expected Behavior:

  1. Pressing Windows+Shift+Q will initially toggle the Right-Alt key to act as a left mouse click.
  2. Pressing Windows+Shift+Q again will toggle the Right-Alt key back to acting as a Right-Alt key.
  3. Pressing Windows+Shift+Q again will revert to the left-click behavior (see #1). And so on.

Here's the most current iteration of my code:

Hotkey, RAlt, MyClick, On
#+Q::   ;Win+Shift+Q :: ::Right-Alt acts as a left mouse button click
    switch := !switch
    MsgBox %switch%
    Hotkey RAlt, % (switch ? "Off": "On")
    Return

MyClick:
    MouseClick
Return

When I run my script I get the following error after clicking OK on the MsgBox and the script quits:

Error: Nonexistent hotkey

Specifically: RAlt

    Line#
    141: Hotkey,RAlt,MyClick,On
    143: switch:=!switch
    144: MsgBox %switch%
 -->145: Hotkey RAlt, % (switch ? "Off": "On")
    146: Return
    149: MouseClick
    150: Return

 The current thread will exit.

Most of the other posts that might relate (Can AutoHotKey toggle keymapping?, Autohotkey: Toggle a set of keybinds on and off) only deal with key to key mapping and not key to mouse mapping. I can't tell if that is the cause of my issues or not.

Previously I had this, but the Win+Shift+Q didn't toggle the behavior, RAlt always acted as a left-click so I commented it out:

#+Q::   ;Win+Shift+Q :: ::Right-Alt acts as a left mouse button click
    RAlt::LButton
    ;Hotkey, RAlt, Toggle ;Does not work for some reason

    int += 1
    test := mod(int, 2) = 0
    if (test) {
        msgbox on
        Hotkey, RAlt, On
    }
    else {
        msgbox off
        Hotkey, leftClick, Off
    }
Return

I'll also add that I would like this behavior across Windows, not just a single application (which also seems to be a topic in other posts that allows for the #IfWinActive-type suggestions/solutions).

2

2 Answers

0
votes

I tried your current iteration of code in AutoHotkey v1.1.13.01 Unicode 32-bit and I don't have any errors after pressing OK on the message box, the script works as advertised.

Try updating your AutoHotkey version here: http://ahkscript.org/download/ and see if the problem persists.

0
votes
bState:=False

#If bState
   RAlt::Click
#If

#+vk51:: ; win + shift + q
   KeyWait, vk51
   TrayTip, % "state of switch", % (bState:=!bState) ? "on":"off"
   Return