Key up event not firing

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ilovelife
Posts: 7
Joined: 23 Oct 2017, 23:40
Contact:

Key up event not firing

Post by ilovelife » 12 Jun 2021, 11:23

I have remapped capslock down event to trigger the ctrl down and it's up state to trigger the ctrl up.
When launching the task manager (which has admin privileges) via capsLock+shift+escape, ahk still thinkgs that capsLock is held down although I release it (can be seen in the loop code which uses get key state)

My question is how can I make AHK get the physical state of a key so that when I launch task manager and release the capslock key the tooltip will write capsPressed=0 (would be nice to also launch the keyup event but this can be done in the loop, and am aware that ctrl is only pressed on the non admin level)

I've tried #InstallKeybdHook, #UseHook and do not work.
I am not interested to launch my script with admin privileges.

Code: Select all

loop {
	ctrlState := getKeyState("ctrl")
	keyState := getKeyState("2", "P")

	display := ""
	display := display . "ctrlState=" . ctrlState . "`n"
	display := display . "keyPressed=" . keyState . "`n"
	tooltip % display
	sleep 500
	tooltip
}

*2::
	send {blind}{ctrl down}
return

*2 up::
	send {blind}{ctrl up}
return
Last edited by ilovelife on 13 Jun 2021, 14:00, edited 1 time in total.
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Key up event not firing

Post by mikeyww » 12 Jun 2021, 12:58

CapsLock uses mode "T".
ilovelife
Posts: 7
Joined: 23 Oct 2017, 23:40
Contact:

Re: Key up event not firing

Post by ilovelife » 13 Jun 2021, 13:59

Thanks for the reply. Unfortunately I chose a bad key example :(
Let's say I use the key `2` instead of capslock (I also edited the code snippit to reflect this). Do you know of a solution in this situation ?
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Key up event not firing

Post by mikeyww » 13 Jun 2021, 14:14

The following seemed to work.

Code: Select all

If !A_IsAdmin && !RegExMatch(DllCall("GetCommandLine", "str"), " /restart(?!\S)") {
 Try Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
 ExitApp
}
Loop {
 SoundBeep, 1500
 ToolTip, % "Ctrl=" GetKeyState("Ctrl") "`n2=" GetKeyState("2", "P")
 Sleep, 500
}
2::Ctrl
ilovelife
Posts: 7
Joined: 23 Oct 2017, 23:40
Contact:

Re: Key up event not firing

Post by ilovelife » 17 Jun 2021, 00:16

Thanks for the reply and sorry for the late reply, I only had now the time to look into the code and understand what it does: it restarts the script with admin privileges in case it is not already runned as admin.

The situation I am encountering is with a bigger script (5000+ lines) that captures all the hotkeys on the keyboard (a-z, 0-9, F1-F12, ctrl, alt, shift winKey, enter, insert etc). I capture all of them in the approach written in the first code. Spontaneously a key is triggered as down (let's say the ctrl key) and when I release it the fire up event is not triggered (and ctrl remains pressed).

I tried to execute a 500ms timer which checks specifically for the ctrl,shift,alt,win keys using GetKeyState("lctrl", "P") [or even GetKeyState("2", "P") - because 2 is remapped as ctrl ] but the strange thing is that when the up event is missed the GetKeyState "P" still returns true.
I tried running the script as administrator but still the same result of GetKeyState "P" still being 1 spontaneously happens when the up event is missed.

I don't know if this has any influence - when compiling the script with Ahk2exe I see that this issue happens, I have done for a few days the experiment with the regular .ahk file and this issue has not happened so far (I already checked if both the ahk2exe & running the .ahk have the same version and they do).
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Key up event not firing

Post by mikeyww » 17 Jun 2021, 06:02

Thank you for explaining it. Your script does not use the "P" option for Ctrl, so you are seeing the logical state of the key, which might not match the physical state. In fact, these states often differ because AHK manages modifiers to send keys properly. Other reasons for possibly an inability to get a physical state might be lack of a keyboard hook, a conflicting hook or other software conflict, or an admin issue. That's about the extent of my knowledge! Others may know more about this.

Thus, I suspect that you are simply seeing a phenomenon of how AHK works. If you do a short test and then examine the KeyHistory, it may give you an understanding of how the keys are being managed. Keep in mind that if you are pressing many keys at the same time, some may not register, simply as a result of limitations of the keyboard and drivers. If you are seeing a different effect, then you could shorten your script until the problem disappears. At that point, you have pinpointed the issue.

If you are using AHK code in the script to run as admin, note that a different check is needed for a compiled script. That is provided in the AHK documentation, but I did not include it in my earlier post.

While testing, I would ensure that all other scripts, keyboard macros & managers, mouse utilities, clipboard managers, etc. are not running.
Post Reply

Return to “Ask for Help (v1)”