
Originally Posted by
mmbob
No, you are wrong.
You are wrong about me being wrong.
The LSB is set if the key has been pressed since the last time it was called.
this is correct. However the GetAsynkeystate can only tell if that key has been pressed by checking the current state of the key. at the instant it is called. so for instance if u were to press a key and have a 1000 ms delay in which u pressed the key, but were
not pressing at the instant GetAsynckeystate was called again. This press would not be detected.
It does NOT return whether the key is down or not.
inconsequentially it does since the key must be down in order for the LSB to be set. It just also tells us that the key was pressed on the previous call, which lets us know that the key has been held down. Which may be incorrect. The key could also be being pressed at a frequency higher then then our detection rate, but for most cases this is inconsequential since humans can only move their fingers so fast.
For keys on the keyboard, the LSB is set to one every time the key repeat delay is up. The mouse, however has no repeat delay and is "pressed" only once.
No such thing as a key repeat delay. Just because most word processors limit their frequency of detection to human levels, does not mean a delay is included in the function through some kind of static time variable. Test it and u will see.
Smiley faces should come up for however long u click ur mouse.
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
while(1){
for(int ch = 0; ch < 256; ch++)
{
if(GetAsyncKeyState(ch))cout<<(char)ch<<endl;
}
Sleep(200);
}
return 0;
}
NOTE: I don't post anything without checking my facts. Even though I have written on this subject before I reviewed the Windows API, created a sample program, and tested several possible scenarios, because when it comes to programming theory is not enough. Compilation environment, and target platform can cause discrepancies, but I am still fairly confident what Ive said is accurate.
As to ur point:
If you still wish to test a Getsynckeystate function using bitwise operators that the key has not been held for more then one iteration you can do so, by using the ^ or XOR bitwise operator which will be false in ever situation besides in which one bit is off and the other on. and since our ^1 will always be set, the getasynckeystate should only return true when it is the first pass. However this still has the problem that u will have to isolate the first bit of the return value and then the key detection frequency will only be halved as the program will only detect ur key after skipping one iteration.
It is for these reasons that I recommend not using bitwise operators as replacements for boolean operators as well as strongly suggesting to only use the LSB in the return value of GASKS() to test if a key is currently being held, and NOT if it has been held, which is a bit more difficult, and do to the very nature of the API, hell the very nature of Windows and multitasking, or even the physical limitations of the machine... (though I wouldn't dare wonder why anyone would need to press a key that fast) a inherent margin for error. You simply have carried with you the illusion that many novices start off with. That a computer moves so fast that it must be constant, but this is not true. In the tightest weave there are gaps, and so is there here. And when working with programs know that these are consisted of the elements of the computer and are so small as to fall through the gaps humans can not see.
EDIT: Seems like ive missed something. Upon further testing ur concept seems to work with mouse buttons. Only the mouse buttons strange, but very interesting I give u that. Rather this is due to the hardware device or the function I can quite say.