AutoHotKey: Remap Alt, Ctrl, and Alt+Ctrl - autohotkey

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

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

Why autohotkey deos not send Ctrl-Space where Space is an input

Consider the following assignment:
When I type - followed by a key, the result is Ctrl-key. This work for ordinary keys.
But when the key is whitespace, it does not work.
Any idea why this happens? And how to fix the code?
-::
Input, key, L1,{LCtrl}
send, ^{%key%}
return
Edit.
Try to run the above script a program which has Ctrl-Space as a shortcut to see that it does not work. In fact, if you press - followed by Space, the script is suppose to call Ctrl-Space but it is not the case. For example:
In Microsoft Excel or in Libreoffice Calc, Ctrl-Space can select the current column.
In Emacs Ctrl-Space is reserved for setting a Mark.
Use SendInput instead.
Tested in Excel to mimic ^a, ^x, ^v, ^space
-::
Input, key, L1,{LCtrl}
SendInput, ^%key%
Return
If you want to handle "special" keys, add those keys to the list of endkeys using this syntax
Input [, OutputVar, Options, EndKeys, MatchList]
And then check to see which endkey was pressed
Tested in Firefox to mimic ^PgDn, ^PgUp
Input, key, L1,{LCtrl}{PgUp}{PgDn}
If (ErrorLevel = "EndKey:PgUp") {
SendInput ^{PgUp}
}
Else If (ErrorLevel = "EndKey:PgDn") {
SendInput ^{PgDn}
}
Else If (ErrorLevel = "EndKey:LCtrl") {
Return ;assumed you just want to abort input with the control key
}
Else {
SendInput, ^%key%
}
Return

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 - Detect double press of AltGr

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

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