Page 1 of 1

GetKeyState("Insert", "T") returning wrong toggle state

Posted: 28 Jan 2019, 18:57
by Scoox
The following code doesn't always return the correct Insert key toggle state after switching between applications:

Code: Select all

~Insert::
	MsgBox % GetKeyState("Insert", "T")
Return
For instance, if I have two console windows, A & B, both initially with Insert mode turned off. Then I hit the Insert key while A is the active window. Now A is in Insert mode, while B remains in regular (non-insert) mode. Considering this is a per-window thing, what's the point of treating Insert as a toggle state?

Is there some sort of "global" insert state and applications are free to observe this state or keep their own internal state? CapsLock seems to work globally across all applications. Thanks!

Re: GetKeyState("Insert", "T") returning wrong toggle state

Posted: 28 Jan 2019, 19:40
by swagfag
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getkeystate
Retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed).
on, off—alternating each time the key is pressed, this is the extent to which getkeystate will tell u if ur key is toggled
what it wont tell u, is whether whatever edit field, ur cursor happens to reside in, is in insert-mode. some edits dont even support insert-mode, for instance

Re: GetKeyState("Insert", "T") returning wrong toggle state

Posted: 28 Jan 2019, 19:46
by Scoox
Thanks swagfag. Was just googling this and this came up:
I note that the Insert key, unlike Caps-Lock and Num-Lock, does not have a systemwide state, and it varies per-application or even per-control. I believe (though am uncertain) that maintaining the state of the Insert key is your responsibility and that GetKeyState will not work as you intend with the Insert key in particular.
Which confirms what you say. Thanks for chiming in!