AutoHotkey (.ahk). capslock activated - autohotkey

I am using .ahk for some days now and it is cool to work with.
I try to say that if CapsLock is activated my code runs / or not.
This code below checks if CapsLock is pressed down in this moment (working).
GetKeyState, CapLck, CapsLock, P
if ( CapLck == "D" ) {
some statement
}
I would like to change that if I click CapsLock once its activated - when I press it again it deactivates.
My research found out, that the parameter P in GetKeyState, CapLck, CapsLock, P checks if CapsLock is physically clicked down at this moment. If this is true it gives back the D which is used to check in the if-statement below.
So I tried changing the code from
GetKeyState, CapLck, CapsLock, P
if ( CapLck == "D" ) {
some statement
}
to this
GetKeyState, CapLck, CapsLock, T
if ( CapLck == 1 ) {
some statement
}
If I read the GetKeyState documentation right the parameter T checks if CapsLock is activated or not. If activated it gives back 1 for true; 0 for false.
Sadly my changes do not apply and it does not work. Any ideas?
Thanks.

Try this:
#If GetKeyState("CapsLock", "T") ; If Capslock is On
a::MsgBox % "CAPSLOCK IS ON"
#If
a::MsgBox % "CAPSLOCK IS OFF"

In order to check if CapsLock is activated or not simply change
GetKeyState, CapLck, CapsLock, P
if ( CapLck == "D" ) {
some statement
}
to
GetKeyState, CapLck, CapsLock, T
if ( CapLck == "D" ) {
some statement
}
In contrast to the AutoHotKey documentation the parameter T gives back "D" for true not 1.

Related

How do you include a loop with modifiers?

I'm trying to get AHK to continue to press "2" until "2" is pressed a second time. If alt, ctrl, or shift is held it sends ^2, +2, !2 while held and then returns to spamming "2" once the modifier key is released.
This code works so far with modifiers I just need to figure out how to add the loop.
; Disable Alt+Tab
!Tab::Return
; Disable Windows Key + Tab
#Tab::Return
#ifWinActive World of Warcraft
{
$2::
$^2::
$+2::
$!2::
Loop
{
if not GetKeyState("2", "P")
break
if GetKeyState("LCtrl", "P")
Send ^2
else if GetKeyState("LShift", "P")
Send +2
else if GetKeyState("LAlt", "P")
Send !2
else
Send 2
sleep 135
}
return
}
I would recommend using SetTimer for your loop and to be able to toggle it on and off. Please see if the following works for you:
$2::
$<^2::
$<+2::
$<!2::
SetTimer , label_TwoLoop , % ( bT := !bT ) ? "135" : "Off"
Return
label_TwoLoop:
If GetKeyState( "LCtrl" , "P" )
Send , ^2
Else If GetKeyState( "LShift" , "P" )
Send , +2
Else If GetKeyState( "LAlt" , "P" )
Send , !2
Else
Send , 2
Return
https://autohotkey.com/docs/commands/SetTimer.htm
Note that I added the < to the hotkey definitions since the loop-portion is only looking for the left modifier keys. I figured this is the intended behavior.

Layer based Keyboard using AutoHotKey: Change modifiers single press, hold, and double press behavior

