MFC注册热键

MFC 控件添加热键

 

 
  1. MFC 控件添加热键 2014-12-24 14:28:47
  2. 标签:C++ MFC 控件 热键使用
  3. 给MFC中的控件添加我们想要的控件热键,在动手之前,必须清楚,热键分为local的和global的, 其中local的职能在当前程序有焦点(被激活)时有效,而global的,则无论什么时候都有效,测试local的要优先于global的,就是如果当前激活窗口的快捷键与未激活窗口的快捷键重叠,当前激活窗口优先响应。另外还包括menu,button。
  4. 自然而然,创建热键的方法也有多种,不同的创建方法创建的热键作用范围不一定相同。应该根据需求合理的选择自己的方法。
  5.  
  6. 方法一:
  7.  
  8. 打开对话框资源,选择指定控件的属性-->在caption项中你定义的名字后添加(&Y)。这样就可以实现热键。其中Y表示你要制定的快捷按键,我选的是Y,按下ALT+Y即可执行这个控件。
  9.  
  10. 该方式热键只有当FOCUS在控件上时才起作用,算是局部热键。
  11.  
  12. 方法二:
  13.  
  14. 1. 在资源视图中添加资源,选择"Accelerator"选项新建即可;
  15. 2. 打开新建的文件夹下的"IDR_ACCELERATOR1"中编辑,其中包括ID、修饰符、键、类型。ID中选择你要添加快捷键的菜单、按钮的ID编号,修饰符中选择你是否要使用组合键,如:Alt、Alt+Shift等等,键中选择你要使用什么样的虚拟键码,类型中选择VK形式或者是ASCII形式。
  16. 3. 在你的对话框的头文件中添加快捷键变量,如HACCEL m_hAcc;
  17. 4. 在你的对话框的源文件的OnInitDialog函数中加载快捷键,如:
  18. 1 HACCEL m_hAcc=LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));
  19.  
  20. 5. 重载函数PreTranslateMessage,使用类向导,重载虚函数PreTranslateMessage,如:
  21. 12345678910 BOOL CVideoMonitorDlg::PreTranslateMessage(MSG* pMsg)
  22. {
  23. if (WM_KEYFIRST<=pMsg->message&&pMsg->message<= WM_KEYLAST)
  24. {
  25. HACCEL hAccel=m_hAcc;
  26. if (hAccel && ::TranslateAccelerator(m_hWnd, hAccel, pMsg))
  27. return TRUE;
  28. }
  29. return CDialogEx::PreTranslateMessage(pMsg);
  30. }
  31.  
  32. 6. 通过以上5个步骤基本上即可对工程中的菜单或者按钮添加热键。
  33. 方法三:
  34. 使用WM_HOTKEY。
  35. 在对话框头文件中:
  36.  
  37. 12 afx_msg LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);
  38. afx_msg void OnDestroy();
  39.  
  40. 在对话框CPP文件中:
  41.  
  42. 1234 BEGIN_MESSAGE_MAP(CYourDlg, CDialog)
  43. ON_MESSAGE(WM_HOTKEY,OnHotKey)
  44. ON_WM_DESTROY()
  45. END_MESSAGE_MAP()
  46.  
  47. hotkey对应映射:
  48.  
  49. 1234567 LRESULT CYourDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
  50. {
  51. if(wParam==IDC_XXX)
  52. OnYourFunction();
  53. //ToDo: add function
  54. return 0;
  55. }
  56.  
  57. 注册热键:
  58.  
  59. 1234 BOOL CYourDlg::OnInitDialog()
  60. {
  61. ::RegisterHotKey(GetSafeHwnd(), IDC_XXX, MOD_ALT, 'D');//注册热键alt+D(D必须大写)
  62. }
  63.  
  64. 记得销毁:
  65.  
  66. 1234 void CYourDlg::OnDestroy()
  67. {
  68. ::UnregisterHotKey(GetSafeHwnd(),IDC_XXX);//销毁热键
  69. }
  70.  
  71. 另外一篇参考:http://lty2154216.blog.163.com/blog/static/17982629320117129491666/
  72.   键值
    /*==============================================================================================*/

    键盘键值如下图所示:

     

    OK,接下来看看VS键的宏定义:

    #ifndef NOVIRTUALKEYCODES
     
    /*
     * Virtual Keys, Standard Set
     */
    #define VK_LBUTTON        0x01    // 鼠标左键
    #define VK_RBUTTON        0x02    // 鼠标右键
    #define VK_CANCEL         0x03    // CANCEl键
    #define VK_MBUTTON        0x04    /* NOT contiguous with L & RBUTTON */ // 鼠标中键
     
    #if(_WIN32_WINNT >= 0x0500)
    #define VK_XBUTTON1       0x05    /* NOT contiguous with L & RBUTTON */
    #define VK_XBUTTON2       0x06    /* NOT contiguous with L & RBUTTON */
    #endif /* _WIN32_WINNT >= 0x0500 */
     
    /*
     * 0x07 : unassigned
     */
     
    #define VK_BACK           0x08    // BackSpace退格键
    #define VK_TAB            0x09    // Tab键
     
    /*
     * 0x0A - 0x0B : reserved
     */
     
    #define VK_CLEAR          0x0C    // Clear键
    #define VK_RETURN         0x0D    // 回车键(Enter键,非小键盘)
     
    #define VK_SHIFT          0x10    // Shift键
    #define VK_CONTROL        0x11    // Ctrl键
    #define VK_MENU           0x12    // Menu键
    #define VK_PAUSE          0x13    // Pause键
    #define VK_CAPITAL        0x14    // CapsLock键
     
    #define VK_KANA           0x15
    #define VK_HANGEUL        0x15  /* old name - should be here for compatibility */
    #define VK_HANGUL         0x15
    #define VK_JUNJA          0x17
    #define VK_FINAL          0x18
    #define VK_HANJA          0x19
    #define VK_KANJI          0x19
     
    #define VK_ESCAPE         0x1B    // ESC键
     
    #define VK_CONVERT        0x1C
    #define VK_NONCONVERT     0x1D
    #define VK_ACCEPT         0x1E
    #define VK_MODECHANGE     0x1F
     
    #define VK_SPACE          0x20    // Space键(空格)
    #define VK_PRIOR          0x21    // PageUp键(PgUp)
    #define VK_NEXT           0x22    // PageDown键(PgDn)
    #define VK_END            0x23    // End键
    #define VK_HOME           0x24    // Home键
    #define VK_LEFT           0x25    // 左方向键 ←
    #define VK_UP             0x26    // 上方向键 ↑
    #define VK_RIGHT          0x27    // 右方向键 →
    #define VK_DOWN           0x28    // 下方向键 ↓
    #define VK_SELECT         0x29    // Select键
    #define VK_PRINT          0x2A    // PrintScreen键(PrtSc)
    #define VK_EXECUTE        0x2B    // Execute键
    #define VK_SNAPSHOT       0x2C    // SnapShot键
    #define VK_INSERT         0x2D    // Insert键
    #define VK_DELETE         0x2E    // Delete键
    #define VK_HELP           0x2F    // Help键
     
    /*
     * VK_0 - VK_9 are the same as ASCII '0' - '9' (0x30 - 0x39)
     * 0x40 : unassigned
     * VK_A - VK_Z are the same as ASCII 'A' - 'Z' (0x41 - 0x5A)
     */
     
    #define VK_LWIN           0x5B
    #define VK_RWIN           0x5C
    #define VK_APPS           0x5D
     
    /*
     * 0x5E : reserved
     */
     
    #define VK_SLEEP          0x5F
     
    #define VK_NUMPAD0        0x60    // 数字键盘 0
    #define VK_NUMPAD1        0x61    // 数字键盘 1
    #define VK_NUMPAD2        0x62    // 数字键盘 2
    #define VK_NUMPAD3        0x63    // 数字键盘 3
    #define VK_NUMPAD4        0x64    // 数字键盘 4
    #define VK_NUMPAD5        0x65    // 数字键盘 5
    #define VK_NUMPAD6        0x66    // 数字键盘 6
    #define VK_NUMPAD7        0x67    // 数字键盘 7
    #define VK_NUMPAD8        0x68    // 数字键盘 8
    #define VK_NUMPAD9        0x69    // 数字键盘 9
    #define VK_MULTIPLY       0x6A    // 数字键盘 *
    #define VK_ADD            0x6B    // 数字键盘 +
    #define VK_SEPARATOR      0x6C    // 数字键盘 Enter回车
    #define VK_SUBTRACT       0x6D    // 数字键盘 -
    #define VK_DECIMAL        0x6E    // 数字键盘 .
    #define VK_DIVIDE         0x6F    // 数字键盘 /
    #define VK_F1             0x70    // F1
    #define VK_F2             0x71    // F2
    #define VK_F3             0x72    // F3
    #define VK_F4             0x73    // F4
    #define VK_F5             0x74    // F5
    #define VK_F6             0x75    // F6
    #define VK_F7             0x76    // F7
    #define VK_F8             0x77    // F8
    #define VK_F9             0x78    // F9
    #define VK_F10            0x79    // F10
    #define VK_F11            0x7A    // F11
    #define VK_F12            0x7B    // F12
    #define VK_F13            0x7C    // F13
    #define VK_F14            0x7D    // F14
    #define VK_F15            0x7E    // F15
    #define VK_F16            0x7F    // F16
    #define VK_F17            0x80    // F17
    #define VK_F18            0x81    // F18
    #define VK_F19            0x82    // F19
    #define VK_F20            0x83    // F20
    #define VK_F21            0x84    // F21
    #define VK_F22            0x85    // F22
    #define VK_F23            0x86    // F23
    #define VK_F24            0x87    // F24
     
    /*
     * 0x88 - 0x8F : unassigned
     */
     
    #define VK_NUMLOCK        0x90    // NumLock键(NmLk)
    #define VK_SCROLL         0x91    // ScrollLock键(ScrLk)
     
    /*
     * NEC PC-9800 kbd definitions
     */
    #define VK_OEM_NEC_EQUAL  0x92   // '=' key on numpad
     
    /*
     * Fujitsu/OASYS kbd definitions
     */
    #define VK_OEM_FJ_JISHO   0x92   // 'Dictionary' key
    #define VK_OEM_FJ_MASSHOU 0x93   // 'Unregister word' key
    #define VK_OEM_FJ_TOUROKU 0x94   // 'Register word' key
    #define VK_OEM_FJ_LOYA    0x95   // 'Left OYAYUBI' key
    #define VK_OEM_FJ_ROYA    0x96   // 'Right OYAYUBI' key
     
    /*
     * 0x97 - 0x9F : unassigned
     */
     
    /*
     * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
     * Used only as parameters to GetAsyncKeyState() and GetKeyState().
     * No other API or message will distinguish left and right keys in this way.
     */
    #define VK_LSHIFT         0xA0
    #define VK_RSHIFT         0xA1
    #define VK_LCONTROL       0xA2
    #define VK_RCONTROL       0xA3
    #define VK_LMENU          0xA4
    #define VK_RMENU          0xA5
     
    #if(_WIN32_WINNT >= 0x0500)
    #define VK_BROWSER_BACK        0xA6
    #define VK_BROWSER_FORWARD     0xA7
    #define VK_BROWSER_REFRESH     0xA8
    #define VK_BROWSER_STOP        0xA9
    #define VK_BROWSER_SEARCH      0xAA
    #define VK_BROWSER_FAVORITES   0xAB
    #define VK_BROWSER_HOME        0xAC
     
    #define VK_VOLUME_MUTE         0xAD
    #define VK_VOLUME_DOWN         0xAE
    #define VK_VOLUME_UP           0xAF
    #define VK_MEDIA_NEXT_TRACK    0xB0
    #define VK_MEDIA_PREV_TRACK    0xB1
    #define VK_MEDIA_STOP          0xB2
    #define VK_MEDIA_PLAY_PAUSE    0xB3
    #define VK_LAUNCH_MAIL         0xB4
    #define VK_LAUNCH_MEDIA_SELECT 0xB5
    #define VK_LAUNCH_APP1         0xB6
    #define VK_LAUNCH_APP2         0xB7
     
    #endif /* _WIN32_WINNT >= 0x0500 */
     
    /*
     * 0xB8 - 0xB9 : reserved
     */
     
    #define VK_OEM_1          0xBA   // ';:' for US
    #define VK_OEM_PLUS       0xBB   // '+' any country
    #define VK_OEM_COMMA      0xBC   // ',' any country
    #define VK_OEM_MINUS      0xBD   // '-' any country
    #define VK_OEM_PERIOD     0xBE   // '.' any country
    #define VK_OEM_2          0xBF   // '/?' for US
    #define VK_OEM_3          0xC0   // '`~' for US
     
    /*
     * 0xC1 - 0xD7 : reserved
     */
     
    /*
     * 0xD8 - 0xDA : unassigned
     */
     
    #define VK_OEM_4          0xDB  //  '[{' for US
    #define VK_OEM_5          0xDC  //  '\|' for US
    #define VK_OEM_6          0xDD  //  ']}' for US
    #define VK_OEM_7          0xDE  //  ''"' for US
    #define VK_OEM_8          0xDF
     
    /*
     * 0xE0 : reserved
     */
     
    /*
     * Various extended or enhanced keyboards
     */
    #define VK_OEM_AX         0xE1  //  'AX' key on Japanese AX kbd
    #define VK_OEM_102        0xE2  //  "<>" or "\|" on RT 102-key kbd.
    #define VK_ICO_HELP       0xE3  //  Help key on ICO
    #define VK_ICO_00         0xE4  //  00 key on ICO
     
    #if(WINVER >= 0x0400)
    #define VK_PROCESSKEY     0xE5
    #endif /* WINVER >= 0x0400 */
     
    #define VK_ICO_CLEAR      0xE6
     
     
    #if(_WIN32_WINNT >= 0x0500)
    #define VK_PACKET         0xE7
    #endif /* _WIN32_WINNT >= 0x0500 */
     
    /*
     * 0xE8 : unassigned
     */
     
    /*
     * Nokia/Ericsson definitions
     */
    #define VK_OEM_RESET      0xE9
    #define VK_OEM_JUMP       0xEA
    #define VK_OEM_PA1        0xEB
    #define VK_OEM_PA2        0xEC
    #define VK_OEM_PA3        0xED
    #define VK_OEM_WSCTRL     0xEE
    #define VK_OEM_CUSEL      0xEF
    #define VK_OEM_ATTN       0xF0
    #define VK_OEM_FINISH     0xF1
    #define VK_OEM_COPY       0xF2
    #define VK_OEM_AUTO       0xF3
    #define VK_OEM_ENLW       0xF4
    #define VK_OEM_BACKTAB    0xF5
     
    #define VK_ATTN           0xF6
    #define VK_CRSEL          0xF7
    #define VK_EXSEL          0xF8
    #define VK_EREOF          0xF9
    #define VK_PLAY           0xFA
    #define VK_ZOOM           0xFB
    #define VK_NONAME         0xFC
    #define VK_PA1            0xFD
    #define VK_OEM_CLEAR      0xFE
     
    /*
     * 0xFF : reserved
     */
     
     
    #endif /* !NOVIRTUALKEYCODES */
    另外呢,主键盘区域(非小键盘)的A-Z字母与0-9键值与相应字母和数字的ASCII码相同。

    A-Z及0-9的ASCII码值
    描述    ANSII码值
    A    65
    B    66
    C    67
    D    68
    E    69
    F    70
    G    71
    H    72
    I    73
    J    74
    K    75
    L    76
    M    77
    N    78
    O    79
    P    80
    Q    81
    R    82
    S    83
    T    84
    U    85
    V    86
    W    87
    X    88
    Y    89
    Z    90
    0    48
    1    49
    2    50
    3    51
    4    52
    5    53
    6    54
    7    55
    8    56
    9    57
    --------------------- 
    作者:Meditating 
    来源:CSDN 
    原文:https://blog.csdn.net/WU9797/article/details/81146364 

     

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值