Mapping Capslock to Esc and Capslock & C to a function in Autohotkey - 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/

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

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

Make CAPSLOCK work properly when single-, double-, and combo-pressed

I want to write an AutoHotKey script on Windows 10 that gives CAPSLOCK more functionality. My goals with CAPSLOCK are three-fold:
When single-pressed, CAPSLOCK works as usual.
When double-pressed (pressed twice in a short time), CAPSLOCK fires an ESC key. The status/light of CAPSLOCK should remain the same as before, but I am okay if the light went on and off, or off and on.
When CAPSLOCK are held down, in combination with JKLI, CAPSLOCK + JKLI will function as arrow keys (left, down, right, up). Like in goal 2, the status/light of CAPSLOCK should remain the same as before. (I am okay if the light went on and off in the process, so long the terminal status is correct).
If I only needed goal #1 and goal #3, the following script would work just fine.
CapsLock & J::Send {Left}
CapsLock & K::Send {Down}
CapsLock & L::Send {Right}
CapsLock & I::Send {Up}
However, now I want to achieve goal #2 as well, and added some more lines before it, as follows
~CapsLock::
KeyWait, CapsLock
KeyWait, CapsLock, D T0.2
if not ErrorLevel
Send {Escape}
Return
CapsLock & J::Send {Left}
CapsLock & K::Send {Down}
CapsLock & L::Send {Right}
CapsLock & I::Send {Up}
Now I am having the problem: goal #1 and goal #2 are achieved, but goal #3 is not. The status/light of CAPSLOCK would change after say I pressed CAPSLOCK + L. This is not what I want -- I want holding down CAPSLOCK and pressing L to move cursor to the right, and I want this behavior to have no effect on the status of CAPSLOCK.
Please let me know how to achieve my three goals with CAPSLOCK using AutoHotKey. Any help is much appreciated!
By the way, I am working on a Lenovo Thinkpad T model produced in 2016.
You need a timer to restore the CapsLock state after it has been changed in a combination:
Capslock::
If (A_PriorHotKey = "~Capslock Up" AND A_TimeSincePriorHotkey < 400 AND A_TimeSincePriorHotkey > 50) ; double-press
Send, {Esc}
SetTimer, RestoreCapslockState, 50
return
~Capslock Up:: return ; The tilde prefix (~) prevents AHK from blocking the key-down/up events
CapsLock & J::Send {Left}
CapsLock & K::Send {Down}
CapsLock & L::Send {Right}
CapsLock & I::Send {Up}
RestoreCapslockState:
KeyWait, Capslock ; wait for Capslock to be released
SetTimer, RestoreCapslockState, OFF
If (A_PriorKey != "Capslock")
SetCapsLockState % !GetKeyState("CapsLock", "T") ; Toggles CapsLock to its opposite state, requires [v1.1.30+]
return
https://www.autohotkey.com/docs/commands/SetTimer.htm
https://www.autohotkey.com/docs/commands/SetNumScrollCapsLockState.htm#ex2

How to send a key repeatedly in Autohotkey

I want to write an AutoHotkey script which loop a key X number of times.
For example, here's is a script which overwrites the function of ENTER key with function of F2 key in File Explorer.
#IfWinActive ahk_class CabinetWClass
Enter::
Send, {F2}
#IfWinActive ahk_class CabinetWClass
Enter::
Send, {ENTER}
#IfWinActive
The goal is to press ENTER to rename a select file, and then press ENTER to confirm the rename. Pressing ENTER on the same file that have just been renamed should send F2 key again (In case there is typo error).
Currently the second block doesn't work as I'm sending the same key, how to fix this?
The KeyWait command is your friend in this case.
There is still room to improve on how you handle the second Enter
#IfWinActive ahk_class CabinetWClass
$Enter::
sleep,100 ; giving time to detect the first Enter
Send, {F2}
Keywait, Enter, T5 D ; wait 5 seconds for the Enter to be pressed down
If (ErrorLevel == 0)
{
Send, {Enter}
sleep 200
Send, {F2}
}
else
{
traytip, , timeout ; Enter was not pressed down in 5 seconds
}
return
Basically, it appears you're trying to assign different tasks to the same hotkey and due to this being done seperately ahk is selecting one of the tasks and running with that task and only that task. If loops can be used within hotkeys, so I would suggest using this to rotate between the two expected outcomes. Please see example below:
temp:= 1
enter::
if(temp==1)
{
Send, {ENTER}
temp:=2
}
else if(temp==2)
{
Send, {F2}
temp:=1
}
return
1::
Temp:=1
return
2::
temp:=2
return
^x::ExitApp
I also added in hotkeys for 1/2 to allow you to manually decide the outcome rather than it being specifically assigned in the case of any issues.
Oh, and ctrl+x to close the macro.
You're trying to rebind the enter key twice.
Rebinding a key is like saying "When I press this key, do this:" - in this case it's under an #IfWinActive so it's more like "When this window is open and I press this key..."
When you break that down you have "When I press enter - press F2" as well as "When I press enter, press enter"
What you're wanting to achieve is make the rebind conditional - i.e. it only sends F2 under certain conditions.
It's hard to know how to help without more context. Is there any reason you can't use a different key combination? Like Ctrl + Shift + Enter?
Something like:
+^Enter::send, {F2}

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