Arrow Keys Input

Hello there !

I am writing a Win32 console application to create a maze game, and right now my "player" moves using 2,4,6,8 buttons on the numpad, but since numpads aren't present on laptop keyboards, I want him to move by the up, down, right, left arrow keys, but I don't know how to do that. Can someone please help me ?
You'll want something like this:

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
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_KEYDOWN:
	{
		switch(wParam)
		{
		case VK_LEFT:
		case VK_NUMPAD4:
			// move it left
			break;
		case VK_UP:
		case VK_NUMPAD8:
			// move it up
			break;
		case VK_RIGHT:
		case VK_NUMPAD6:
			// move it right
			break;
		case VK_DOWN:
		case VK_NUMPAD2:
			// move it down
			break;
		}

	}
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
return 0;
}


http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

If you have num-lock on, then I don't think you need to map VK_NUMPAD#.
@ Stewbound: If this was a Windowed application you'd have it nailed. But the OP said this was a console app and consoles don't have callback functions. You could make the argument that the OP might as well make this into a Windowed app and I'd agree with you 100%, but for now let's look at another way of getting this done for them.

@OP: Take everything that Stewbound has from Lines 3 - 27 and put it inside of a while() loop with one or two minor changes. Like this:
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
    HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
    DWORD NumInputs = 0;
    DWORD InputsRead = 0;
    bool running = true;
    
    INPUT_RECORD irInput;
    
    GetNumberOfConsoleInputEvents(hInput, &NumInputs);
    
    while(running)
    {
        ReadConsoleInput(hInput, &irInput, 1, &InputsRead);
        //std::cout << irInput.Event.KeyEvent.wVirtualKeyCode << std::endl;
        
        switch(irInput.Event.KeyEvent.wVirtualKeyCode)
        {
            case VK_ESCAPE:
                running = false;
                //Quit The Running Loop
            break;
            case VK_LEFT:
		case VK_NUMPAD4:
			// move it left
			break;
		case VK_UP:
		case VK_NUMPAD8:
			// move it up
			break;
		case VK_RIGHT:
		case VK_NUMPAD6:
			// move it right
			break;
		case VK_DOWN:
		case VK_NUMPAD2:
			// move it down
			break;
        } 
        
    }


The thing to note here is that we are using the function "ReadConsoleInput()" to get the input to the console instead of a callback function. This posses a problem because the program will stop at "ReadConsoleInput()" and not execute anything else if there is no input on the buffer. This means that until the user does something the entire game pauses. If you want monsters to move around in the background or something even when the user isn't moving then I can show you how to do this by adding a call to "PeekConsoleInput()".
Whoops, I stopped reading at WIN32...
Thanks a lot guys .... this really helped. BTW do I need to include any libraries or header files ?

Thank you once again.
The only header files I had to include were windows.h, iostream and wincon.h there were no additional libraries beyond kernel32 and user32. There were a few more changes I made to the whole thing, for some reason I just kept writing. I could post them later if you'd like.
Last edited on
Topic archived. No new replies allowed.