C/C++

C++控制台注册热键,MFC注册热键,全局热键

控制台方式 ,MSDN实例代码:

#include "stdafx.h"
#include <Windows.h>
​
int _tmain(int argc, _TCHAR* argv[])
{
	if (RegisterHotKey(
        NULL,
        1,
        MOD_ALT | MOD_NOREPEAT,
        0x42))  //0x42 is 'b'
    {
        _tprintf(_T("Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag\n"));
    }
 
    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0) != 0)
    {
        if (msg.message == WM_HOTKEY)
        {
            _tprintf(_T("WM_HOTKEY received\n"));            
        }
    } 
 
    return 0;
}

MFC方式 , 打开对话框,添加消息 : WM_HOTKEY 在OnCreat事件里加入:(添加消息响应 -> WM_CREAT) RegisterHotKey(m_hWnd,1001,MOD_ALT,’ Q’); 

LRESULT CdDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
{
    if(wParam == 1001)
    {
       int nMod = LOWORD(lParam);
       int Vk = HIWORD(lParam);
       CString str;
       str.Format(_T("Mod = %d, Vk = %d"), nMod, Vk);
       AfxMessageBox(str);
    }
    return true;
}

44f3ee996be85a55cfb5c1f50ca79b51.png

0 条评论

发表评论

你需要登录后才可进行发表