How to disable global remapping effect in AutoHotKey? - autohotkey

I want to make the Shift key behave like PageDown, then I write a script
~Shift::
KeyWait, Shift
Send, {PgDn}
However, when I want to type 'A', I pressed the key Shift and the key a, PageDown was triggered.
How to disable PageDown effect when I want to type 'A'?

Try this:
$Shift:: Send {PgDn}
The $ prefix forces the keyboard hook to be used to implement this hotkey, which as a side-effect prevents the Send command from triggering it.
https://autohotkey.com/docs/Hotkeys.htm#Symbols

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

Remap keys in loops

Background: I'm trying to have a f-mode and d-mode which means if I press down the f key and press another key like i then nothing happens excepts a shortcut. let say it will send Up key instead of f and I.
Issue: how I can remap a pressed key (I in my example) to a shortcut (Up as example)?
Code:
d::
f::{
;...
loop{
if !GetKeyState("f","p") && !GetKeyState("d","p"){
break
}
if GetKeyState("i","p") {
OutputDebug "i"
send "{up}"
continue
}
; ...
}
}
Looks like you want to make a custom combination.
From the Docs:
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
But also note:
The prefix key loses its native function: In the above example,
Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its
original/native function when it is pressed by itself. To avoid this,
a script may configure Numpad0 to perform a new action such as one of
the following:
Numpad0::WinMaximize A ; Maximize the active/foreground window.
Numpad0::Send {Numpad0} ; Make the release of Numpad0 produce a Numpad0 keystroke. See comment below.
This is to prevent holding down a key from spamming inputs while you wait to press the second part of a key combination. So essentially, your 'f' and 'd' keys will now perform their normal functions when you release them instead of initially pressing them down.
Anyways, the code would become:
f & i::
d & i::
Send {Up}
return
f::f
d::d

How to map CAPSLOCK to ESC and ESC to CAPSLOCK in Autohotkey?

So I want to map both keys:
Capslock to Esc
Esc to Capslock
And this script works for Capslock to Esc but it doesn't really work for Esc to Capslock. It causes a Capslock then Esc to be sent. I only want a Capslock sent:
Capslock::Esc
Esc::Capslock
Based on Griffin's Comment
$Capslock::Esc
$Esc::Capslock
The problem that you had was that one of the hotkeys you had was triggering another, which can be prevented by using the $ modifier.
From the docs:
This is usually only necessary if the script uses the Send command to
send the keys that comprise the hotkey itself, which might otherwise
cause it to trigger itself. The $ prefix forces the keyboard hook to
be used to implement this hotkey, which as a side-effect prevents the
Send command from triggering it. The $ prefix is equivalent to having
specified #UseHook somewhere above the definition of this hotkey.

Hotkey to remap keys does not trigger hotstring

I have a hotkey and hotstring that don't seem to work together:
9::(
:?ob0:(::){left 1}
To give some context, in one part of the code I remapped all the symbols to the number below them and vice versa so 9 prints the parenthesis (. Later on I put a hotstring that would type a closed parenthesis after an open one and then places the cursor in between.
Seems simple enough because they both work individually but together when I press the key for 9 and press the Spacebar I only get the open parenthesis ( as if the hotstring was ignored.
Am I missing something obvious?
Try using a combination of Send and InputLevel.
#InputLevel 1
9::SendEvent (
#InputLevel 0
;; Add closing parenthesis
:?ob0:(::){left 1}
Explanation
#InputLevel
By default, hook hotkeys and hotstrings ignore keyboard and mouse events generated by any AutoHotkey script. This behavior can be overridden using SendLevel or #InputLevel
By setting the 9 hotkey to a higher InputLevel, it is able to the activate other hotstrings.
SendEvent
Bizarrely, remapping a numkey to its Shift+# equivalent produced no input when #InputLevel 1 was active.
i.e. Couldn't use 1::!, 2::#, 3::#, ..., 8::*, 9::(, etc.
A Send command was used to workaround this remapping limitation
By default, Send and SendEvent are synonymous with each other.
Notes
SendPlay is not affected by InputLevel.
Remarks for Remapping Keys may explain why 9::( wouldn't trigger other hotkeys.
"Although a remapped key can trigger normal hotkeys, by default it cannot trigger mouse hotkeys or hook hotkeys."
Related
#InputLevel, Send, Remapping (Remarks)

Remove auto modifier from hotkeys

Is there a way to make a hotkey with a modifier that doesn't automatically put the modifier on the output key? ex: ^a::b -- except, when I hit ctrl+a it just gives me ctrl+b, when I just want ctrl+a to = b.
Playing a game which for some reason won't let me use hotkey ctrl + keys so I'm circumventing the issue by setting the hotkeys as something out of the way then setting the actual hotkey I want to those keys in autohotkey.
Try:
^a::
Send, b
Send, c
Send, d
Return
or
^a::Send, bcd
Could you show the exact text of line 4 that causes the error?