# Keyboard input

This topic is 4771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello Consider the following c++ program:
[source lang='c++']

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
do
{
if(GetAsyncKeyState('A')) cout << "A ";
if(GetAsyncKeyState('S')) cout << "S ";
if(GetAsyncKeyState('W')) cout << "W ";
if(GetAsyncKeyState('D')) cout << "D ";

Sleep(10);

}while (!GetAsyncKeyState(VK_ESCAPE));

return 0;
}


When i push and hold all the keys A, S, D and W the program should output A S W D each time the loop is run. Problem is: it doesnt. Instead the PC speaker starts beeping and some of the pressed keys are not detected. This isnt only a problem in my program. Many commercial games i play have this problem too. So my guess is that this problem is due to the operating system (XP) or the BIOS. Are there any workarounds so that i can use more than 3 keys simultaneously? Is this even a recognised problem? //Emil

##### Share on other sites
I faced quite similar problem some time ago when I spilled some wine on my keyboard. I tried cleaning it but gave up after a couple of hours. Next day I bought a new one and realised that when I keep up and left arrows at the same time left ctrl doesn’t work.

I guess I could find some more keys combinations that behave similar way. My best guess is that there was some short circuit on the mainboard.

If there is nothing you can do in your BIOS there may be little you can do about it. Have you tried connecting some other keyboard, by the way?

##### Share on other sites

http://www.dribin.org/dave/keyboard/one_html/

##### Share on other sites
most keyboards wont handle more then a couple of keys being pressed simultanously.

##### Share on other sites
GetAsyncKeyState() doesn't return a simple true or false; the return value is encoded in such a way that it carries more information than just whether the key is down or not. MSDN says:
Quote:
 Return ValueIf the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

Note that if the most significant bit is set, the key is down, but that other bits may be set even if it is not, resulting in a false positive sometimes if you just check if the return is zero or not. Many people (including myself) use a macro like this to avoid the confusion:

#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code) & 0x8000)

Thus, your original code would become:

#include <iostream>#include <windows.h>#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code) & 0x8000)using namespace std;int main(){    do    {        if(KEY_DOWN('A')) cout << "A ";        if(KEY_DOWN('S')) cout << "S ";        if(KEY_DOWN('W')) cout << "W ";        if(KEY_DOWN('D')) cout << "D ";        Sleep(10);	    }while (!KEY_DOWN(VK_ESCAPE));    return 0;}

That may not actually change the results you get; it isn't totally clear to me whether you were having any problems not related to pressing too many keys at once, but you should use something like that macro in any case, or it will probably cause you trouble down the line.

As to pressing many keys at once: it is a known hardware limitation related to the way keyboards work. You can't fix it with software. The best work-around that I have seen (and it usually works pretty well) is to design your input configuration so that there isn't a ligitimate reason to press very many keys at once. Usually that means moving some of the stuff that requires holding keys, like movement or firing, to the mouse and/or joystick. Of course, that won't help you much if you stick with console programs, but I suspect you are just trying things out while you learn to program, so I don't think you should worry much about it.

1. 1
2. 2
JoeJ
20
3. 3
4. 4
frob
12
5. 5

• 13
• 18
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632194
• Total Posts
3004681

×