AutoHotkey - Detect double press of AltGr - autohotkey

I want to detect double press on AltGr.
According to documentation:
; Example #4: Detects when a key has been double-pressed (similar to double-click).
; KeyWait is used to stop the keyboard's auto-repeat feature from creating an unwanted
; double-press when you hold down the RControl key to modify another key. It does this by
; keeping the hotkey's thread running, which blocks the auto-repeats by relying upon
; #MaxThreadsPerHotkey being at its default setting of 1.
; Note: There is a more elaborate script to distinguish between single, double, and
; triple-presses at the bottom of the SetTimer page.
~RControl::
if (A_PriorHotkey <> "~RControl" or A_TimeSincePriorHotkey > 400)
{
; Too much time between presses, so this isn't a double-press.
KeyWait, RControl
return
}
MsgBox You double-pressed the right control key.
return
AltGr is actually a combination of LControl & RAlt. So, for AltGr, script should be something like this:
~LControl & RAlt::
if (A_PriorHotkey <> "~LControl & RAlt" or A_TimeSincePriorHotkey > 400)
{
click
KeyWait, LControl & RAlt
return
}
click 2
return
But when I try to load this script, AutoHotkey gives an error:
Maybe there is a way to make an alias for key combinations.

As mentioned in the comments, KeyWait can only wait on one key (not hotkey) at a time. You only need to wait for RAlt to be released, not the combination of LCtrl and RAlt.
This works:
~LControl & RAlt::
if (A_PriorHotkey <> "~LControl & RAlt" or A_TimeSincePriorHotkey > 400)
{
KeyWait, RAlt
return
}
MsgBox Double-click
return
However, in this case KeyWait is only being used (in combination with the default #MaxThreadsPerHotkey setting of 1) to prevent key-repeat from activating the hotkey. You can remove KeyWait and it will still detect double-presses; but it will also activate if you hold AltGr down until it auto-repeats.
Note that in your case, double-pressing the hotkey would click three times: once on the first press and an additional two times on the second press.
If you just want to use AltGr as a mouse button and allow double-click, all you need is <^RAlt::Click.
If you want to perform two different actions depending on whether it is a single or double click, you must delay the response to the first click until you know whether there's a second click. For example:
<^RAlt::
KeyWait RAlt
KeyWait RAlt, D T0.4
if ErrorLevel
MsgBox Single
else
MsgBox Double
return

Related

Release of Ctrl and Shift doesn't work properly with Caps Lock

I try to do a shortcut of Ctrl+f+j so that the Ctrl+j will make j a left arrow, and the combination of f will make it a Ctrl (d should work the same with shift), so Ctrl+f+j will be considered as Ctrl+Left Arrow.
I've succeeded in making it work, but after I release the keys, f and d stuck and I can not return to normal mode.
I have the following code:
CapsLock & j::
{
Send, {blind}{Left}
return
f::Ctrl
d::Shift
return
}
CapsLock & l::
{
Send, {blind}{Right}
return
f::Ctrl
d::Shift
return
}
CapsLock up::
{
Send {Ctrl Up}
Send {Shift Up}
return
}
this works well until I release the l key because the d and f keys can not be used afterward. Any ideas why? I just can't use them regularly They keep function as Ctrl and Shift
The solution was to separate the combinations. Turns out ahk does not support nested hotkeys, and doing that messes the releases of the keys. The following code solved my problem.
CapsLock & l::Send, {blind}{Right}
CapsLock & j::Send, {blind}{Left}
CapsLock & f::Ctrl
CapsLock & d::Shift

How to map semi-colon then Esc to something other in AutoHotkey?

How to set up AutoHotkey so that when I press semi-colon then Esc ;esc in order, it will instead do something other?
:?*:;Esc::
msgbox, hello world
;; do something
;; Send, {BACKSPACE} ;; remove the ; at last
return
I think you might not be able to do it with hotstrings, but instead with a regular hotkey. Also, I think you'll need to change the comment flag to something else that isn't a semicolon. Here's my attempt:
#CommentFlag //
~;::
KeyWait , Esc , DT2
If !ErrorLevel
{
Send , {backspace}
msgbox
}
Return

AutoHotKey: Remap Alt, Ctrl, and Alt+Ctrl

I'd like to use AutoHotKey to remap:
RAlt::Volume_Down
RCtrl::Volume_Up
RAlt & RCtrl::SendInput {Volume_Mute}
While Vol up works fine with the script as above, vol down is non-repeating & mute only works if the buttons are pressed as Alt,Ctrl (not Ctrl,Alt). I understand why, I just haven't been able to come up with a solution. I can map either volume up/down or mute - but if I try to do both, the behavior is always finicky. I think what I need is something to the effect of:
if GetKeyState("RAlt") and GetKeyState("RCtrl")
{
SendInput {Volume_Mute}
}
else if GetKeyState("RAlt")
{
SendInput {Volume_Down}
}
else if GetKeyState("RCtrl")
{
SendInput {Volume_Up}
}
But this just runs & terminates. Is there a way to achieve what I'm after?
The problem with your solution is that RAlt & RCtrl::SendInput {Volume_Mute} turns RAlt into a "prefix key" and according to the Hotkeys section of Autohotkey help "The prefix key loses its native function".
Try this instead:
RAlt::Volume_Down
RCtrl::Volume_Up
#if GetKeyState("RAlt", "P")
RCtrl::Volume_Mute
#if GetKeyState("RCtrl", "P")
RAlt::Volume_Mute

AutoHotkey's for the arrow keys

I would like to hold
alt + spacebar + U for the up key
alt + spacebar + H for the left arrow key
alt + spacebar + J for the right arrow key
alt +spacebar + N for the down arrow
is this possible to do with AutoHotkey?
Hello and welcome to AutoHotkey,
you might want to have a look at the basic introduction to the heart of AHK, Hotkeys:
https://www.autohotkey.com/docs/Hotkeys.htm
Configuring hotkeys which do nothing but send another key is fairly simple. For example, alt + spacebar for the up key could be translated into
!Space::
send {up}
return
(note that alt is a modifier and can be written as !)
or short form:
!Space::send {up}
spacebar + U for the up key would be Space & U::send {up}.
But you are seeking for 2 keys PLUS a modifier (alt). For a hotkeylabel triggered by more than just two keys (alt + space + u), you'll need a workaround:
!Space:: ; once alt + space was pressed, ...
while(getKeyState("alt") || getKeyState("space") || getKeyState("u")) { ; ... while either of these is being pressed down, ...
hotKey, *u, altSpaceU, ON ; u should now send {up}
}
hotKey, *u, altSpaceU, OFF
return
altSpaceU: ; note: this is a label, no hotkey
send {up}
return
Please, don't be undeterred by this. AutoHotkey is actually quite powerful and easy to learn. Sadly, (afaik) this is the only working way to solve more-than-two-key-hotkeys.
EDIT
jesus why didnt anybody tell me..
#if getkeystate("alt")
!space::send {up}
#if
is obviously a way better solution
this was the solution
!Space::
send {up}
return

How to toggle a set of keybinds on and off?

I am trying to set up a group of keybinds that I can toggle on and off with a single button press but haven't been able to find any examples anywhere.
I want ^NumpadSub to toggle these different keybinds to turn them on and off when I press ^NumpadSub.
q::w
z::s
w::up
s::down
Can anyone help on how I would set up the code to do so?
When these are the ONLY ones, you could add one more hotkey:
^NumpadSub::Suspend
This will suspend ALL hotkeys (except the one that is used for toggling suspend on/off)
Otherwise you would have to use the actual hotkey function (http://www.autohotkey.com/docs/commands/Hotkey.htm) which allows you to turn hotkeys on/off, but the hotkey function refers to labels: (go to addresses).
If you want to ONLY have these keys act a certain way when you use ONE particular application (Game), you can use the #IfWinActive command.
e.g.
SetTitleMatchMode, 2
#IfWinActive, Notepad ; Start of Notepad specific keys.
a::Send, Haha
b::SoundBeep, 500, 500
#IfWinActive ; End of Notepad specific keys.
In that situation, Check out if this works for you! I added $ signs in front of w and s because hitting q and z would trigger the execution of w and s
Hotkey, q , MyQ, On
Hotkey, z , MyZ, On
Hotkey, $w , MyW, On
Hotkey, $s , MyS, On
Return
^NumpadSub::
KeyToggle:=!KeyToggle
Hotkey, q , % (KeyToggle ? "Off": "On")
Hotkey, z , % (KeyToggle ? "Off": "On")
Hotkey, $w , % (KeyToggle ? "Off": "On")
Hotkey, $s , % (KeyToggle ? "Off": "On")
Return
MyQ:
SendInput, w
Return
MyZ:
SendInput, s
Return
MyW:
SendInput, {Up}
Return
MyS:
SendInput, {Down}
Return