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 ?
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;
}
@ 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:
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()".
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.