函数功能:该函数定义一个系统范围的热键。
函数原型:BOOL RegisterHotKey(HWND hWnd,intid,UINT fsModifiers,UINT vk);
参数说明:
hWnd:接收热键产生WM_HOTKEY消息的窗口句柄。
registerhotkey
若该参数NULL,传递给调用线程的WM_HOTKEY消息必须在消息循环中中进行处理。
id:定义热键的标识符。调用线程中的其他热键不能使用同样的标识符。应用功能程序必须定义一个0X0000-0xBFFF范围的值。一个共享的动态链接库(DLL)必须定义一个0xC000-0xFFFF范围的值伯GlobalAddAtom函数返回该范围)。为了避免与其他动态链接库定义的热键冲突,一个DLL必须使用GlobalAddAtom函数获得热键的标识符。
fsModifoers:定义为了产生WM_HOTKEY消息而必须与由nVirtKey参数定义的键一起按下的键。该参数可以是如下值的组合:
MOD_ALT:按下的可以是任一Alt键。MOD_CONTROL:按下的可以是任一Ctrl键。
MOD_SHIFT:按下的可以是任一Shift键。
MOD_WIN:按下的可以是任一Windows按键。这些键可以用Microsoft Windows日志记录下来。
vk:定义热键的虚拟键码。
返回值:若函数调用成功,返回一个非O值。若函数调用失败,则返回值为0。若要获得更多的错误信息,可以调用GetLastError函数。
备注:当某键被接下时,系统在所有的热键中寻找匹配者。一旦找到一个匹配的热键,系统将把WM_HOTKEY消息传递给登记了该热键的线程的消息队列。该消息被传送到队列头部,因此它将在下一轮消息循环中被移去。该函数不能将热键同其他线程创建的窗口关联起
若为一热键定义的击键己被其他热键所定义,则RegisterHotKey函数调用失败。
registerhotkey
若hWnd参数标识的窗口已用与id参数定义的相同的标识符登记了一个热键,则参数fsModifiers和vk的新值将替代这些参数先前定义的值。
Windows CE:Windows CE 2.0以上版本对于参数fsModifiers支持一个附加的标志位。叫做MOD_KEYUP。
若设置MOD_KEYUP位,则当发生键被按下或被弹起的事件时,窗口将发送WM_HOTKEY消息。
RegisterHotKey可以被用来程之间登记热键。
速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:不支持;头文件:winuser.h;库文件:user32.lib。
UnregisterHotKey()
===========================
函数功能:该函数在系统中注消热键。
函数原型:BOOL UnregisterHotKey( HWND hWnd, int id);
参数:该函数的参数和 RegisiterHotKey 的那两个参数意义大体相同。
UnitFormMain.h
private:
ATOM HotKeyStart, HotKeyEnd;
void __fastcall WndProc(Messages::TMessage &Message);
UnitFormMain.cpp
void __fastcall TFormMain::FormCreate(TObject *Sender)
HotKeyStart = GlobalAddAtom("HotKeyStart");
HotKeyEnd = GlobalAddAtom("HotKeyEnd");
RegisterHotKey(Handle, HotKeyStart, MOD_CONTROL, VK_F2);
RegisterHotKey(Handle, HotKeyEnd, MOD_CONTROL, VK_F3);
void __fastcall TFormMain::WndProc(Messages::TMessage &Message)
if (Message.Msg == WM_HOTKEY)
if (Message.LParamHi == VK_F2)
Start();
if (Message.LParamHi == VK_F3)
End();
TForm::WndProc(Message);
void __fastcall TFormMain::FormDestroy(TObject *Sender)
if(HotKeyStart) UnregisterHotKey(Handle, HotKeyStart); GlobalDeleteAtom(HotKeyStart);
if(HotKeyEnd) UnregisterHotKey(Handle, HotKeyEnd); GlobalDeleteAtom(HotKeyEnd);
demo2:
void __fastcall TForm1::FormCreate(TObject *Sender)
int tid;
strList=new TStringList();
tid=GlobalFindAtom(AnsiString("MyHotkey").c_str());
if(tid==0)
id=GlobalAddAtom(AnsiString("MyHotkey").c_str());
id=tid;
strList->Add(IntToStr(id));
RegisterHotKey(Handle, id, MOD_CONTROL, VK_F1);
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
UnregisterHotKey(Handle,StrToInt(strList->Strings[0]));
GlobalDeleteAtom(StrToInt(strList->Strings[0]));
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
if(Msg.message==WM_HOTKEY && ((int)Msg.wParam)==StrToInt(strList->Strings[0]))
//SetForegroundWindow(Handle);
//ShowMessage("Ok");
this->Visible=!this->Visible;
最近我也刚试了热键
Ahift-ctrl-alt-<space>
Ahift-ctrl-alt-<return>
Ahift-ctrl-alt-<f1>
// 一、定义热键
#define HotkeyID1 0xb123
#define HotkeyID2 0xb124
#define HotkeyID3 0xb125
RegisterHotKey(Handle,HotkeyID1,
MOD_ALT+MOD_CONTROL+MOD_SHIFT,
VK_RETURN);
RegisterHotKey(Handle,HotkeyID2,
MOD_ALT+MOD_CONTROL+MOD_SHIFT,
VK_SPACE);
RegisterHotKey(Handle,HotkeyID3,
MOD_ALT+MOD_CONTROL+MOD_SHIFT,
VK_F1);
// 二、得到热键通知
void __fastcall TForm1::WndProc(Messages::TMessage &Message)
if (Message.Msg==WM_HOTKEY) // LParamHi: VirtualKey
{ // LParamLo: key-modifier
if (Message.LParamHi==VK_RETURN)
;// 热键ID1处理
if (Message.LParamHi==VK_SPACE)
;// 热键ID1处理
if (Message.LParamHi==VK_F1)
;// 热键ID1处理
TForm::WndProc(Message);
// 三、注销热键
UnregisterHotKey(Handle,HotkeyID1);
UnregisterHotKey(Handle,HotkeyID2);
UnregisterHotKey(Handle,HotkeyID3);
demo3:
unit Unit1
interface
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ComCtrls
TForm1 = class(TForm)
BitBtn1: TBitBtn
HotKey1: THotKey
procedure BitBtn1Click(Sender: TObject)
procedure FormCreate(Sender: TObject)
private
procedure wmhotkey(var Msg: TMessage)
{ Private declarations }
public
{ Public declarations }
end
Form1: TForm1
Key, Shift: Word
implementation
{$R *.dfm}
function ShiftStateToWord(Shift:TShiftState):Word
begin
Result:= 0
if ssShift in Shift then Result :=MOD_SHIFT
if ssCtrl in Shift then Result :=Result or MOD_CONTROL
if ssAlt in Shift then Result:=Result or MOD_ALT
end
procedure ShortCutToKey(ShortCut: TShortCut
begin
Key := ShortCut and not (scShift + scCtrl + scAlt)
Shift := []
if ShortCut and scShift <> 0 then Include(Shift, ssShift)
if ShortCut and scCtrl <> 0 then Include(Shift, ssCtrl)
if ShortCut and scAlt <> 0 then Include(Shift, ssAlt)
end
procedure TForm1.BitBtn1Click(Sender: TObject)
T: TShiftState
begin
ShortCutToKey(HotKey1.HotKey, Key, T)
Shift := ShiftStateToWord(T)
RegisterHotKey(Handle, GlobalAddAtom('MyHotKey') - $C000, Shift, Key)
end
procedure TForm1.wmhotkey(var Msg: TMessage)
begin
if (Msg.LparamLo = Shift) and (Msg.LParamHi = Key) then
ShowMessage('OK ')
end
procedure TForm1.FormCreate(Sender: TObject)
begin
RegisterHotKey(Handle, GlobalAddAtom('hotkey'), MOD_WIN, VK_SPACE)
end
demo4:
unit Unit1
interface
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls
TForm1 = class(TForm)
HotKey1: THotKey
Memo1: TMemo
CheckBox1: TCheckBox
CheckBox2: TCheckBox
CheckBox3: TCheckBox
CheckBox4: TCheckBox
procedure FormCreate(Sender: TObject)
procedure CheckBox1Click(Sender: TObject)
end
Form1: TForm1
implementation
{$R *.dfm}
procedure TForm1.CheckBox1Click(Sender: TObject)
begin
HotKey1.Modifiers := []
if CheckBox1.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkShift]
if CheckBox2.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkCtrl]
if CheckBox3.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkAlt]
if CheckBox4.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkExt]
end
procedure TForm1.FormCreate(Sender: TObject)
begin
Memo1.Clear
Memo1.Align := alTop
Memo1.ScrollBars := ssVertical
Memo1.Lines.Add('1、你现在看到的可不是 TEdit, 它是处理快捷键用的 HotKey 控件
Memo1.Lines.Add('2、激活后, 输入几个字母试试
Memo1.Lines.Add('3、按 Ctrl+*
Memo1.Lines.Add('4、按 Ctrl+Alt+*
Memo1.Lines.Add('5、按 Shift+Ctrl+* 或 Shift+Alt+* 都可以
Memo1.Lines.Add('6、按 Shift+Ctrl+Alt+* 也可以
Memo1.Lines.Add('7、但按 Shift+* 不可以
Memo1.Lines.Add('8、不过我们可以用 HotKey.Modifiers 打开它.')
CheckBox1.Caption := 'hkShift'
CheckBox2.Caption := 'hkCtrl'
CheckBox3.Caption := 'hkAlt'
CheckBox4.Caption := 'hkExt'
CheckBox1.Checked := False
CheckBox2.Checked := False
CheckBox3.Checked := True
CheckBox4.Checked := False
CheckBox2.OnClick := CheckBox1.OnClick
CheckBox3.OnClick := CheckBox1.OnClick
CheckBox4.OnClick := CheckBox1.OnClick
end
键盘VK键值列表
/* Virtual Keys, Standard Set*/
VK_LBUTTON 0x01
VK_RBUTTON 0x02
VK_CANCEL 0x03
VK_MBUTTON 0x04
#define VK_LBUTTON 0x01 //鼠标左键
#define VK_RBUTTON 0x02 //鼠标右键
#define VK_CANCEL 0x03 //Ctrl + Break
#define VK_MBUTTON 0x04 //鼠标中键/* NOT contiguous with L & RBUTTON */
#define VK_BACK 0x08 //Backspace 键
#define VK_TAB 0x09 //Tab 键
#define VK_CLEAR 0x0C
#define VK_RETURN 0x0D //回车键
#define VK_SHIFT 0x10
#define VK_CONTROL 0x11
#define VK_MENU 0x12 //Alt 键
#define VK_PAUSE 0x13
#define VK_CAPITAL 0x14 //Caps Lock 键
#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 //空格
#define VK_PRIOR 0x21 //Page Up 键
#define VK_NEXT 0x22 //Page Down 键
#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
#define VK_PRINT 0x2A
#define VK_EXECUTE 0x2B
#define VK_SNAPSHOT 0x2C //Print Screen 键
#define VK_INSERT 0x2D //Insert键
#define VK_DELETE 0x2E //Delete键
#define VK_HELP 0x2F
/* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
/* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
#define VK_LWIN 0x5B //左WinKey(104键盘才有)
#define VK_RWIN 0x5C //右WinKey(104键盘才有)
#define VK_APPS 0x5D //AppsKey(104键盘才有)
#define VK_NUMPAD0 0x60 //小键盘0-9
#define VK_NUMPAD1 0x61
#define VK_NUMPAD2 0x62
#define VK_NUMPAD3 0x63
#define VK_NUMPAD4 0x64
#define VK_NUMPAD5 0x65
#define VK_NUMPAD6 0x66
#define VK_NUMPAD7 0x67
#define VK_NUMPAD8 0x68
#define VK_NUMPAD9 0x69
#define VK_MULTIPLY 0x6A //乘
#define VK_ADD 0x6B //加
#define VK_SEPARATOR 0x6C //除
#define VK_SUBTRACT 0x6D //减
#define VK_DECIMAL 0x6E //小数点
#define VK_DIVIDE 0x6F
#define VK_F1 0x70 //功能键F1-F24
#define VK_F2 0x71
#define VK_F3 0x72
#define VK_F4 0x73
#define VK_F5 0x74
#define VK_F6 0x75
#define VK_F7 0x76
#define VK_F8 0x77
#define VK_F9 0x78
#define VK_F10 0x79
#define VK_F11 0x7A
#define VK_F12 0x7B
#define VK_F13 0x7C
#define VK_F14 0x7D
#define VK_F15 0x7E
#define VK_F16 0x7F
#define VK_F17 0x80
#define VK_F18 0x81
#define VK_F19 0x82
#define VK_F20 0x83
#define VK_F21 0x84
#define VK_F22 0x85
#define VK_F23 0x86
#define VK_F24 0x87
#define VK_NUMLOCK 0x90 //Num Lock 键
#define VK_SCROLL 0x91 //Scroll Lock 键
* 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(WINVER >= 0x0400)
#define VK_PROCESSKEY 0xE5
#endif /* WINVER >= 0x0400 */
#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
如何注册全局热键(c++builder)
这样吧,我给你一个例程,包括了ResiterHotKey的用法和消息处理的用法,你仔细看看吧。
这个程序有一个窗体Form1,Form1上注册了一个热键Ctrl+F11,以后只要这个程序在运行,不管在什么,你一按Ctrl+F11, 就会弹出一个消息框。
//Unit.H
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
//这个函数就是消息处理函数,通过VCL_MESSAGE_HANDLER宏和指定消息相连
void __fastcall WMHotKey(TMessage &Msg);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
//以下三句就是消息处理,可以在BEGIN和END之间加入多个HANDLER
BEGIN_MESSAGE_MAP
//处理WM_HOTKEY消息,注册热键之后,按下热键就会向指定窗口发出该消息
VCL_MESSAGE_HANDLER(WM_HOTKEY, TMessage, WMHotKey);
END_MESSAGE_MAP(TForm);
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//Unit1.CPP
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
//---------------------------------------------------------------------------
//这就是消息处理函数的实现
void __fastcall TForm1::WMHotKey(TMessage &Msg)
SetForegroundWindow(Handle);
ShowMessage("Hot Key Registered!");
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
RegisterHotKey(Handle, 0x1000, MOD_CONTROL, VK_F11);
//以上那句注册热键,Handle是窗口句柄,0x1000是一个ID号,设置该热键在线程中
//的唯一值,第三个参数表示要按下那些系统键,最后一个是按键的虚拟键码
//具体可参见MSDN帮助
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
UnregisterHotKey(Handle, 0x1000);
//注销热键,0x1000和上面对应,指明注销哪个热键
}