February 21st, 2011 09:48 AM
#16
Re: [C++] Win32 Program Wide Keyboard Shortcut
Thanks for all your help then.
February 26th, 2011 02:25 PM
#17
Re: [C++] Win32 Program Wide Keyboard Shortcut
I found it!
With the help of http://www.cplusplus.com/forum/windows/36860 I changed my search from "shortcut" to "hotkey" I found something about keyboard accelerators (http://social.msdn.microsoft.com/For...2-617d69557057 ) which led me here: http://msdn.microsoft.com/en-us/libr...ting_acc_table
After figuring out the way it worked, I have this (Spread across several files)
Code:
/*Keyboard Accelerators*/
ShortAccel ACCELERATORS {
VK_ESCAPE, IDA_ESC, VIRTKEY
}
/*---*/
//Load accelerators
HACCEL haccel;
haccel=LoadAccelerators(hinst, _T("ShortAccel"));
if(!haccel) //Make sure it loaded
MessageBox(hwnd, _T("Unable to load accelerator table"), _T("Error"), MB_ICONERROR);
while(GetMessage(&msg, NULL, 0, 0) > 0){
if(!TranslateAccelerator(FindWindow(_T("SettingsWindow"),_T("Settings")),haccel,&msg)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
/*---*/
#define IDA_ESC 4001
/*---*/
LRESULT fnSettingsProc_OnCommand(lpWndEventArgs wea){
switch(LOWORD(wea->wParam)){
case IDA_ESC: //Name given in accelerator table
DestroyWindow(wea->hwnd);
break;
}
return 0;
}
It works prefectly.
February 26th, 2011 03:08 PM
#18
Re: [C++] Win32 Program Wide Keyboard Shortcut
You may be surprised, but code suggested by Mark hardly differs much from the last code you showed. What translate accelerator does is sending WM_COMMAND with corresponding id in case virtual key can be found in the table.
Code:
while(GetMessage(&msg, NULL, 0, 0) > 0){
if (msg.message == WM_KEYDOWN &&
LOWORD(msg.wParam) == VK_ESCAPE)
SendMessage(FindWindow(_T("SettingsWindow"),_T("Settings")), WM_COMMAND, IDA_ESC, 0);
if(!IsDialogMessage(glbWin.vScrollContent, &msg)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
February 26th, 2011 03:16 PM
#19
Re: [C++] Win32 Program Wide Keyboard Shortcut
I see what you mean. I think I'll stick with the method I found though, as it is easier to expand.
February 26th, 2011 05:30 PM
#20
Re: [C++] Win32 Program Wide Keyboard Shortcut
...and you're 100% right. My point was just to make a bond between what you've found yourself and what you've been told of before.
February 26th, 2011 05:34 PM
#21
Re: [C++] Win32 Program Wide Keyboard Shortcut
And that I do appreciate. I wouldn't have known that is how it worked if it wasn't for your telling me.
March 3rd, 2011 11:29 PM
#22
Re: [RESOLVED] [C++] Win32 Program Wide Keyboard Shortcut
Originally Posted by
Marc G
You can do something as follows for your message loop:
Code:
while(GetMessage(&msg, NULL, 0, 0) > 0){
if (msg.message == WM_KEYDOWN &&
LOWORD(msg.wParam) == VK_ESCAPE)
MessageBox(hwnd, _T("Cancel"), _T(""), MB_OK);
if(!IsDialogMessage(glbWin.vScrollContent, &msg)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
of course, instead of displaying a messagebox, forward the escape command to the correct window.
This can done in the window callback handler. ie: in WindowProc. And for removing that behavior instead of showing a msgbox just drop the value bu setting a NULL.
Code:
LRESULT CALLBACK wndProc (HWND hW, UINT msg, WPARAM wP, LPARAM lP)
{
switch (msg)
{
case WM_KEYDOWN:
{
if (wParam == VK_ESCAPE)
wParam = NULL;
}
}
return 0;
}
just an alternative for the above code.
Last edited by hypheni; March 3rd, 2011 at 11:40 PM .
March 4th, 2011 01:54 AM
#23
Re: [RESOLVED] [C++] Win32 Program Wide Keyboard Shortcut
just an alternative for the above code.
No, it's not. The code in the message loop works with focus set anywhere in the app. Unlike your "alternative" which requires to have a focus on the particular window.
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks