how to bind a hotkey with preserving its default behaviour - autohotkey

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.

Related

Unable to utilize numpad as my keyboard trigger

I am trying to change media using Autohotkey. I want to utilize Left Shift (<+) + LWin (<#) + Numpad8. Here is my code below but it does not work. I am having issues trying to utilize my Numpad8 to change music. I already know I can use the regular numbers on top of my keyboard but I would like to utilize my numpad instead. Please advise, thanks.
<+<#NumPad8::
Send {Media_Next}
return
From the Numpad Section of the docs, they mention:
If NumLock is OFF but Shift is pressed, the system temporarily releases Shift and acts as though NumLock is ON.
This appears to also be applicable vice versa, as holding Shift while Numlock is on before clicking NumPad8 instead appears to send NumpadUp instead of Shift+Numpad8.
We can work around this by creating a Hotkey to activate when Win+NumpadUp[+ any other modifier keys (such as Shift)] is pressed, and then check ourselves whether Shift and Numlock are active. If they are, then we Send the Media key.
Current Code:
*#NumPadUp::
if(GetKeyState("NumLock", "T") and GetKeyState("Shift", "P"))
Send {Media_Next}
return
However, if we hold down Win+Shift while repeatedly pressing the key, it appears to revert back from NumpadUp to Numpad8. In order to account for this, we can reuse the body of the previous Hotkey in a new Hotkey that activates when *#NumPad8 is used.
Final Code:
*#NumPadUp::
*#NumPad8::
if(GetKeyState("NumLock", "T") and GetKeyState("Shift", "P"))
Send {Media_Next}
return
Edit: Thank you #samthecodingman for bringing up an optimization that I missed- code has been edited to include this

AutoHotKey: Change keyboard layout by pressing both shift keys

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

Shortcuts key replacemento with AutoHotkey

I just started using Autohotkey (I am a noob with it) for remapping some key combinations like CTRL+TAB (which is good if you are using your left hand) to be accessible while using your right hand.
My initial script is the following:
RControl & RShift::
{
send {LControl down}{tab}{LControl up}
return
}
It works fine, but while switching tabs in Visual Studio, for example, I cannot hold the CTRL key to keep switching tabs, I can only switch between 2 tabs.
Does anyone know if it is possible to achieve this kind of functionality with Autohotkey?
Thanks in advance.
You don't need the { } around the hotkey body. Hotkeys simply start with :: and end with return. Braces are only needed in functions afaik.
send {LControl down}{tab}{LControl up} could be expressed easier by send ^{tab} which is Ctrl+Tab. The tab-switch in VS also works with right RCtrl.
In either way, this does not work because of the send {ctrl up}. Ctrl needs to stay pressed down in order for the "Active files" window to stay open. Try:
RControl & RShift::send {RCtrl down}{tab}

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

How to remap key in certain case and not remap in other case with autohotkey?

I want to remap alt+e when caps is on in autocad.
And when capslock is not on, alt+e should open menu edit.
I use script like this
<!e::
if(GetKeyState( "CAPSLOCK", "T" ))
{
SendInput erase{space}wp{space}
}
else
{
Send !e
}
When I turn on capslock, remap key is OK.
When I turn off capslockand alt+e, menu edit opened, but closed immediately.
Thanks.
You will want a $ at the beginning of your hotkey to prevent the endless loop that the !e in your else block will trigger. You will also want to add a Return at the end of the hotkey to prevent the script from continuing into what is below this hotkey.
$!e::
if GetKeyState( "CapsLock", "T" )
Sendinput, erase{space}wp{space}
else
Sendinput, !e
Return
(Brackets are only required when if/else blocks are more than one line.)
Beyond that, the likely issue is that it's an alt hotkey that is also set to send alt.
I say this is an issue because if you press and hold alt, it activates menus,
and then the script sends alt, which will be in conflict with that.
As Ricardo said, the ideal way to script this is with the #IF command (only included with AHK_L).
#If GetKeyState("CapsLock", "T") and WinActive("AutoCAD")
!e:: SendInput, erase{space}wp{space}
#If
Notice that you can add the WinActive() function to the #If command's expression.
Try it without that first, and also realize that the application's title needs to be exactly "AutoCAD" at all times for that to work. I would recommend finding AutoCad's ahk_class,
with AHK's window spy, instead of using the title.
If it still does not work, it is likely that AHK is sending faster than AutoCAD would like to receive.
Info on how to deal with that can be found here.
Try to change your else block to this:
Send, {ALTDOWN}e{ALTUP}
I do not rely on these symbols to send keystrokes in AutoHotKey.