Making two AutoHotkey key remapping scripts work together correctly - autohotkey

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..

Related

How to only trigger hotkey combo once?

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

Autohotkey: binding win key

I've got a keyboard without the Win key (a legendary Model M!), so I want to bind it to ctrl + esc.
I tried those things, but that doesn't work -_-
LCtrl & Escape::LWin
or
LCtrl & Escape::
Send {LWin}
return
If I simply do:
Escape::LWin
or
Escape::
Send {LWin}
return
It's OK...
I also got another function that works OK like this:
^!F2::Send {Volume_Up 100}
So right now, can't figure what's wrong... Probably the misuse with the "&"?
Any idea?
Thank you!
After answer 1: I found this solution if I want to use the win key for combination (Win + e, Win + d, etc.):
^Esc::
KeyWait Ctrl ;wait until Ctrl is up
Send {LWin Down} ;send left Windows key down
sleep, 500
Send {LWin Up} ;send left Windows key up
Return
That way, when I depress ctrl, I've got 500ms to type the 2nd key.
The problem is that you if you press Ctrl+Esc, then
LWin is sent by AHK, but you are still holding down Ctrl so the result of this 'cooperation' is Ctrl+LWin which is ignored by Windows.
Solution:
^Esc::
KeyWait Ctrl ;wait until Ctrl is up
Send {LWin} ;send left Windows key
Return

Conditionally intercept a mouse click in Autohotkey?

I want to have a script which will intercept a mouse click and send a key press instead, but only when the capslock key is toggled on. I want the mouse click to be sent normally if the capslock key is toggled off.
Currently I have made this:
$LButton::
if GetKeyState("CapsLock", "T") = 1
send, {a}
else
send, {LButton}
return
The problem with this is that when the capslock key is off, the left button can click perfectly normally but it cannot drag.
If I change $ to ~, it is able to drag but it also performs a click when the capslock key is toggled on.
Is there any way to make the script ignore the click completely if the capslock key is toggled off?
AHK_L's #If will give you what you want:
#If GetKeyState("CapsLock", "T")
LButton::Send, a
With this code, you won't have to bother what happens when capslock is off. AHK will intercept the click on a lower level and let it trickle through.
How to use the symbol UP.
SetBatchLines, -1 ; you pretty much have to include this to speed up the execution
LButton::
if( GetKeyState("CapsLock", "T") )
tooltip, ignore left click
else
send, {LButton Down}
return
LButton UP::
send, {LButton Up}
return

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!