today I was becoming crazy for this problem, I have a simple dialog box created through the classical DialogBox() but the problem is that you cannot load accelerators (the shortcuts like CTRL-O, CTRL-F and so on) because the loop which handles them is internally managed by Windows.
So my friend James suggested me this method which works and allows us to get anything typed on the keyboard so accelerators are only one of the many things which we can do with this code, for example I have added the possibility of navigating inside a fixed size edit control.
IDC_TIMER is the ID of the resource, you can assign any value to it
100 is the amount of milliseconds of the timer
If you know other ways, write them here:
#include <windows.h>
#include "resource.h"
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_INITDIALOG: {
SetTimer(hwnd, IDC_TIMER, 100, (TIMERPROC)NULL);
} break;
case WM_COMMAND: {
} break;
case WM_TIMER: {
if(GetForegroundWindow() != hwnd) break;
if(GetAsyncKeyState(VK_CONTROL)) {
if(GetAsyncKeyState('A')) {
MessageBox(0, "CTRL-A pressed", "test", MB_OK | MB_TASKMODAL);
}
}
} break;
case WM_CLOSE: {
KillTimer(hwnd, IDC_TIMER);
DestroyWindow(hwnd);
} break;
case WM_DESTROY: {
PostQuitMessage(0);
} break;
default: {
return(0);
} break;
}
return(0);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
return(DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc));
}
|