How to only trigger hotkey combo once? - autohotkey

I have this autohotkey script to make some media keys
RWin & AppsKey Up::Run calc1.exe
Pause::Media_Play_Pause
Ralt & F11::Media_Prev
Rcontrol & F11::Media_Prev
Ralt & F12::Media_Next
Rcontrol & F12::Media_Next
I'm trying to trigger previous and next track only once (if held),
I tried adding Up like I did with the AppsKey, but it doesn't work - it shows error when trying to run.
Interestingly Pause only triggers once.
And why can't I use CTRL+NUMLOCK? It actually triggers the Pause key...
Also, how do I assign a hotkey to the Wake key?

1,you can try this:
<!F11::
send {Media_Prev}
keywait F11
return
<!F12::
send {Media_Next}
keywait F12
return
here <! mean left alt key;
keywait is used to wait a key to to released
2,because
While Ctrl is held down, NumLock produces the key code of Pause, so use ^Pause in hotkeys instead of ^NumLock
(from https://www.autohotkey.com/docs/KeyList.htm#numpad)
here is code for test:
Pause::
tooltip you press "Pause"
return
NumLock::
tooltip you press "NumLock"
return
;ctrl+numlock
^pause::
tooltip you press "ctrl+numlock"
return
;ctrl+Pause
^CtrlBreak::
tooltip you press "ctrl+Pause"
return
3, I don't have "wake" key,so I cannot test it, you maybe can try this:
https://www.autohotkey.com/docs/KeyList.htm#SpecialKeys

Related

Making two AutoHotkey key remapping scripts work together correctly

I have two AutoHotkey scripts that enable the use of the Ctrl key on both sides of my laptop's keyboard:
Map Caps Lock to (left) Ctrl:
SetCapsLockState, Off
CapsLock::LCtrl
Map Enter to (right) Ctrl when pressed down; otherwise (if no timeout) send Enter:
Enter::RCtrl
~Enter Up::Send % "{RCtrl up}" (A_PriorKey = "Enter" ? "{Enter}" : "")
The two scripts work perfectly with almost no edge cases.
However, I'm unable to trigger Ctrl + Enter, which is a shortcut that I usually use to open a new line on my text editor. Pressing down Caps Lock and hitting Enter, nothing happens. Even if I press down (left)Ctrl (the real key) and hit Enter, nothing happens as well.
What should I do to enable both scripts to work together in order to enable Ctrl + Enter?
I managed to solve the problem in two steps:
Natively map CapsLock to Control. AutoHotkey thinks my CapsLock key is indeed the Control key, which exempts me from handling weird CapsLock on/off edge cases within AutoHotkey.
Use the following script to map Enter as dual-function RCtrl/Enter:
LShift & Enter Up::
GetKeyState, state, Shift
if (A_PriorKey = "Enter" and state = "D") {
Send +{Enter}
}
Send {LCtrl Up}{RCtrl Up}
Return
LCtrl & Enter Up::
GetKeyState, state, Control
if (A_PriorKey = "Enter" and state = "D") {
Send ^{Enter}
}
Send {LCtrl Up}{RCtrl Up}
Return
LAlt & Enter Up::
GetKeyState, state, Alt
if (A_PriorKey = "Enter" and state = "D") {
Send !{Enter}
}
Send {LCtrl Up}{RCtrl Up}
Send {LAlt Up}{RAlt Up}
Return
Enter::RCtrl
~Enter Up::
Send % "{RCtrl up}" ((A_PriorKey = "Enter") ? "{Enter}" : "")
It's super exciting because it works very well! The modifier dance between CapsLock and Enter as symmetric control keys is perfect and I can seamlessly alternate between both sides without nasty edge cases, unexpected modifier presses, releases, or {Enter} presses. The order of the declarations is very important for that to work; the edge cases must come first.
However, as you can see, it is necessary to explicitly handle Alt + Enter, Ctrl + Enter, and Shift + Enter. If I ever need Ctrl + Alt + Enter, I will need to handle that as well.
I wonder if there's a better way to make that work without having to define these additional mappings.
I've tryied on Libreoffice Writter, where CTRL+ENTER go to the next page..
The above code worked fine.
Just added ~ before Enter::RCtrl to not block native function from Enter key.
SetCapsLockState, Off
CapsLock::LCtrl
~Enter::RCtrl
~Enter Up:: Send % "{RCtrl up}" (A_PriorKey = "Enter" ? "{Enter}" : "")
P.S:
And i think you may use only one script with both instructions, instead of using one script for each..

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

Exchange LWin and LAlt

This is my code.
#InputLevel 1
LAlt::LWin
LWin::LAlt
#InputLevel 2
!a::
#a::
tooltip You pressed %A_ThisHotkey%.
return
From my understanding, InputLevel 2 is executed first, then InputLevel 1, which means if I press LAlt key and a key, screen will show I pressed !a, if I press LWin key and a key, screen will show I pressed #a.
However actually my screen shows nothing. My key is not captured.
Do I miss anything? I want to capture hotkey first then do remapping.
When you press alt + a the LAlt::LWin hotkey is being fired first and the !a:: hotkey is being ignored. If you want to fire the !a:: regardless of the first hotkey fired you must add ~ on the LAlt::LWin hotkey so its native function (necessary to perform alt+a) isn't going to be blocked.
It may be easier to understand with this example:
#InputLevel 1
~LAlt:: tooltip First pressed %A_ThisHotkey%.
~LWin:: tooltip First pressed %A_ThisHotkey%.
#InputLevel 2
!a::
#a::
tooltip Then pressed %A_ThisHotkey%.
return
ps: if you also want to ignore extra modifiers that are (maybe) being held down you can add the wildcard * on the !a:: and #a:: hotkeys. So, in the end, the hotkeys would look like this: ~LAlt::LWin ~LWin::LAlt *!a:: and *#a::

Mapping Capslock to Esc and Capslock & C to a function in Autohotkey

I want to configure autohotkey in the following way:
Capslock::Esc
Capslock & C::
Run, www.stackoverflow.com
return
So if I just press Capslock it treated like if I would have pressed Esc. If I on the other hand press both Capslock and c, it call the function that opens the browser with www.stackoverflow.com.
At the moment the remapped seems to break when I have the other function in the script. When I press capslock now it toggles it for a short time, so the key alone does effectively nothing. I don't get my Esc.
Pressing capslocks + A on the other hand activates capslock and produces a real A.
Is there an easy way to fix this?
Check out this code:
inProcess = 0
Capslock::
Gui, 93:+Owner ; prevent display of taskbar button
Gui, 93:Show, y-99999 NA, Enable nav-hotkeys
inProcess = 1
KeyWait, Capslock ; wait until the Capslock button is released
Gui, 93:Cancel
if (inProcess == 1){
Send, {Esc}
}
Return
#IfWinExist, Enable nav-hotkeys
*c::
Run, www.stackoverflow.com
inProcess = 0
return
#IfWinExist, ; end context-sensitive block
I've modified an answer available here: http://www.autohotkey.com/board/topic/56428-problem-rebinding-ctrl-to-capslock-using/

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!