AutoHotKey: Change keyboard layout by pressing both shift keys - autohotkey

I have used Linux and KDE for a long time and my muscle memory wants to switch keyboard layouts by pressing both shift keys simultaneously. Can I use AutoHotKey to implement that on Windows?
I lack two pieces of information:
How do I remap "both shift keys pressed at the same time"? I can use + to capture the pressing of one shift key, but how about both?
How can I send the key combination that Windows uses to switch layouts (Ctrl+Shift in my current setup)? More generally, how can I remap something to a key combination?

Use combo key notation and L/R prefix, see the documentation (or the help file):
LShift & RShift::send {LShift down}{LCtrl down}{LShift up}{LCtrl up}

I wanted to disable native Windows hotkey altogether. The following works well so far, including console windows.
~RShift & ~LShift::
~LShift & ~RShift::
INPUTLANGCHANGE_FORWARD := 0x2
WM_INPUTLANGCHANGEREQUEST := 0x0050
WinGet, windows, List
Loop % windows {
PostMessage WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_FORWARD, % Lan, , % "ahk_id " windows%A_Index%
}
return

Related

Creating a hotkey with backslash in AutoHotkey

I want to create a hotkey using a backslash key (\). Namely, I would like to assign Media_Prev to Windows button + backslash. I have tried both LWin & \::Media_Prev and #\::Media_Prev. However, these do not work: it just normally sends the backslash character. In AutoHotkey's key history, I do see that both \ and LWin register when I press this key combination.
Interestingly, something like LWin & c::Media_Prev or #v::Media_Prev does work well, just not with the backslash character.
This worked for me, using AHK version 1.1.13.01
LWin & \:: run Notepad
you can also use scan codes - something like
SC15B & SC02B:: run Notepad
should have the same effect
reference: here
I successfully tested the following:
<#vk dc::Send {Media_Prev}
< is Left key
# is Windows key
VK DC is virtual key number DC (you can find this code using AHK menu View > Key History and Script Info). With VK, the same key works regardless of active keyboard layout.
Note: Don't do LWin & key or Ctrl & key etc... & has a bit different meaning. For the above purpose AHK provides prefixes as shown above, e.g. ^b or #F1 etc. See AHK help on Remapping Keys and Buttons

how to bind a hotkey with preserving its default behaviour

language = de
~Alt & Shift::
~Shift & Alt::
if (language == "de") {
language = en
}
else {
language = de
}
msgbox % language
Return
This is the code Im working on, binding a work to Alt+Shift. But I wanna preserve the default behavior of Alt+Shift which seems to be disabled after this code runs.
Any Ideas?
Use ~Alt & ~Shift:: instead of ~Alt & Shift::.
The tilde ~ prefix makes a hotkey keep its original function. As far as I understand,
~Alt & Shift::msgbox
could be translated into the following:
~Alt:: ; Alt enables the detection of a shift press
hotkey, Shift, shift, ON ; OVERRIDES the normal shift behavior, not keeping it (~Shift)
hotkey, Shift up, shiftUp, ON
return
shift:
msgBox
return
shiftUp:
hotkey, Shift, shift, OFF
hotkey, Shift up, shiftUp, OFF
return
. Alt is a hotkey and Shift also is one, being activated on Alt pressure. But that very Shift hotkey overwrites any normal shift behaviour.
So, alt & shift is actually two different hotkeys combined to one. Each of them needs to be prefixed with its own options.
My solution works with another concurring AutoHotkey script. But it seems not with the windows built-in hotkey shift+alt aka alt+shift. Even if the ahk hotkey trigger looks like that
~Alt & ~Shift::
, this will STILL override the usual behavior which is Window's language switching mechanism. I do not know the reason behind it, maybe someone else does? I can only think of a (fairly simple) workaround:
~Alt & ~Shift::
send {alt down}{shift}{alt up}
; (your other actions)
return
In this case, you might even want to remove the ~'s in order to prevent confusion.

Emulating Ctrl + Spacebar + AlphabeticalKey with Autohotkey

