You never set the elements when a key is pressed.
You are also using the keyboardState
array as a local variable. You must make it global or static, because each time the MsgProc
function is called the keyboardState
is allocated on the stack, and there is no guarantee of its contents, which may have been modified by previous function calls using the same memory space.
In your code basically each element of keyboardState
has 255/256 chance of being non-zero, and you are detecting that instead of real key presses.
If you are not accessing keyboard states from outside, don't bother with the array - just use the wParam
argument of the MsgProc
function:
case WM_KEYDOWN:
if (wParam == DIK_W) {
OutputDebugStringW(L"W Button Pressed\n");
}
if (wParam == DIK_A) {
OutputDebugStringW(L"A Button Pressed\n");
}
if (wParam == DIK_S) {
OutputDebugStringW(L"S Button Pressed\n");
}
if (wParam == DIK_D) {
OutputDebugStringW(L"D Button Pressed\n");
}
return 0;
(Assuming your DIK_ key values are the same as WinAPI's VK_ key codes)