AHK script: pressing CapsLock to toggle CapsLock + Suspend - toggle

Setting up an Autohotkey script.
How to make CapsLock key do 'Suspend' while toggling the CapsLock state on the same key press?
I want this:
CapsLock::
Suspend
ToggleCapslock()
Return
The code should be able to make CapsLock key toggle both the CapsState and the Suspend state with a single key press of CapsLock.
How to achieve that?
The script below doesn't toggle both the CapsLock state and the Suspend state.
1st key press: it activates Suspend and sets CapsLockState to OFF.
2nd key press: it does unsuspend, CapsLockState remains at OFF.
CapsLock::
Suspend
;ToggleCapslock()
if GetKeyState("CapsLock", "P")
SetCapsLockState, Off
if !GetKeyState("CapsLock", "P")
SetCapsLockState, On
return
ToggleCapslock() {
flag := false
if (flag) {
SetCapsLockState, On
} else {
SetCapsLockState, Off
}
flag := !flag
}
I want to toggle Suspend/Unsuspend AND CapsLockState ON/OFF on each single press of CapsLock.
(Essentially, this thread asks how to put actions to CapsLock key while maintaining its native function.)
Glad for your help.

Final easy Solution:
~CapsLock::Suspend
The documentation covers the most fundamental solution for this problem you could possible come up with. (Found by reading through the documentation: https://www.autohotkey.com/docs/Hotkeys.htm#Symbols)
~ : When the hotkey fires, its key's native function will not be blocked (hidden from the system).
< closeThread >

Do you mean the Pause key? Or suspend as in suspending the hotkey macro?
Try:
CapsLock::
Send {Pause}
ToggleCapslock()
Return
There is good help here on Pause:
https://www.autohotkey.com/docs/KeyList.htm#other
Pause Pause or Ctrl+NumLock. While Ctrl is held down, Pause produces the key code of CtrlBreak and NumLock produces Pause, so use ^CtrlBreak in hotkeys instead of ^Pause.
Suspending the hotkeys is here:
https://www.autohotkey.com/docs/commands/Suspend.htm
Suspend Disables or enables all or selected hotkeys and hotstrings.
Let us know what you want,

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

AHK script designed to toggle based on CapsLock Status always toggles on 'off' and I cannot change it to 'on'

(Note: Very new to scripting, borrowed some phrases from other scripts I've online.) I have Carpal Tunnel and play a video game that does not have any key-bind options not set to the F1-F0 keys so I want to rebind the F1-F4 keys to Z-V but only when capslock is enabled, to prevent being unable to type in chat windows and in other programs without closing the script. However, the script re-binds the basic keys to the f-keys ONLY when capslock is OFF, rather than allowing me to change it to ON. Not sure why.
I've tried 'hotfixing' it by rebinding it to Numlock, but when I moved to CapsLock changing 'OFF' to 'ON' did not keep the script from only rebinding the keys while CapsLock was OFF. Not sure why.
$Z::
GetKeyState, state, NumLock, T
if state = D ; NumLock is toggled ON
send, {z}
else
send, {F1}
Return
$X::
GetKeyState, state, NumLock, T
if state = D ; NumLock is toggled ON
send, {x}
else
send, {F2}
Return
etc...
etc...
I expected changing the value 'OFF' to 'ON' would result in the key rebinds only happening during the CapsLock status being toggled on.
Have you checked if you restarted the script after making changes? It's a very common mistake, not only among beginners. According to your example, your keys Z..V should behave as F1..F4 only when the NumLock is toggled off.
Given the nature of your script, you can consider to add the directive #SingleInstance Force which will automatically replace any older instance of your script by a new one each time you run the script again, making testing easier.
You can do conditional binding much easily with an #if directive, which makes the subsequent hotkeys and hotstring only effective when a condition is met.
To check the state of the CapsLock or NumLock keys you can also use the built-in function GetKeyState, which for toggle keys, such as CapsLock or NumLock, with the "T" mode returns either True or False based on the toggle state of the key.
Also, if you want to remap keys, you can simply write the target key's name at the right of the hotkey, which will completely bind the keys, on both Down and Up events. However, for this to work, you must specify your triggering keys as lowercase, since specifying uppercase would only trigger the remap when pressed the keys with the Shift key as well (the CapsLock would have no effect), and that is not your desired behaviour. [More on remapping keys]
Here is an example of what you could do:
#If GetKeyState("CapsLock", "T")
z::F1
x::F2
c::F3
v::F4
Note that, since key remapping always uses the keyboard hook (because it needs to register the Up events as well), there is no need to use the $ prefix in your hotkeys at all.
Nonetheless, you can automate your script even more if you use as condition for your hotkeys the currently active window and bind them to your game using the #IfWinActive directive.
However if there are also chats in the game, you might want to combine both conditions in a single #If, using the built-in function WinActive like this:
#If GetKeyState("CapsLock", "T") and WinActive("My Game Title")
z::F1
; ...
You can check how to narrow your search for the window by its title on the documentation for the WinTitle parameter.
If you want to improve your script even further, you could explore if there is any detectable change on the game window when the chat is active, such as if a certain control exists (you can check that as if it was another window using the WinExist function to check for a certain Window class.
To seek for such changes, you can use a script as the following (from the MouseGetPos documentation):
#Persistent
SetTimer, WatchCursor, 100
return
WatchCursor:
MouseGetPos, , , id, control
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
ToolTip, ahk_id %id%`nahk_class %class%`n%title%`nControl: %control%
return
Which would allow you to see the window information of the windows below your mouse. You can use it to check for the name or class of the chat control by placing your mouse over it.
However, keep in mind that many games do not use Windows controls at all in their interfaces and rather just draw them on screen by themselves, so if you're trying this and can't progress much after a while you shouldn't waste too much time on it and rather enjoy playing with your CapsLock toggled binding.
Another tricky way to check if the chat is active is searching for an image on the screen or a pixel color using ImageSearch or much simpler PixelGetColor, but you can only do that if your game's interface is not very complex/animated.

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