CapsLock (trigger/hot) key "stuck"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

CapsLock (trigger/hot) key "stuck"

Post by downstairs » 09 Oct 2020, 15:21

I use the caps lock as my "trigger" key for a host of hotkey combinations to do all sorts of stuff. Probably a common setup, and this has worked flawlessly for a few years. Recently, though, the caps lock key seems to get into a "stuck" situation (and I don't think it is hardware, as I've swapped out 3 keyboards now- all the same brand).

When the caps lock is "stuck"... normal typing triggers all of my various AHK macros- a nightmare! It does not get stuck at predictable times, and not always. But often.

My main .AHK file is massive and has some personal information, so I can't really paste it all here. I'll paste what I think is the relevant parts.

Note... hitting "caps lock" and sometimes other keys seems to reset everything.

Also note... I've tried all forms of:

SendInput, {CapsLock}
SetCapsLockState, AlwaysOff

In a loop, after keys are hit... nothing seems to work.

Code: Select all

#NoEnv
#Persistent
#InstallKeybdHook
#UseHook
SendMode Input
SetCapsLockState, AlwaysOff

F1:: Return
Insert:: Send %A_Tab%

#If GetKeyState("Capslock", "P")

4::SendInput, TEXTHERE
5::SendInput, TEXTHERE
NumpadEnter::Run calc.exe

S & U::
SendInput, {enter}MY EMAIL SIGNATURE HERE{enter}ON MULTIPLE LINES
Exit

T::
FormatTime, Time,, M/d/yyyy h:mm tt
SendInput, %Time%
Exit

F5::
FormatTime, TimestampS,, yyyy-MM-dd
SendInput, %TimestampS%
Exit

F6::
FormatTime, Timestamp,, yyyy-MM-dd-HH-mm
SendInput, %Timestamp%
Exit

; ------------------------------------------------------------------------------------------------------------------------

#If MouseIsOver("ahk_class Shell_TrayWnd")
WheelUp::Send {Volume_Up}
WheelDown::Send {Volume_Down}

~LButton::
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1){
	Send #m
	;Send {f5}
}
Return


; --------------------------------------------------------------------------------------------------------------------------

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}


GenerateRandomString(length = 8) {

characters := "abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ0123456789@$%&*()_;:?<>-"
StringSplit, chars, characters
uppercases := "ABCDEFGHIJKLMNOPQURSTUVWXYZ"
StringSplit, ucases, uppercases
lowercases := "abcdefghijklmnopqurstuvwxyz"
StringSplit, lcases, lowercases
numbers := "0123456789"
StringSplit, nums, numbers
specials := "@$%&*()_;:?<>-"
StringSplit, specs, specials

Loop, %length%
{
	Random, rand, 1, 78
	password .= chars%rand%
}
Random, rand, 1, 10
password .= nums%rand%
Random, rand, 1, 26
password .= lcases%rand%
Random, rand, 1, 14
password .= specs%rand%
Random, rand, 1, 26
password .= ucases%rand%
return password
}


; ------------------------------------------------------------------------------------------------------------------------

downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 09 Oct 2020, 15:22

Note... I've also messed with these, not having them, having only some of them... nothing works... but I brute forced added all these for the example, at the top of the script:

#NoEnv
#Persistent
#InstallKeybdHook
#UseHook
SendMode Input
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 09 Oct 2020, 18:39

It's hard to say without seeing more, but a few things might be worth noting.

1. #If stays in effect until the next such statement. I tend to bracket my conditional blocks with a trailing #If to avoid any confusion.

2. Exit is a bit unusual at the end of a routine. Return usually fits the need. Exit exits from the entire thread rather than just the routine. If you're not sure that you want to do that, then it might be causing unexpected behavior.

3. AHK has been known to "lead" to stuck modifier keys, presumably due to conflicts with other running software. I don't know whether that might happen with CapsLock.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 10 Oct 2020, 10:17

Ok... I was able to replicate this sometimes, not always. And it does't JUST do it when I do the following... but I am reliably able to often get the CapsLock stuck when I do the following:

1. Do something like edit my AHK file

2. Close all/most windows

3. Open Notepad

