I am extremely confused by the sheer amount of how many different answers there are about GetAsyncKeyState(), I know what it does, but the confusing part is it's return value. I've seen many different examples on how people use this, but the main two are the following:
First:
if (GetAsyncKeyState(KEY) == -32767)
Second:
if (GetAsyncKeyState(KEY) & 0x8000)
The first confusing bit about this is that when using the Windows Calculator and converting -32767 to a hexadecimal, it is equal to 0x8001, so already they are not comparing the same thing. As far as I am aware (and please correct me if I understood incorrectly), 0x8000 is checking that the key is currently pressed but 0x8001 is doing the same while at the same time while telling you that it had been pressed after the last time GetAsyncKeyState() was called on that key.
Here is some sample code of the second confusion/problem with these different ways of essentially doing the same thing, yet at the same time achieving different results...
#include <iostream>
#include <Windows.h>
int main() {
while (true) {
for (int KEY = 8; KEY <= 190; KEY++)
{
if (GetAsyncKeyState(KEY) == -32767) {
std::cout << char(KEY) << std::endl;
}
}
}
return 0;
}
As you can see in the code above, I am using the "First" option to check if the key has been pressed. With this option no matter how fast you type (only tested under 120wpm) it won't repeat letters, as long as you are not holding them for a strangely longer time than a normal person holds a letter to type, if you do hold them you get the expected behaviour of it just repeating it (printing it out to the screen). It works perfectly when typing at normal speeds or fast speeds up to 120wpm tested, no repeated keys (I will explain what I mean by 'repeated keys'). Here is some code using the "Second" way...
#include <iostream>
#include <Windows.h>
int main() {
while (true) {
for (int KEY = 8; KEY <= 190; KEY++)
{
if (GetAsyncKeyState(KEY) & 0x8000) {
std::cout << char(KEY) << std::endl;
}
}
}
return 0;
}
With this one I run into a problem where no matter how fast I tap the key it will repeat a few times at minimum, you can try it yourself if you want to see what I mean, but as an example:
If I were to type H once this would be the result for First and Second respectively:
First:
H
Second:
H
H
H
H
H
.....etc maybe a few more times maybe one or two less times..... highly inconsistent
Why do they have very different outcomes even though in theory they should do the same thing (even though -32767 is not the same as 0x8000, it is the thing I've more commonly seen out there apart from 0x8000).