Reputation: 300
I am trying to get my program to toggle a bool if the shift key is pressed. However, for some reason it doesn't seem to be receiving the message, as I've put MessageBoxes in both the KEYDOWN and KEYUP cases, and they don't get triggered. This feels like something of a noob question, but any help would be appreciated.
BOOL CALLBACK CreateRoom(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
static char *achTileDetails = new char[100,100];
static POINT *pDimensions = new POINT, *pMonsterLocations = new POINT[10], *pMinDisplay = new POINT, *pMaxDisplay = new POINT;
static HBITMAP *pahbmTileset = new HBITMAP[4];
static bool *pbShowBars = new bool;
switch(Message)
{
case WM_INITDIALOG:
g_hDialogInUse = hwnd;
SetFocus(hwnd);
for (int iii = 0; iii < 100; iii++)
for(int jjj = 0; jjj < 100; jjj++)
achTileDetails[iii,jjj] = 'g';
(*pDimensions).x = 20;
(*pDimensions).y = 10;
(*pMinDisplay).x = 0;
(*pMinDisplay).y = 0;
(*pMaxDisplay).x = 20;
(*pMaxDisplay).y = 10;
for (int iii = 0; iii < 4; iii++)
pahbmTileset[iii] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_CLEAR_GROUND + iii));
return TRUE;
case WM_PAINT:
DisplayRoom(hwnd, achTileDetails, *pMaxDisplay, *pMinDisplay, pahbmTileset, *pbShowBars);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDCANCEL:
delete[] achTileDetails;
delete pDimensions;
delete[] pMonsterLocations;
delete pMinDisplay;
delete pMaxDisplay;
delete[] pahbmTileset;
delete pbShowBars;
EndDialog(hwnd, IDCANCEL);
break;
}
break;
case WM_LBUTTONDOWN:
break;
case WM_KEYDOWN:
MessageBox(hwnd, "Down", "", NULL);
switch (wParam)
{
case VK_SHIFT:
*pbShowBars = true;
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
break;
}
break;
case WM_KEYUP:
{
MessageBox(hwnd, "Up", "", NULL);
switch(wParam)
{
case VK_SHIFT:
*pbShowBars = false;
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
break;
}
}
break;
default:
return FALSE;
}
}
Message Loop:
while(GetMessage(&Msg, NULL, 0, 0) != 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
This is now the full code for this callback. I'm aware the various static dynamic variables at the top probably aren't the most effective way of doing this, but work for my purposes.
Upvotes: 2
Views: 2545
Reputation: 942408
The WM_KEYUP/DOWN messages are posted to the window that has the focus. That is not going to be your dialog window, we can tell from the WM_COMMAND message handler that you have at least a Cancel button. Which is very likely to get the focus. That button doesn't do anything special with the message, other than you pressing the space bar.
This problem got started by using a dialog as your main window. It isn't exactly a suitable window type to implement a game, it is suitable for dialogs. Where the window just acts as a container, the work is done by the controls you put on the dialog. You can rescue it by intercepting the message in your message loop, before it is dispatched to the window with the focus. The TranslateAccelerator() function is the standard way to implement shortcut keystrokes. Or by calling IsDialogMessage inside the loop and implementing a handler for WM_GETDLGCODE.
Upvotes: 3