4. Run my CapsLock-S-R key combo to send-keys my signature into notepad to test.

5. I then hit various other keys and CapsLock (my trigger key) is now stuck, therefore it sends the various items triggered by my AHK script rather than just the keys I'm pressing. (Shortly: the CapsLock is now stuck)

6. If I hit the CapsLock key just once, all is good... but you can see what a problem I have here.

NOTE... I think I've tried everything- even in the CapsLock-S-R part of the script... to un-stick CapsLock ({CapsLock Up}, etc...)... nothing seems to work. But I am sure I have not tried everything possible
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 10 Oct 2020, 10:23

mikeyww wrote:
09 Oct 2020, 18:39
It's hard to say without seeing more, but a few things might be worth noting.

1. #If stays in effect until the next such statement. I tend to bracket my conditional blocks with a trailing #If to avoid any confusion.

2. Exit is a bit unusual at the end of a routine. Return usually fits the need. Exit exits from the entire thread rather than just the routine. If you're not sure that you want to do that, then it might be causing unexpected behavior.

3. AHK has been known to "lead" to stuck modifier keys, presumably due to conflicts with other running software. I don't know whether that might happen with CapsLock.
Thanks for the ideas. I've replaced all "Exit" with "Return", it doesn't fix the issue. It doesn't hurt, so I'll keep it as per your suggestion anyway.

I don't quite know where to put the trailing #If alone... but I tried a few places and it doesn't seem to help.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 10 Oct 2020, 11:02

OK. Short of seeing your script: you could always try removing parts of your main script until the problem disappears. You will then have some clues. It's sort of a brute-force, last-resort debug method! Nonetheless, once you isolate the problem, you can fix it. Although this "mechanical" method is somewhat brain-free in terms of poring over code, it can actually isolate many kinds of problems quickly.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 12 Oct 2020, 18:25

I guess one thing I'm not understanding is... as a hack, just for now... I'd love a way to try to trigger this "stuck" caps lock off.

But I've tried putting:

SendInput, {CapsLock}

SendInput, {CapsLock Up}

SetCapsLockState, AlwaysOff

...together or alone in a loop... just to hack at this thing. Yes, I'd love to solve the problem and have a perfect AHK file... but I'm not against a hack ;)
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 12 Oct 2020, 18:28

I actually have an emergency "reset" function that I assign to a Windows keyboard shortcut!

Code: Select all

resetKB() {
 BlockInput, Off
 SendEvent {Shift Up}{Ctrl Up}{Alt Up}
}
It's great for those modifiers when they get stuck down. Nonetheless, I'm not sure how to incorporate your fix for the CapsLock, because I don't use CapsLock as a modifier. Someone else may know.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 12 Oct 2020, 18:38

mikeyww wrote:
12 Oct 2020, 18:28
I actually have an emergency "reset" function that I assign to a Windows keyboard shortcut!

Code: Select all

resetKB() {
 BlockInput, Off
 SendEvent {Shift Up}{Ctrl Up}{Alt Up}
}
It's great for those modifiers when they get stuck down. Nonetheless, I'm not sure how to incorporate your fix for the CapsLock, because I don't use CapsLock as a modifier. Someone else may know.
I tried to put the following in a loop (just for testing)... as it's important to note... I can't seem to replicate my CapsLock sticking 100% of the time... so it takes some time to see if the problem still exists upon any fix attempt.

But, I put this in a loop... and for some reason it disables the shift and control keys? And I'm not even specifying them, I removed them from your code and put in CapsLock:

Code: Select all

BlockInput, Off
SendEvent {CapsLock Up}
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 12 Oct 2020, 18:45

I don't think you'll need the BlockInput.

Some possible causes of the other problem:

1. Other code in your script is doing it, not this

2. Other code somewhere is intercepting and then taking its own action

3. Faulty keyboard

Note that it is possible in Windows Region settings to alter how SHIFT and CAPSLOCK work with each other. I actually have mine set to turn off CAPSLOCK when I press SHIFT. That's how old typewriters worked, because it saved one more keypress when you wanted to type a word with a capitalized initial letter.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 13 Oct 2020, 09:50

