Lightweight alternative to GetKeyState()?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Lightweight alternative to GetKeyState()?

22 May 2017, 04:14

Hi folks,
Is there a more lightweight way of checking if Ctrl/Shift/Alt buttons are pressed than GetKeyState()?
I have keys get stuck sometimes on my old PC, and since many keys need to be constantly monitored, I wonder if there is a more lightweight / non-intrusive way to check them.
Thanks!
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Lightweight alternative to GetKeyState()?

22 May 2017, 05:41

Declare a pass-through hotkey to all modifier buttons?

Code: Select all

~Shift::
~Ctrl::
~Alt::
~LWin::
~RWin::
	tooltip % "Modifier pressed: " A_ThisHotkey
	return

~Shift up::
~Ctrl up::
~Alt up::
~LWin up::
~RWin up::
	tooltip % "Modifier released: " A_ThisHotkey
	return

serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Re: Lightweight alternative to GetKeyState()?

23 May 2017, 02:36

evilC, thanks!
Yeah, that' a good idea. I will just add SetTimer to wait few seconds before showing tooltip
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Lightweight alternative to GetKeyState()?

23 May 2017, 05:41

A bit more elegant code - routes all hotkeys through same function, but instead of having to do string manipulation to work out what modifier was pressed / released (A_ThisHotkey would give like "~*Shift up" instead of "Shift"), it is all pre-calculated

Code: Select all

#SingleInstance Force

mods := ["Shift", "Ctrl", "Alt", "LWin", "RWin"]

for i, mod in mods {
	fn := Func("ModifierEvent").Bind(mod, 1)
	hotkey, % "~*" mod, % fn
	fn := Func("ModifierEvent").Bind(mod, 0)
	hotkey, % "~*" mod " up", % fn
}

ModifierEvent(key, state){
	Tooltip % "Modifier " key " entered state " state
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, doanmvu, uchihito, zvit and 279 guests