0. Test environment
VS2015 Professional Edition, dialog-based MFC program
1, MFC project Settings
Class Wizard, message->wm_hotkey-> Add handler, OK or edit code
The following code is generated by the MFC project after execution
// -------------------Mfc_testdlg.h afx_msg void Onhotkey (UINT Nhotkeyid, uint nKey1, uint nKey2); // Declares the message handler function // -------------------Mfc_testdlg.cpp On_wm_hotkey () // message map macros, binding messages and corresponding handler functions . Span style= "color: #0000ff;" >void Cmfc_testdlg::onhotkey (UINT Nhotkeyid, uint nKey1, uint nKey2) // Span style= "color: #008000;" > implement message handler { // TODO: Add the message Handler code here and/or call the default value Cdialogex::onhotkey (Nhotkeyid, NKey1, NKey2);}
2. Add Hotkey Registration/Cancellation Code2.1. Hotkey Registration/Cancellation API
//defines a system-wide hot key.BOOL WINAPI RegisterHotKey (//https://msdn.microsoft.com/en-us/library/ms646309(v=vs.85). aspx_in_opt_ HWND hwnd,//window handle to receive Hotkey trigger event_in_intId//the ID of the hotkey, the application ID range [0X0000,0XBFFF]. DLL range [0XC000~0XFFFF]_in_ UINT Fsmodifiers,//declares Alt (Mod_alt), Ctrl (Mod_control), Shift (Mod_shift), WIN key (Mod_win), mod_norepeat, or its combination in a key combination_in_ UINT VK//the code of the normal key (for example ' A '), other see https://msdn.microsoft.com/en-us/library/dd375731 (v=vs.85). aspx);//frees a hot key previously registered by the calling thread.BOOL WINAPI Unregisterhotkey (//https://msdn.microsoft.com/en-us/library/ms646327(v=vs.85). aspx_in_opt_ HWND hwnd,//window handle to receive Hotkey trigger event_in_intId//the ID of the hotkey
2.2. Examples of API usage
The registration of the hotkey can be placed in the OnInitDialog () function or the Wm_create event handler, and the unregister can be placed in the Wm_destroy event handler function.
//wm_create Event Proc Handlerintcmfc_testdlg::oncreate (lpcreatestruct lpcreatestruct) {if(Cdialogex::oncreate (lpcreatestruct) = =-1) return-1; //TODO: Add your own dedicated creation code here//Register Hotkey Ctrl + s (s)RegisterHotKey ( This->getsafehwnd (),1001, Mod_control,'S'); RegisterHotKey ( This->getsafehwnd (),1002, Mod_control,'s'); //Register Hotkey Ctrl + F1 (do not repeat, do not receive multiple times)RegisterHotKey ( This->getsafehwnd (),1003, Mod_control |mod_norepeat, vk_f1); return 0;}//wm_destroy Event Proc HandlervoidCmfc_testdlg::ondestroy () {Cdialogex::ondestroy (); //TODO: Add Message Handler code here//Unlock Hotkey RegistrationUnregisterhotkey ( This->getsafehwnd (),1001); Unregisterhotkey ( This->getsafehwnd (),1002); Unregisterhotkey ( This->getsafehwnd (),1003);}
3, modify the hotkey processing function
voidCmfc_testdlg::onhotkey (UINT Nhotkeyid, uint nKey1, uint nKey2) {//TODO: Add Message Handler code and/or call default values here Switch(Nhotkeyid) { Case 1001: Case 1002: //proc Ctrl + S here Break; Case 1003: //proc Ctrl + F1 here Break; default: Break; } cdialogex::onhotkey (Nhotkeyid, NKey1, NKey2);}
----------------------------------------------
Resources:
http://blog.csdn.net/wangjieest/article/details/6910166
Msdn
mfc-Registration Hotkey