check which button on keyboard is stuck

Post your working scripts, libraries and tools
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

check which button on keyboard is stuck

24 Dec 2016, 13:34

;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.
}
Outsourcing Clicks & Presses Since 2004.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: check which button on keyboard is stuck

24 Dec 2016, 13:38

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...
Outsourcing Clicks & Presses Since 2004.
Acecool
Posts: 37
Joined: 10 Jan 2016, 18:25
Facebook: Acecool
Google: Acecool
GitHub: Acecool
Contact:

Re: check which button on keyboard is stuck

25 Dec 2016, 18:41

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.
-Josh 'Acecool' Moser
I teach various programming languages, game programming, etc... I am also taking on programming jobs as my health allows.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: check which button on keyboard is stuck

26 Dec 2016, 07:37

yeah a bool return us probably better,but this was just for the casual ahk user if u will...
Outsourcing Clicks & Presses Since 2004.
User avatar
TheDewd
Posts: 1330
Joined: 19 Dec 2013, 11:16
Location: USA

Re: check which button on keyboard is stuck

26 Jan 2017, 14:54

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}"
        }
    }
}
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: check which button on keyboard is stuck

29 Jan 2017, 01:11

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:
Outsourcing Clicks & Presses Since 2004.
guest3456
Posts: 2566
Joined: 09 Oct 2013, 10:31

Re: check which button on keyboard is stuck

29 Jan 2017, 01:22

your getKeyState_fnc() is redundant because AHK has a GetKeyState() built in func already

brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: check which button on keyboard is stuck

02 Feb 2017, 08:09

@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()....
Outsourcing Clicks & Presses Since 2004.
guest3456
Posts: 2566
Joined: 09 Oct 2013, 10:31

Re: check which button on keyboard is stuck

02 Feb 2017, 10:35

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")

brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: check which button on keyboard is stuck

03 Feb 2017, 10:54

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...
Outsourcing Clicks & Presses Since 2004.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bad husband and 52 guests