Why you should avoid while(GetKeyState("a", "P")) to detect release of key a
Posted: 30 Jun 2016, 05:21
Because it only works for one key.
I think we should avoid advising people to use this technique, as it has limitations that are not immediately obvious to most users.
Notice how with this code, if you hold a, then b, then release a, you do not get a "A RELEASED" tooltip.
Use this technique instead:
Note that KeyWait without a timeout is the same thing - the following code also exhibits this issue:
I think we should avoid advising people to use this technique, as it has limitations that are not immediately obvious to most users.
Notice how with this code, if you hold a, then b, then release a, you do not get a "A RELEASED" tooltip.
Code: Select all
~a::
tooltip A PRESSED
while (GetKeyState("a", "P")){
sleep 10
}
tooltip A RELEASED
return
~b::
tooltip B PRESSED
while (GetKeyState("b", "P")){
sleep 10
}
tooltip B RELEASED
return
Code: Select all
~a::
tooltip A PRESSED
return
~a up::
tooltip A RELEASED
return
~b::
tooltip B PRESSED
return
~b up::
tooltip B RELEASED
return
Code: Select all
~a::
tooltip A PRESSED
KeyWait, a
tooltip A RELEASED
return
~b::
tooltip B PRESSED
KeyWait, b
tooltip B RELEASED
return