folks,
I want to create a layer based keyboard using AutoHotkey. Basicly, I want to achieve what shift already does: modify each key when a modifier is used.
I want to improve regular shift in the following:
press modifier once: only change layer for next character
hold modifier: change layer as long as modifier is down
press modifier twice: enter layer mode, like capslock. (end by another press)
Modifiers: LAlt, RAlt, LControl, RControl (CapsLock, Shift)
How cas I accomplish this?
what I found so far on stackoverflow:
This code allows for shift to be pressed and released for the next character
$*LShift::
SendInput, {LShift Down} ; press shift
Input, Key, L1 M V ; wait for input character
If GetKeyState("LShift", "P") ; if shift still pressed, wait for release
KeyWait, LShift
SendInput, {LShift Up} ; send input with shift down, the shift up
Return
this code turns a double shift press into CapsLock
LShift::
KeyWait, CapsLock ; wait to be released
KeyWait, CapsLock, D T0.2 ; and pressed again within 0.2 seconds
if ErrorLevel
return
else if (A_PriorKey = "CapsLock")
SetCapsLockState, % GetKeyState("CapsLock","T") ? "Off" : "On"
return
#If, GetKeyState("CapsLock", "P") ; hotkeys go below
a::b
#If
But I am not experienced enough with AHK to bring this together. My goal is to have something like
Modifier::
; code that makes the modifier behave like expected: single press, hold, double press
Return
#If, GetKeyState("Modifier", "P") ; List of key remaps in specific layer
#If
I hope this is specific enough and that you can help me out here.
thanks!
Assign the corresponding Booleam values (true or false) to the variables "Double_LAlt" and "Double_LAlt_holding" in order to create context-sensitive hotkeys depended on their values:
LAlt::
ToolTip,,,, 3
ToolTip,,,, 4
Double_LAlt := false
; Press twice or press twice and hold LAlt within 0,2 seconds
If (A_PriorHotKey = "~LAlt Up" AND A_TimeSincePriorHotkey < 200)
{
Sleep, 200
If GetKeyState("LAlt","P")
{
ToolTip,,,, 4
ToolTip, Double_LAlt_holding,,, 2
Double_LAlt_holding := true
}
else
{
ToolTip,,,, 4
ToolTip, Double_LAlt,,, 3
Double_LAlt := true
}
}
If !((Double_LAlt_holding) || (Double_LAlt)) ; "!" means "NOT" and "||" means "OR"
ToolTip, LAlt_holding,,, 1
return
~LAlt Up::
ToolTip,,,, 1
ToolTip,,,, 2
Double_LAlt_holding := false
Sleep, 100
If (A_TimeIdlePhysical > 100)
Tooltip, PriorHotKey = LAlt Up,,, 4
SetTimer, RemoveTooltip, 1000
return
#If (Double_LAlt_holding) ; If this variable has the value "true"
<!a:: MsgBox, a while Double_LAlt_holding ; "<!" means "LAlt"
<!1:: MsgBox, 1 while Double_LAlt_holding
#If (Double_LAlt)
a:: MsgBox, a after Double_LAlt
1:: MsgBox, 1 after Double_LAlt
; Press a key within 2 seconds after releasing LAlt:
#If (A_PriorHotKey = "~LAlt Up" AND A_TimeSincePriorHotkey < 2000)
a:: MsgBox, a after LAlt Up
1:: MsgBox, 1 after LAlt Up
#If GetKeyState("LAlt","P")
a:: MsgBox, a while LAlt_holding
1:: MsgBox, 1 while LAlt_holding
#If
RemoveTooltip:
If (A_TimeSincePriorHotkey > 2000) ; 2 seconds
ToolTip,,,, 4
return

Send only if key was pressed alone

I'd like to remap my windows key to something else, but also I'd like to keep all windows key based shortcut.
In pseudo code it would be something like this:
when LWin Down
until LWin Up
if not LWin down
abort
else
execute command
Release the left windows key within 0,3 seconds after pressing it, to do something else (e.g. to send a):
~LWin::
KeyWait, LWin
return
~LWin Up::
Send {LWin Up}
If (A_PriorHotKey = "~LWin" AND A_TimeSincePriorHotkey < 300)
Send, a
; else ; another action after long press (not recommendet)
; Send, b
return
EDIT:
Try also this:
LWin up::
If (A_PriorKey = "LWin")
Send a
return
; In this case its necessary to define a custom combination by using "&" or "<#"
; to avoid that LWin loses its original function as a modifier key:
<#d:: Send #d ; <# means LWin

`If WinActive` together with `GetKeyState`

Can anybody explain, why I get "Error" message box?
I think, the code is self-explanatible. I was "surprised" when I discovered it doesn't work for some reason.
(When you press F1 in opened Notepad window, you should see "It works!" message. Instead, I get an "Error" message).
I've tried different ways to fix it, i.e. percents, variables assignments, parentheses, but currently it still doesn't work.
#SingleInstance, Force
SetTitleMatchMode, 2
f1::
+f1::
GetKeyState, shift_state, Shift
msgbox, %shift_state%
if (WinActive("Notepad") and shift_state = D)
msgbox, It works! By the way, the Shift key is pressed.
else if (WinActive("Notepad") and shift_state = U)
msgbox, It works! The Shift key is not pressed.
else
msgbox, error
return
In expressional mode literal strings must be enclosed in double quotes to distinguish them from variables.
https://autohotkey.com/docs/Variables.htm#Expressions
expressional mode:
#SingleInstance, Force
SetTitleMatchMode, 2
f1::
+f1::
GetKeyState, shift_state, Shift
msgbox, %shift_state%
if (WinActive("Notepad") and shift_state = "D")
msgbox, It works! By the way, the Shift key is pressed.
else if (WinActive("Notepad") and shift_state = "U")
msgbox, It works! The Shift key is not pressed.
else
msgbox, error
return
traditional mode:
#SingleInstance, Force
SetTitleMatchMode, 2
f1::
+f1::
GetKeyState, shift_state, Shift
msgbox, %shift_state%
IfWinActive Notepad
{
If shift_state = D
msgbox, It works! By the way, the Shift key is pressed.
else If shift_state = U
msgbox, It works! The Shift key is not pressed.
}
else
msgbox, error
return