My problem :
^space & c::
send {F2}
send {Escape}
but it didn't work, how do I emulate Ctrl+Space + AlphabeticaklKey ?
As my previous speakers said, it can't be done easily. Here's my suggestion, it seems to work fine:
^space::
Loop {
if(GetKeyState("c")) {
break
}
if(!GetKeyState("CTRL") || !GetKeyState("SPACE")) {
return
}
Sleep, 50
}
msgbox, You have pressed CTRL+SPACE+C
return
The code is pretty self-explanatory. When CTRL + SPACE is pressed, it waits until either one of both is released or C is pressed. The latter triggers the actual functionality, otherwise it will return.
I actually don't like it very much, because theoretically it may fail in some cases (e.g. when CTRL + SPACE + C is pressed and released before the execution reaches the check for the state of C; although that seems very unlikely).
Update
There's also a way using #If. I recommend using that since it's more sophisticated and reliable. This is due to the fact that it doesn't need any loops:
#If GetKeyState("SPACE")
^c::Msgbox, You have pressed CTRL+SPACE+C
#If GetKeyState("c")
^space::Msgbox, You have pressed CTRL+SPACE+C
As far as I know, you can only combine two non-hotkey keys with the syntax:
space & c:: msgbox space and c
You can read it here
You can define a custom combination of two keys (except joystick
buttons) by using & between them. In the below example, you would hold
down Numpad0 then press the second key to trigger the hotkey:
Numpad0 & Numpad1::MsgBox You pressed Numpad1 while holding down
Numpad0. Numpad0 & Numpad2::Run Notepad
Trying to use control as well like in: space & c & control or space & ^c or any other combination will result in compile error.
My recommendation is that you don't combine that three keys together. Look for a pure hotkey combination or use another more or less useless key.
#!c:: windows + alt + c
AppsKey & c::
Remember that if you use a normal key as modificator, you have to remap it to itself to keep the original functionality, for example with the menu key (appskey):
AppsKey:: Send {Appskey}
AppsKey & c:: ;do what you want
There are actually a couple ways to get help. First of all the authors of this language have moved to a new domain ahkscript.org. It is always welcome to ask questions like these in our forum. I just happened to be digging through this site today and saw this by accident.
When you have more than one line of code after a hotkey you need to have a return follow it:
^space & c::
send {F2}
send {Escape}
return
Hope that helps

Using AutoHotKey scripting language is it possible to swap Alt and Ctrl keys and also retain native AltTab key behaviour?

Currently I must Ctrl & Tab to perform a traditional AltTab.
I've already swapped my Alt and Ctrl keys. However, I still want to be able to AltTab traditionally
This is what I have so far:
LCtrl::Alt
Alt::LCtrl
LCtrl & Tab::AltTab
LCtrl & Tab::Send {Alt Down}{Tab}{Alt Up}
Is the simple answer and will switch between the last 2 open windows.
If you want proper functionality, you'll have to read:
http://www.autohotkey.com/docs/commands/GetKeyState.htm
and use it in conjuction with
GetKeyState, OutputVar, LCtrl
If OutputVar = "D"
Send {Tab}
etc
This is works for me
LControl & Tab::AltTab

How can I simulate the Windows Key in Autohotkey

I have an old IBM Model M from 1994. It's awesome, but it doesn't have a Windows key. I'd like to use AutoHotkey to map the combination of Ctrl + Alt to simulate the Windows key in order to take advantage of the default Windows shortcuts. Here's what I have:
LCtrl & LAlt :: Send {LWin}
It was suggested that maybe windows is overriding the Ctrl + Alt combo, so I also tried:
~Alt & Space :: Send {LWin}
Neither of these work. I'd at least like to be able to open the Start Menu from the keyboard (Ctrl + Esc is too awkward.)
It seems the windows key is not working as long as either ctrl or alt is pressed. The following script works for me:
<^LAlt::
KeyWait Alt
KeyWait Ctrl
Send {RWin}
return
<!LCtrl::
KeyWait Alt
KeyWait Ctrl
Send {RWin}
return
You can press the left Ctrl and left alt in any order, and when you release both, the windows key is generated. This way you will not be able to send combination like Windows-E. If you want that too, you can do something like:
<^<!e::
KeyWait Alt
KeyWait Ctrl
Send {RWin down}e{RWin up}
return
<^<!space::
KeyWait Alt
KeyWait Ctrl
Send {RWin}
return
Now press leftctrl-leftalt-e to genereate windows-e, and press leftctrl-leftalt-space for just the windows key.
I'm also using an IBM Model M. I've mapped RCtrl to the RWin key using KeyTweak (in Windows 7 and XP).
You can get KeyTweak here: KeyTweak homepage
(you can edit directly your registry but it's much easier to use the above program).
With this approach you can continue to use Win+R, Win+Tab (in Windows 7), Win+E, etc. and your Autohotkey scripts will also detect your RCtrl keypresses as RWin.
Try this:
Ctrl & Q::send {LWin}
Someone suggested that I make my comment an answer.
I did very similar to what wimh did above, but I removed the KeyWait commands. Normal keyboard hotkeys don't wait until you have all of your fingers lifted off of the keys, you can press the hotkey combination and then hold the keys down, and have the action still occur. The >^>! and <^<! make the command work with either the left or the right Alt and Ctrl keys.
; Open explorer
>^>!e::
Send #e
return
<^<!e::
Send #e
return
; Lock workstation, #L and downLup don't work
>^>!L::
DllCall("LockWorkStation")
return
<^<!L::
DllCall("LockWorkStation")
return
; Run dialog
<^<!r::
Send {RWin down}r{RWin up}
return
>^>!r::
Send {RWin down}r{RWin up}
return
I'm not sure why some key combinations you can use #, but others like the Run dialog require you to RWin down, press r, then RWin up. This must be a quirk with Windows.
I've uploaded my autohotkey.ahk file to Github, if anyone is interested!