Ok... while I'd really love to solve this... I've gone through three keyboards (all the same make model):

Dell Smartcard Keyboard (KB813-BK-US) Smartcard Reader, Wired USB, Black, US Key Layout

This keyboard had, for years, been perfect... as it is one of those rare keyboards that can handle almost any three-key combo (as you probably know, many keyboards don't)

So... I'm willing to buy a new keyboard, because now I feel this is a keyboard issue (I am noticing OTHER keys in my hotkey combos being stuck!)

I'm surprised this may be a keyboard issue, but does anyone have a recommendation for a good keyboard that can handle any set of three-key combos and accept all the keys?

I'm getting so frustrated trying to rip apart what I think is a simple master AHK file to figure out what is sticking keys... code that has worked for months if not years.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 13 Oct 2020, 10:02

Doesn't sound like a keyboard issue if you already tried a few.

Can we see more of the script that is causing the problem for you?

Note that if you are running a tight loop that is sending keys and text with no delays, it's quite possible that the loop will prevent other things from happening properly. That is by design. If you want something else to occur during a loop, you have to adjust the code accordingly.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 13 Oct 2020, 10:32

mikeyww wrote:
13 Oct 2020, 10:02
Doesn't sound like a keyboard issue if you already tried a few.

Can we see more of the script that is causing the problem for you?

Note that if you are running a tight loop that is sending keys and text with no delays, it's quite possible that the loop will prevent other things from happening properly. That is by design. If you want something else to occur during a loop, you have to adjust the code accordingly.
Ok, here is the full script... I run it as an AHK directly from Windows Startup - I don't compile it to EXE. I've removed personal information, but replaced it with generic text so everything is as complete as possible.
Last edited by downstairs on 23 Dec 2020, 12:47, edited 1 time in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 13 Oct 2020, 10:44

Thanks-- helpful.

Although I did not read every line, I did notice that you have a very large #If block-- almost the entire script-- for the CapsLock key state. You then have many hotkeys under it that contain prefix keys (double-key hotkeys). Is this your intent, such that you would actually need to hold three keys? I do note that AHK does not actually support key combinations that exceed two, so you might be bound for trouble here.

Using prefix keys is fairly tricky, because the prefix key is subsequently disabled by design. This means that if you want to be able to press the prefix key independently of your prefixed hotkey, you need to add an additional capability (separate hotkey for the prefix key alone). This is explained in the documentation. If your prefix key is a modifier key such as SHIFT or ALT, that also applies: those keys will be disabled unless you handle them separately.

With such a large number of key combinations as you have, just putting the commands into a ListView might actually be more usable and reliable for you. You could then show the ListView and just select what you want. An alternative is that when a letter like "F" is pressed, it activates an InputBox like a launcher, and you can then type your own sort of "command" that your script will recognize. Just ideas here.
Last edited by mikeyww on 13 Oct 2020, 10:53, edited 1 time in total.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 13 Oct 2020, 10:51

mikeyww wrote:
13 Oct 2020, 10:44
Thanks-- helpful.

Although I did not read every line, I did notice that you have a very large #If block-- almost the entire script-- for the CapsLock key state. You then have many hotkeys under it that contain prefix keys (double-key hotkeys). Is this your intent, such that you would actually need to hold three keys? I do note that AHK does not actually support key combinations that exceed two, so you might be bound for trouble here.

Using prefix keys is fairly tricky, because the prefix key is subsequently disabled by design. This means that if you want to be able to press the prefix key independently of your prefixed hotkey, you need to add an additional capability (separate hotkey for the prefix key alone). This is explained in the documentation.
I didn't know three keys were an issue... I've used three-key combos forever in this way, and I've actually seen videos on some keyboards not handling three keys at a time (other than obvious stuff like Ctrl-Alt-Delete)... and how some keyboards (like the one I have) being great for that.

I'm basically using CapsLock as the trigger key for a bunch of three-key combos... and AHK has been working... except until I recently got random stuck keys.

I've grown very used to this working fine... it's a perfect way to use AHK in my mind... I have tons of things I want to automate, which needs way more combos than a two-key combo system could handle.

So..... I'm not sure! I appreciate your help though.

I didn't quite invent doing things this way... I started from a borrowed script back when I knew nothing about AHK.

EDIT:

Note, I do not ever intend to use CapsLock for it's intended purpose. I don't need it. Hence, that is my trigger key
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 13 Oct 2020, 10:55

If you attempt to have three keys as a trigger, you will get only sporadic and probably random success. Avoid it! Change it!

Your use of prefix keys is also causing problems for you because those keys alone are automatically disabled, even if the prefix keys are modifier keys such as SHIFT or ALT. The documentation shows examples of how to fix that situation, in case you decide that you have to have the prefix keys (but only with one additional key, not two additional keys!).
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 13 Oct 2020, 10:59

mikeyww wrote:
13 Oct 2020, 10:55
If you attempt to have three keys as a trigger, you will get only sporadic and probably random success. Avoid it! Change it!

Your use of prefix keys is also causing problems for you because those keys alone are automatically disabled, even if the prefix keys are modifier keys such as SHIFT or ALT.
I'm curious about this part:
Your use of prefix keys is also causing problems for you because those keys alone are automatically disabled.
Is that something I should fix in my code? If so, how? Is there a better way to use CapsLock as a trigger while keeping it disabled?

Note... this code has worked flawlessly, and never sporadically or randomly.... save for this stuck key issue.

I never click any three-key combo and have it not fire... it works :)
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 13 Oct 2020, 11:07

