VC如何注册、屏蔽全局键盘热键

 VC如何注册、屏蔽全局键盘热键

1、定义按键消息热键的宏,后续可自行添加更多

/************************************************************************/  
// VirtualKey.H     By:Koma 2009.08.22  
// 定义热键  
// http://blog.csdn.net/wangningyu  
  
/************************************************************************/  
/* Ctrl组合键 (D、F2、F8、F9、F10、F11、F12              共6个) 
/************************************************************************/  
#define IDH_HOT1        4001  
#define IDH_HOT2        4002  
#define IDH_HOT3        4003  
#define IDH_HOT4        4004  
#define IDH_HOT5        4005  
#define IDH_HOT6        4006  
#define IDH_HOT19       4019  
  
/************************************************************************/  
/* Alt组合键 (F1、F4、F9                             共3个) 
/************************************************************************/  
#define IDH_HOT7        4007  
#define IDH_HOT8        4008  
#define IDH_HOT9        4009  
  
/************************************************************************/

2、注册全局的热键,从而实现屏蔽热键

/************************************************************************/    
/* 函数说明:MFC初始化函数,用来注册全局热键                                        
/* 参    数:无                                    
/* 返 回 值:成功返回TRUE、失败返回FALSE     
/* By:Koma   2009.07.30 17:50                                 
/************************************************************************/   
BOOL CHookKBDlg::OnInitDialog()  
{  
    CDialog::OnInitDialog();  
    // 如果需要添加其他的组合键,需要在VirtualKey.h添加记录  
    // 在OnInitDialog、PreTranslateMessage中各添加一行代码即可  
  
    // Ctrl组合键 (D、F2、F8、F9、F10、F11、F12              共7个)  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT1, MOD_CONTROL, VK_F2);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT2, MOD_CONTROL, VK_F8);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT3, MOD_CONTROL, VK_F9);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT4, MOD_CONTROL, VK_F10);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT5, MOD_CONTROL, VK_F11);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT6, MOD_CONTROL, VK_F12);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT19, MOD_CONTROL, 'D');  
  
    // Alt组合键 (F1、F4、F9                             共3个)  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT7, MOD_ALT, VK_F1);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT8, MOD_ALT, VK_F4);  
    RegisterHotKey(this->GetSafeHwnd(),IDH_HOT9, MOD_ALT, VK_F9);  
  
    return TRUE;  // return TRUE  unless you set the focus to a control  
}

3、有点像"占着茅坑不拉S”,直接过滤掉此消息...

/************************************************************************/    
/* 函数说明:MFC虚函数,用来屏蔽热键                                        
/* 参    数:无                                    
/* 返 回 值:屏蔽返回TRUE、不屏蔽返回FALSE     
/* By:Koma   2009.07.30 17:50                                 
/************************************************************************/    
BOOL CHookKBDlg::PreTranslateMessage(MSG* pMsg)   
{  
    // TODO: Add your specialized code here and/or call the base class  
    if(pMsg->message==WM_HOTKEY)    
    {     
        switch(pMsg->wParam)  
        {  
        case IDH_HOT1:  
        case IDH_HOT2:  
        case IDH_HOT3:  
        case IDH_HOT4:  
        case IDH_HOT5:  
        case IDH_HOT6:  
        case IDH_HOT7:  
        case IDH_HOT8:  
        case IDH_HOT9:  
            // MessageBox(_T("此类组合键严禁使用!"));  
            // true 标示处理过此消息不再投递到消息队列,  
            SetDlgItemText(IDC_STATIC1,"别按了,没用的!");  
            return TRUE;  
        }  
    }     
    return CDialog::PreTranslateMessage(pMsg);  
}
注意:   取消注册热键用 UnregisterHotKey(......)

 ------------------------------------------

MFC 热键设置 OnHotKey方法和Accelerator方法的设置

在写MFC程序时,如果想自定义热键比如ALT+S可以采用下面两种方法:

方法一:定义热键的消息响应函数:OnHotKey
定义系统全局热键:

1、首先在.h文件中添加消息响应函数声明
  afx_msg LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);
2、关联消息及响应函数
      BEGIN_MESSAGE_MAP()
   ON_MESSAGE(WM_HOTKEY,OnHotKey)
END_MESSAGE_MAP()

3、在初始化函数里注册热键
RegisterHotKey(m_hWnd,ID_HOTKEY,MOD_ALT,‘s’);  
// ID_HOTKEY是自己定义的一个ID宏
‘s'可以写成ASCII码

4、在消息响应函数里添加操作
LRESULT  CMyTest::OnHotKey(WPARAM wParam,LPARAM lParam) 
 { 
 UINT Mod = (UINT) LOWORD(lParam); // key-modifier flags 
 UINT uVirtKey = (UINT) HIWORD(lParam); // virtual-key code 
 //判断响应了什么热键 
 if( MOD_CONTROL ==  Mod  && ‘s’ == uVirtKey ) 
 { 
 AfxMessageBox(_T("你按下了组合键 ALT+ S"));
 } 
 else 
 AfxMessageBox(_T("你按下了未知热键")); 
 return 0; 
 }
 

方法二: 
Accelerator方法 只在本应用程序内有效
http://support.microsoft.com/?kbid=222829  
1、插入一个新的Accelerator到资源里,把加速键和对应的响应控件(如一个按钮)关联 
2、在对话框头文件中声明: 
     HACCEL   m_hAccel; 
3、在对话框的构造函数里初始化m_hAccel 
    m_hAccel   =   ::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1)); 
4、然后重载对话框的PreTranslateMessage函数,在 
BOOL   CAboutDlg::PreTranslateMessage(MSG*   pMsg)   

      if   (m_hAccel)   
      { 
            if   (::TranslateAccelerator(m_hWnd,   m_hAccel,   pMsg))   
            { 
                  return(TRUE); 
            } 
      } 
      return   CDialog::PreTranslateMessage(pMsg); 


然后重载 OnCommand();函数;
BOOL CFreeChatDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
if (ID_ALT_S == LOWORD(wParam))//ID_ALT_S是在 Accelerator中注册快捷键的ID
{
//加入响应的快捷键的操作
}
return CDialog::OnCommand(wParam,lParam);
}


希望能帮你少走点弯路^^

注意:   取消注册热键用 UnregisterHotKey(......)




转载于:https://my.oschina.net/ypimgt/blog/101084

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MICROSOFT FOUNDATION CLASS LIBRARY : ShieldPower AppWizard has created this ShieldPower application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your ShieldPower application. ShieldPower.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. ShieldPower.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CShieldPowerApp application class. ShieldPower.cpp This is the main application source file that contains the application class CShieldPowerApp. ShieldPower.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. ShieldPower.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\ShieldPower.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file ShieldPower.rc. res\ShieldPower.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// AppWizard creates one dialog class: ShieldPowerDlg.h, ShieldPowerDlg.cpp - the dialog These files contain your CShieldPowerDlg class. This class defines the behavior of your application's main dialog. The dialog's template is in ShieldPower.rc, which can be edited in Microsoft Visual C++. ///////////////////////////////////////////////////////////////////////////// Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named ShieldPower.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Visual C++ reads and updates this file. ///////////////////////////////////////////////////////////////////////////// Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system's current language, you will need to copy the corresponding localized resources MFC42XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC42DEU.DLL contains resources translated to German.) If you don't do this, some of the UI elements of your application will remain in the language of the operating system. /////////////////////////////////////////////////////////////////////////////
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值