注册热键完善,增加容器,可以增加删除热键

来源:互联网 发布:知乎用户数量 编辑:程序博客网 时间:2024/06/11 23:54

全局变量:

typedef enum HotKeyStatus
{
WaitStatus = 0,
AddStatus = 1,
DeleteStatus = 2,
};
struct HOT_KEY
{
int key_Tag; // Tag = 1 表示注册热键 =2 表示删除,=0 表示不操作
int key_Modifier;
int key_VK;
};
std::vector<HOT_KEY> _vHotKey;
static Mutex hotkey_Mutex;

#ifndef _LOCK_H_#define _LOCK_H_#include <windows.h>  //锁接口类 class IMyLock { public: virtual ~IMyLock() {}  virtual void Lock() const = 0; virtual void Unlock() const = 0; };  //互斥对象锁类 class Mutex : public IMyLock { public: Mutex(); ~Mutex();  virtual void Lock() const; virtual void Unlock() const;  private: HANDLE m_mutex; };  //锁 class CLock { public: CLock(const IMyLock&); ~CLock();  private: const IMyLock& m_lock; }; #endif

#include "Lock.h"  //创建一个匿名互斥对象 Mutex::Mutex() { m_mutex = ::CreateMutex(NULL, FALSE, NULL); } //销毁互斥对象,释放资源 Mutex::~Mutex() { ::CloseHandle(m_mutex); }  //确保拥有互斥对象的线程对被保护资源的独自访问 void Mutex::Lock() const{ DWORD d = WaitForSingleObject(m_mutex, INFINITE); }  //释放当前线程拥有的互斥对象,以使其它线程可以拥有互斥对象,对被保护资源进行访问 void Mutex::Unlock() const{ ::ReleaseMutex(m_mutex); }  //利用C++特性,进行自动加锁 CLock::CLock(const IMyLock& m) : m_lock(m) { m_lock.Lock(); }  //利用C++特性,进行自动解锁 CLock::~CLock() { m_lock.Unlock(); } 

//fuModifiers 辅助按键 Mod_Ctrl、Mod_Alt、Mod_Shif、Mod_win//uVirtKey  快捷键// 自定义方法获取HotKeyID 保证每次获取ID值不同ATOM GetHotKeyID(int fuModifiers, int uVirtKey){int param = ((long)fuModifiers << 4)+ uVirtKey;ATOM m_HotID = param - 0xC000;return m_HotID;}// 监听到热键响应回调int AddHotKeyCallbacks(int fuModifiers, int uVirtKey){printf("热键回调开始\n");// 实现热键按下之后的响应功能printf("热键回调成功\n");return 0;}// 侦听是否按下快捷键int IntercepteHotKey(){std::vector<HOT_KEY>::iterator it;int key = 0;HWND hConsole = GetActiveWindow(); // 获取当前显示窗口的句柄(即控制台这个窗口的句柄)MSG msg = {0};while (1){     if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))        {// 当收到快捷键消息时if (msg.message == WM_HOTKEY){int msgS1 = msg.lParam;int msgS2 = msg.wParam;UINT fuModifiers = (UINT) LOWORD(msgS1);// key-modifier flags UINT uVirtKey = (UINT) HIWORD(msgS1);       // virtual-key code AddHotKeyCallbacks(fuModifiers, uVirtKey);}        TranslateMessage (&msg) ;        DispatchMessage (&msg) ;        }        else  if(!_vHotKey.empty()){hotkey_Mutex.Lock();it = _vHotKey.begin();while(it != _vHotKey.end()){int ID = GetHotKeyID((*it).key_Modifier, (*it).key_VK);if((*it).key_Tag == AddStatus){RegisterHotKey(hConsole, ID, (*it).key_Modifier, (*it).key_VK);(*it).key_Tag = WaitStatus;it++;}else if((*it).key_Tag == DeleteStatus){UnregisterHotKey(hConsole, ID);// 删除该项it = _vHotKey.erase(it);}else{it++;}}hotkey_Mutex.Unlock();} Sleep(10);}_vHotKey.clear();return 0;}// status = 1 注册 = 2 删除热键int oprationHotKey(int fuModifiers, int uVirtKey, int status){std::vector<HOT_KEY>::iterator it;HOT_KEY elem ;elem.key_Tag = status;elem.key_Modifier = fuModifiers;elem.key_VK = uVirtKey;hotkey_Mutex.Lock();if(status == AddStatus){for(it = _vHotKey.begin(); it != _vHotKey.end();it++){if((*it).key_Tag == WaitStatus){if((*it).key_Modifier == fuModifiers && (*it).key_VK == uVirtKey){printf("The HotKey is Exsit, cannot be add ! \n");return 1;}}}//遍历,不存在允许}else if(status == DeleteStatus){for(it = _vHotKey.begin(); it != _vHotKey.end();it++){if((*it).key_Tag == WaitStatus){if((*it).key_Modifier != fuModifiers || (*it).key_VK != uVirtKey){printf("The HotKey is  not Exsit, cannot be delete ! \n");return 1;}}}// 遍历,存在允许}_vHotKey.push_back(elem);hotkey_Mutex.Unlock();return 0;}// 初始化热键bool initHotKey(){//Sleep(10000);HANDLE hThread;hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)IntercepteHotKey,NULL, 0, NULL);if(hThread == NULL)return false;//CloseHandle(hThread);return true;}

先初始化,在
oprationHotKey(0,112,1)//注册F1  为热键


0 0