Can't use W,A,S,D and arrow keys together

With my OPENGL game I've gotten my characters to move around using the windows.h library, however I can't move both characters at the same time one uses wasd other uses arrow keys. here is my code, how do I make it still work if multiple keys are pressed at same time.
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
30
31
32
33
34
35
36
37
38
39
40
41
case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_UP:
            posy = posy + 0.025f;
        }
        switch (wParam)
        {
        case VK_DOWN:
            posy = posy - 0.025f;
        }
         switch (wParam)
        {
        case VK_LEFT:
            posx = posx - 0.025f;
        }
         switch (wParam)
        {
        case VK_RIGHT:
            posx = posx + 0.025f;
        }
         switch (wParam)
        {
        case VK_W:
            runnery = runnery + 0.025f;
        }
        switch (wParam)
        {
        case VK_S:
            runnery = runnery - 0.025f;
        }
         switch (wParam)
        {
        case VK_A:
            runnerx = runnerx - 0.025f;
        }
         switch (wParam)
        {
        case VK_D:
            runnerx = runnerx + 0.025f;
        }
This is probably a limitation of your keyboard, and has nothing to do with your code.

Btw, you might want to revisit the concept of the switch. You don't seem to get it. ;)
you have to implement the multiple keys functionnality..
You can use flags or an array then simply compare two or three elements and return true.
My friend told me I could make a separate thread (he knows Java) but how do I do that?
That's not the way processing key press in game.
All you need to do is to use an array to store state of each key, and iterate through them in your update function.

Sample code:

bool keys[256] = {}; // an array to store all key states, put to global or somewhere else

In the message loop

1
2
3
4
5
6
7
case WM_KEYDOWN:
    keys[wParam] = true;
    break;

case WM_KEYUP:
    keys[wParam] = false;
    break;


In your update function:

1
2
3
4
5
if (keys[VK_UP]) ...
if (keys[VK_DOWN]) ...
if (keys[VK_LEFT]) ...
if (keys[VK_RIGHT]) ...
...

Ugh, no errors but it just isn't moving my characters now.
How do I make an update function?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;
        
        #define VK_A           0x41
        #define VK_D         0x44
        #define VK_S             0x53
        #define VK_W             0x57

    case WM_KEYDOWN:
    keys[wParam] = true;
    break;

    case WM_KEYUP:
    keys[wParam] = false;
    break;
    if (keys[VK_UP])
        {
            posy = posy + 0.025f;
        }
        if (keys[VK_DOWN])
        {
            posy = posy - 0.025f;
        }
         if (keys[VK_LEFT])
        {
            posx = posx - 0.025f;
        }
         if (keys[VK_RIGHT])
        {
            posx = posx + 0.025f;
        }
         if (keys[VK_W])
        {
            runnery = runnery + 0.025f;
        }
        if (keys[VK_S])
        {
            runnery = runnery - 0.025f;
        }
         if (keys[VK_A])
        {
            runnerx = runnerx - 0.025f;
        }
         if (keys[VK_D])
        {
            runnerx = runnerx + 0.025f;
        }
    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}
Also, I just have my boolean at the top. do I need anything in the brackets?
You inserted the if statements to the wrong place.
Find your message loop - Something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while (1)
{
    if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
    {
        if (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg, NULL, 0, 0);
            DispatchMessage(&msg, NULL, 0, 0);
        }
        else
        {
            break;
        }
    }
    else
    {
        Update();  // This is where you update your character
        Draw();  // This is where you draw your things
    }
}


Put the if statements in the Update() function.

Hope that works.
Last edited on
I dont have an update or draw, also should I move my switch( windows procedure) above the message loop?
If you're using the
1
2
3
4
5
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

message loop, maybe you could try create a timer, and put the update character code (the if-else statements) there.
Topic archived. No new replies allowed.