Well, it certainly sounds like you were interested in fixing your script so that your prefix keys work....

It's possible that by having one of the three trigger keys in your #If block, you get some reliability, but I can't say I'd recommend this! You could use CapsLock as a regular prefix key in combination with one other key.

See (with examples):

https://www.autohotkey.com/docs/Hotkeys.htm
The prefix key loses its native function: In the above example, Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its original/native function when it is pressed by itself. To avoid this, a script may configure Numpad0 to perform a new action such as one of the following....
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: CapsLock (trigger/hot) key "stuck"

Post by downstairs » 13 Oct 2020, 11:56

mikeyww wrote:
13 Oct 2020, 11:07
Well, it certainly sounds like you were interested in fixing your script so that your prefix keys work....

It's possible that by having one of the three trigger keys in your #If block, you get some reliability, but I can't say I'd recommend this! You could use CapsLock as a regular prefix key in combination with one other key.

See (with examples):

https://www.autohotkey.com/docs/Hotkeys.htm
The prefix key loses its native function: In the above example, Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its original/native function when it is pressed by itself. To avoid this, a script may configure Numpad0 to perform a new action such as one of the following....
Yeah, they seem to be saying that while three-key combos aren't supported in terms of, say, this example:

Code: Select all

Control & Shift & Y::
You can achieve them just fine with

Code: Select all

#If GetKeyState("Capslock", "P")
S & U::
If your keyboard supports it, which good keyboards will (not all!)

I can't imagine using AHK without three keys.... if I used 2 key combos, I'd run out of keys? For example, my signatures are the CapsLock through #If GetKeyState("Capslock", "P") and:

S & U::
S & D::
S & P::

And my phone numbers are the same CapsLock trigger and:

P & 1::
P & Q::
P & U::

Then on and on through a ton of other automations....
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: CapsLock (trigger/hot) key "stuck"

Post by mikeyww » 13 Oct 2020, 12:45

Why not use hotstrings? You can have as many as you want. Easier to remember, too. You just type in a few letters of regular text, and AHK will convert them into whatever you want. I have hundreds of them. You could even use the same letters as your current hotkeys, like typing "su" will do whatever S & U are supposed to do. Easy. I also have a AHK launcher script-- you are welcome to it-- that loads my LNK file names and lets me run any of them from a simple ListView GUI. I can even just type one or two keys to jump to the files starting with those letters.

Example of hotstring that executes a program (uses my path for Photoshop):

Code: Select all

:X:rp::Run, C:\Program Files\Adobe\Adobe Photoshop 2020\Photoshop.exe
Of course, you have to be typing into a window to make this work.

Another example:

Code: Select all

:O:qe::expedia{ENTER}
Type qe into your favorite place.
Post Reply

Return to “Ask for Help (v1)”