问题
For instance the Shift button makes things capitals when you have it held down whereas the Capslock button does the same thing but as a toggle.
How can I turn a hold button such as the shift button act like a toggle button like the capslock button?
I have tried this:
if toggle
Send, {LShift down}
else
Send, {LShift up}
LShift::
toggle := !toggle
return
But it does not work for some reason. I figure that it will lift up or hold down the LShift button depending on the value of toggle which is changed by the LShift button. But it doesn't work, it's never a capital letter.
回答1:
First thing that popped out to me is that your If Statement is in the Autoexec section of your code and not contained within anything that will be called or executed more than once, so it's only checking Toggle state once, you could hit LShift and change Toggle all day long but no code would happen from there.
First I thought to move your code to the Hotkey section. While this semi worked I wasn't able to produce consistent results, feel free to try yourself:
LShift::
toggle := !toggle
if (toggle = true) ; added this for clarity and consitency
Send, {LShift down}
else
Send, {LShift up}
return
Below is a solution I've used in the past, when I remembered it, the reason this works is SetTimer is an interruptible by Hotkeys, so no matter where Timer routine is working the hotkey interrupts it and sets the toggle.
Solution:
SetTimer, Toggler, 100
$LShift::toggle := !toggle
Toggler:
if (toggle = true)
Send, {LShift down}
else
Send, {LShift up}
Return
来源:https://stackoverflow.com/questions/33115326/ahk-script-to-turn-hold-buttons-into-toggle-buttons