Reading multiple key states with Win32 API

I am trying to use Win32 API to get key states, but when I press too many keys at once it seems to ignore some of the keys. I have tried using GetAsyncKeyState and GetKeyboardState, but both have the same result. What could I be doing wrong that would cause this problem?

With the code below I can only read 2 keys at once. Pressing a third key is ignored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    while (exit == 0)
    {
        TimeWait(.054);
        if (GetAsyncKeyState(VK_SPACE))
            Map.WriteChar(Player.xpos, Player.ypos, ' ');
        if (GetAsyncKeyState(VK_CONTROL))
            Map.ClearMap(fillchar);
        if (GetAsyncKeyState(VK_RIGHT))
            Player.Shift(1);
        if (GetAsyncKeyState(VK_LEFT))
            Player.Shift(2);
        if (GetAsyncKeyState(VK_UP))
            Player.Shift(4);
        if (GetAsyncKeyState(VK_DOWN))
            Player.Shift(3);
        if (GetAsyncKeyState(VK_ESCAPE))
            exit=1;
        if (Player.xpos>mapx)
            Player.xpos=mapx;
        else if (Player.xpos<1)
            Player.xpos=1;
        if (Player.ypos>mapy)
            Player.ypos=mapy;
        else if (Player.ypos<1)
            Player.ypos=1;
        Map.PrintMap();
        SetConsoleCursorPosition(cppstdout,{(Player.xpos-1), (Player.ypos-1)});
        WriteConsole(cppstdout, playericon.c_str(), lstrlen(playericon.c_str()), &cChars, NULL);
    };


EDIT: sometimes it reads three keys at once, I am not sure why.
Last edited on
when I press too many keys at once it seems to ignore some of the keys
That'll always be the case.

You need to describe how many keys you press together and how frequenlty to us to guage if this is unreasonable behaviour.
kbw wrote:
That'll always be the case.

You need to describe how many keys you press together and how frequenlty to us to guage if this is unreasonable behaviour.

I did
R3B3LCAUSE wrote:
With the code below I can only read 2 keys at once. Pressing a third key is ignored
sometimes it reads three keys at once, I am not sure why.
Before I begin...

1
2
3
4
5
if(GetAsyncKeyState(VK_ESCAPE)) // <- this is wrong
{}

if(GetAsyncKeyState(VK_ESCAPE) & 0x8000) // <- this is right
{}


Bit 15 is the only bit you are interested in (assuming you want the realtime state of the key). Other bits mean other things and may give you false positives if they are set. Use the & operator to isolate the interesting bit.



As for your actual problem:
With the code below I can only read 2 keys at once. Pressing a third key is ignored


This is not a problem with your code. It's a hardware limitation of your keyboard.

Most keyboards (especially low-end ones) are designed for typing. When people type, they typically do not press more than 1 key at a time.

Gaming keyboards typically allow more keys to be pressed simultaneously, but they are usually much more expensive, and not many people have them.


There is nothing you can do to solve this problem. It's hardware issue. You have 2 options:

1) Get a better keyboard

2) Redesign your program so that it doesn't require multiple keys to be pressed at once.


#2 is the better solution, because if you are having this problem... then other people are going to have it as well. So unless you only want your program to be usable for people who have really high-end keyboards, you don't want to use solution #1.



EDIT:

On an unrelated note. Using WinAPI to make a game is wretched. Consider getting a lib like SFML. Aside from having better performance, it's also easier to program and will be more portable.
Last edited on
I am not really making anything I plan on distributing, just messing around for fun/experience. And I am pretty sure my keyboard can read more than that 2 or 3 keys because I regularly press more than that many at once when I play games without any issues. Thanks for that tip about the bits!
And I am pretty sure my keyboard can read more than that 2 or 3 keys because I regularly press more than that many at once when I play games without any issues.


Which keys?

Keys like Ctrl/Shift/Alt are designed to be held down in conjunction with other keys, so you are likely to be able to hold 3+ if one or more of the keys is one of them. This is why many FPS's use 'wads' for movement, and hold Shift/Ctrl to crouch... rather than holding 'q' to crouch or something like that (since holding 'q' and pressing a+w at the same time is less likely to work)
I use space, control, and the arrow keys. And the key combo that doesn't work is left+up+space
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <Windows.h>
using namespace std;

bool keydown(int key)
{
    return (GetAsyncKeyState(key) & 0x8000) != 0;
}

int main()
{
    while(!keydown(VK_ESCAPE))
    {
        if(keydown(VK_SPACE) && keydown(VK_LEFT) && keydown(VK_UP))
        {
            cout << "combo pressed!" << endl;
        }
        Sleep(10);
    }

    return 0;
}


This code works on my machine. I am able to press that key combo and see the "combo pressed!" message.

If this code does not work for you, it is your keyboard.
Last edited on
I used wasd in my program and it fixed the issue. It must be my keyboard
Topic archived. No new replies allowed.