全局快捷键设置类,文章最下面,有3种不同的用法(如果设置的快捷键,与其他软件的快捷键相同,那么只有你的程序起作用,你释放后它才可以使用)
.h文件
#pragma once
class CHotKey
{
public:
CHotKey(void);
~CHotKey(void);
public:
//注册全局热键
BOOL RegisterHotKey(HWND hWnd, UINT fsModifiers, UINT vk);
//卸载全局热键
BOOL UnRegisterHotKey(HWND hWnd);
//是否触发热键
BOOL IsTriggerHosKey(WPARAM wParam, LPARAM lParam);
//
virtual void ResponseKeyEvent(WPARAM wParam, LPARAM lParam);
private:
int m_nHotKeyID;//热键ID
HWND m_hWnd;
UINT m_fsModifiers;
UINT m_vk;
};
.cpp文件
#include "StdAfx.h"
#include "HotKey.h"
CHotKey::CHotKey(void)
:m_nHotKeyID(-1)
,m_hWnd(NULL)
{
}
CHotKey::~CHotKey(void)
{
if (m_hWnd)
UnRegisterHotKey(m_hWnd);
}
BOOL CHotKey::RegisterHotKey( HWND hWnd, UINT fsModifiers, UINT vk )
{
m_hWnd = m_hWnd;
m_fsModifiers = fsModifiers;
m_vk = vk;
m_nHotKeyID = GlobalAddAtom("MyHotKey") - 0xC000;
return ::RegisterHotKey(hWnd,m_nHotKeyID,m_fsModifiers,m_vk);
}
BOOL CHotKey::UnRegisterHotKey(HWND hWnd)
{
m_hWnd = NULL;
return ::UnregisterHotKey(hWnd,m_nHotKeyID);
}
BOOL CHotKey::IsTriggerHosKey(WPARAM wParam, LPARAM lParam)
{
if (LOWORD(lParam) == m_fsModifiers && HIWORD(lParam) == m_vk)
return TRUE;
return FALSE;
}
void CHotKey::ResponseKeyEvent( WPARAM wParam, LPARAM lParam )
{
if (IsTriggerHosKey(wParam,lParam))
{
//.....
}
}
//使用如下
列1:1个系统键+1虚拟键
m_HotKey.RegisterHotKey(m_uiPaintManager.GetPaintWindow(),MOD_ALT,'S');
//收到数据处理
switch(uMsg)
{
case WM_HOTKEY:
{
if (m_HotKey.IsTriggerHosKey(wParam,lParam))
{
//做事情
}
}
break;
}
列2:2个系统键(ctrl+shit)
qq的发送消息方式
m_HotKey.RegisterHotKey(hWnd,m_nHotKeyID,MOD_SHIFT|MOD_CONTROL,0);
//收到数据处理
switch(uMsg)
{
case WM_HOTKEY:
{
if ((GetKeyState(VK_SHIFT)<0) && (GetKeyState(VK_CONTROL)<0))
{
//做事情
}
}
break;
}
列3:2个系统键+1虚拟键
m_HotKey.RegisterHotKey(hWnd,m_nHotKeyID,MOD_SHIFT|MOD_CONTROL,‘S’);
//收到数据处理
switch(uMsg)
{
case WM_HOTKEY:
{
if ((GetKeyState(VK_SHIFT)<0) && (GetKeyState(VK_CONTROL)<0) && (HIWORD(lParam) == 'S'))
{
//做事情
}
}
break;
}