Shift Key Training Wheels and Shift Parenthesis Remap

I'm looking to use AutoHotKey to modify the functionality of my shift keys. The functionality is described in Steve Losh's Blog entry here. Specifically, I'd like my shift keys to do the following:
If LShift or RShift is pressed and released in under 300 ms with no other keys being pressed in between, send ( or ), respectively.
If LShift and RShift are "rolled" together (press LShift, press RShift, release LShift, release RShift, etc.) in under 300ms, send () or )(.
If a shift key is used improperly (LShift and S, RShift and K, etc.) then nothing happens.
I've been having issues with the 300ms requirement and the "rolling" functionality. Specifically, I'm having issues with only being able to detect when the key is released due to the hotkey combos such as:
LShift & 0:: return
This is where I'm at so far:
LShift::
Send {LShift Down}
KeyWait, LShift
Send {LShift Up}
if (A_TimeSinceThisHotkey < 300){
if (A_PriorKey = "LShift")
{
Send {)}
}
}
return
I don't see a reason to use a 300 ms timeout anyway, it seems unreliable and unnecessary.
Have a look at this commented code, it is short and efficient, and seems to meet all of your requirements:
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
/* Put your unwanted combinations here
* and let them do nothing
*/
LShift & q::return
RShift & y::return
Edit:
Since LShift and RShift already are prefix hotkeys, I left out the trick described here.
MCL's answer is close but when I tested it I found that shift-clicking didn't select text. Here's a version with a passthrough to allow shift-clicking to work.
;shift to parens
LShift::Send, (
RShift::Send, )
LShift & RShift:: Send, ()
RShift & LShift:: Send, )(
;passthrough for shift-click
LShift & LButton::
Send, {LShift Down}{LButton}
Send, {LShift Up}
RShift & LButton::
Send, {RShift Down}{LButton}
Send, {RShift Up}
I don't think the 300ms timeout is possible without either very deep understanding of autohotkey's implementation or actual modification to autohotkey. When I tried to get it to work (using http://www.autohotkey.com/board/topic/98742-remapping-shift-key/) I found that A_PriorHotkey was not consistently populated. I don't think that variable was meant to work with modifier keys this way.
I felt compelled to figure this one out. Here you go!
I basically created a hotkey for every Shift + Letter key combination in order to send the correct key case and also set the Abort value. The Abort value is then referenced whenever one of the Shift keys is pressed in order to determine whether or not to send the corresponding ( or ).
The "Rolling" was accomplished by creating a Hotkey for LShift + RShift (and the opposite). It then looks to see which key is released first to determine () or )(.
Accept if this was what you were looking for!
Loop 26
{
Hotkey, % "~LShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
Hotkey, % "~RShift & " Chr(A_Index+96), LetterKey ; Hotkeys for A-Z
}
Return
RShift::
LShift::
Abort := 0
keyDown := A_TickCount
Keywait, % A_ThisHotkey
duration := A_TickCount - keyDown
If (duration < 200) and (Abort = 0)
SendInput % (A_ThisHotkey = "LShift") ? "(" : ")"
Send {LShift Up}
Return
RShift & LShift::
LShift & RShift::
Keywait, LShift
If GetKeyState("RShift", "P")
{
Keywait, RShift
Send ()
}
Else
Send )(
Return
LetterKey:
SendInput, % "+" SubStr(A_ThisHotKey, 0, 1)
Abort := 1
Return
EDIT:
Hmm, I seem to be having the same problem as you. I always get a duration of 0 due to the hotkeys.
I found and modified this script on the AutoHotKey Forums. (The original script was prone to type "K(" when you meant to type "K" if you type too quickly, so I've modified it so that shouldn't happen any more)
$LShift Up::send, % getkeystate("LShift") ? "{LShift Up}" : ""
$RShift Up::send, % getkeystate("RShift") ? "{RShift Up}" : ""
~$LShift::
KeyWait, LShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "LShift")
{
send, % getkeystate("LShift") ? "{LShift Down}" : "("
}
KeyWait, LShift
return
~$RShift::
KeyWait, RShift, T0.1 ; wait 100ms to check shift state
if (A_PriorKey = "RShift")
{
send, % getkeystate("RShift") ? "{RShift Down}" : ")"
}
KeyWait, RShift
return