Page 1 of 1

check which button on keyboard is stuck

Posted: 24 Dec 2016, 13:34
by brutus_skywalker
;usage examples
loop
{
getKeyState_fnc("RAlt")
getKeyState_fnc("LAlt")
getKeyState_fnc("RCtrl")
getKeyState_fnc("LCtrl")
getKeyState_fnc("RShift")
getKeyState_fnc("LShift")
Sleep, 100
}

getKeyState_fnc(modifier) ;simply checks if the specified key is being held down,usefull to check if a key is stuck or not.
{
GetKeyState, state, %modifier%
if state = D
MsgBox %modifier% key is down.
}

Re: check which button on keyboard is stuck

Posted: 24 Dec 2016, 13:38
by brutus_skywalker
so,I've been using ahk for ages but my first posting so excitin, neeway I have a slightly rusty laptop with a few modifier keys that tend to get stuck,so here's a little script to detect if a suspected key is currently stuck, though I actually poll for stuck keys on demand instead of a loop that'll pop up msgbox everytime I press a button...

Re: check which button on keyboard is stuck

Posted: 25 Dec 2016, 18:41
by Acecool
I'd suggest rewriting it to return true or false depending on whether it is stuck or not... I'd also recommend including the Win keys.. Create a second function which runs the function and if stuck it releases the key.. Create a 3rd function which calls check and release for all of them.

Re: check which button on keyboard is stuck

Posted: 26 Dec 2016, 07:37
by brutus_skywalker
yeah a bool return us probably better,but this was just for the casual ahk user if u will...

Re: check which button on keyboard is stuck

Posted: 26 Jan 2017, 14:54
by TheDewd
Here's my attempt at writing a function to release the keys:

Code: Select all

ReleaseKey("RAlt", "LAlt", "RCtrl", "LCtrl", "RShift", "LShift")

ReleaseKey(Keys*) {
    For Index, Value In Keys {
        If (GetKeyState(Value, "P")) {
            SendInput, % "{" Keys " Up}"
        }
    }
}

Re: check which button on keyboard is stuck

Posted: 29 Jan 2017, 01:11
by brutus_skywalker
TheDewd wrote:Here's my attempt at writing a function to release the keys:

Code: Select all

ReleaseKey("RAlt", "LAlt", "RCtrl", "LCtrl", "RShift", "LShift")

ReleaseKey(Keys*) {
    For Index, Value In Keys {
        If (GetKeyState(Value, "P")) {
            SendInput, % "{" Keys " Up}"
        }
    }
}


Neat,beats just finding the stuck key and physically beating down on it,though,if key is physically stuck,you still gotta physically beat on the key... :clap:

Re: check which button on keyboard is stuck

Posted: 29 Jan 2017, 01:22
by guest3456
your getKeyState_fnc() is redundant because AHK has a GetKeyState() built in func already

Re: check which button on keyboard is stuck

Posted: 02 Feb 2017, 08:09
by brutus_skywalker
@guest3456

getKeyState_fnc() is a configurable abstraction for the built in function that alerts user to a stuck key based solely on key being specified as a param, nothing more nothing less. though perhaps I should have named it getStuckKey()....

Re: check which button on keyboard is stuck

Posted: 02 Feb 2017, 10:35
by guest3456
brutus_skywalker wrote:@guest3456

getKeyState_fnc() is a configurable abstraction for the built in function that alerts user to a stuck key based solely on key being specified as a param, nothing more nothing less. though perhaps I should have named it getStuckKey()....
no.

your func is an abstraction for the built-in GetKeyState command. there is also a GetKeyState() function built-into AHK as well, which is what TheDewd used in his reply above.

read the docs:
https://autohotkey.com/docs/commands/GetKeyState.htm

this is all that is needed:

Code: Select all

Loop
   MsgBox, % GetKeyState("LCtrl")
or if you want more text:

Code: Select all

Loop
   MsgBox, % "LCtrl modifier is " . (GetKeyState("LCtrl") ? "down" : "up")

Re: check which button on keyboard is stuck

Posted: 03 Feb 2017, 10:54
by brutus_skywalker
well silly me,my bad AND thanks for the heads up. I didn't consider callin the function from the msgbox, cheers.

I SHOULD HAVE READ THE DOCS MORE THOROUGHLY!

I'll leave up my post incase anyone else makes the same mistake...