RegisterHotKey的具体使用方法

用到3个API函数

BOOL RegisterHotKey(
HWND hWnd,  //响应该热键的窗口句柄
Int id,      //该热键的唯一标识
UINT fsModifiers, //该热键的辅助按键
UINT vk    //该热键的键值
);

为了得到唯一标识,我们还将用到另一个API函数

ATOM GlobalAddAtom(

LPCTSTR lpString //自己设定的一个字符串

);

因为我们还要在程序退出的时候,消除这个热键, 所以需要声明一个全局变量:

HotKeyId: Integer;

第一步:

在窗口的create事件中,加入以下代码

HotKeyId := GlobalAddAtom(‘MyHotKey’) - $C000;
注: HotKeyId的合法取之范围是0x0000到0xBFFF之间, GlobalAddAtom函数得到的值
在0xC000到0xFFFF之间,所以减掉0xC000来满足调用要求。

第二步:

在上面的代码下面加入:

RegisterHotKey(Handle, hotkeyid, MOD_ALT, VK_F8); 

热键的辅助按键包括Mod_Ctrl 、Mod_Alt、Mod_Shift,对于Windows兼容键盘还支持Windows
键,即其键面上有Windows标志的那个键,其值为Mod_win。

上面 的代码注册了一个热键:ALT+F8。当然如果你希望象TAKEIT那样,只用F8, 就这么写:

RegisterHotKey(Handle, hotkeyid, 0, VK_F8); 

注:handle是一个特殊的变量,它表示当前窗口的句柄。

这个函数你应该能句举一反三了吧。

原理:

一旦热键设置成功,在程序应用过程中如果有相应的键被按下,Windows系统都会给你的应
用程序发送一个消息WM_HOTKEY,不管你的应用程序是否为当前活动的。其中WM_HOTKEY消
息的格式为: 

idHotKey = (int) wParam; // 该参数在设置系统级的热键有用,一般不予使用
fuModifiers = (UINT) LOWORD(lParam); //热键的辅助按键
uVirtKey = (UINT) HIWORD(lParam); //热键的键值

第三步:

注册了热键,就该写下响应代码了。

首先,在程序头部分的private段中加入声明 (作用是声明这个过程,和声明变量类似。
关于如何声明函数、过程,请请参考各自的帮助文件或其它资料):

procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY;

然后在程序中加入如下代码:

procedure Tfmain.HotKeyDown(var Msg: Tmessage); 

begin 

if (Msg.LparamLo = MOD_ALT) AND Msg.LParamHi = VK_F8 then // 假设热键为ALT+F8 

begin 

end; 

end;

最后一步:

在窗口的close事件中加入

UnRegisterHotKey(handle, HotKeyId); //注销HotKey, 释放资源。
-----------------------------------------------------------------------------------------------

一个实际的例子!

  
    
type
TForm1
= class (TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
protected
procedure hotykey( var msg:TMessage); message WM_HOTKEY;
end ;

var
Form1: TForm1;
id,id2:Integer;

implementation

{ $R *.DFM }


procedure TForm1.hotykey( var msg:TMessage);
begin
if (msg.LParamLo = MOD_CONTROL) and (msg.LParamHi = 81 ) then
begin
ShowMessage(
' Ctrl + Q ' );
end ;

if (msg.LParamLo = MOD_CONTROL) and (msg.LParamHi = 82 ) then
begin
ShowMessage(
' Ctrl + R ' );
end ;
end ;


procedure TForm1.FormCreate(Sender: TObject);
begin
id:
= GlobalAddAtom( ' hotkey ' );
ReGISterHotKey(handle,id,
mod _control, 81 );

id2:
= GlobalAddAtom( ' hotkey2 ' );
RegisterHotKey(handle,id2,
mod _control, 82 );
end ;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(handle,id);
UnRegisterHotKey(handle,id2);
end ;

附虚键码:

 

代码
   
     
Virtual Key Code Corresponding key

VK_LBUTTON
Left mouse button
VK_RBUTTON
Right mouse button
VK_CANCEL Control
+ Break
VK_MBUTTON Middle mouse button
VK_BACK Backspace key
VK_TAB Tab key
VK_CLEAR Clear key
VK_RETURN Enter key
VK_SHIFT Shift key
VK_CONTROL Ctrl key
VK_MENU Alt key
VK_PAUSE Pause key
VK_CAPITAL Caps Lock key
VK_KANA Used
with IME
VK_HANGUL Used
with IME
VK_JUNJA Used
with IME
VK_FINAL Used
with IME
VK_HANJA Used
with IME
VK_KANJI Used
with IME
VK_CONVERT Used
with IME

VK_NONCONVERT Used
with IME
VK_ACCEPT Used
with IME
VK_MODECHANGE Used
with IME
VK_ESCAPE Esc key
VK_SPACE
Space bar
VK_PRIOR Page Up key
VK_NEXT Page Down key
VK_END
End key
VK_HOME Home key
VK_LEFT
Left Arrow key
VK_UP Up Arrow key
VK_RIGHT
Right Arrow key
VK_DOWN Down Arrow key
VK_SELECT
Select key
VK_PRINT Print key (keyboard
- specific)
VK_EXECUTE
Execute key
VK_SNAPSHOT Print Screen key
VK_INSERT Insert key
VK_DELETE Delete key
VK_HELP Help key

VK_LWIN
Left Windows key (Microsoft keyboard)
VK_RWIN
Right Windows key (Microsoft keyboard)
VK_APPS Applications key (Microsoft keyboard)
VK_NUMPAD0
0 key (numeric keypad)
VK_NUMPAD1
1 key (numeric keypad)
VK_NUMPAD2
2 key (numeric keypad)
VK_NUMPAD3
3 key (numeric keypad)
VK_NUMPAD4
4 key (numeric keypad)
VK_NUMPAD5
5 key (numeric keypad)
VK_NUMPAD6
6 key (numeric keypad)
VK_NUMPAD7
7 key (numeric keypad)
VK_NUMPAD8
8 key (numeric keypad)
VK_NUMPAD9
9 key (numeric keypad)

VK_MULTIPLY Multiply key (numeric keypad)
VK_ADD Add key (numeric keypad)
VK_SEPARATOR Separator key (numeric keypad)
VK_SUBTRACT Subtract key (numeric keypad)
VK_DECIMAL Decimal key (numeric keypad)
VK_DIVIDE Divide key (numeric keypad)
VK_F1 F1 key
VK_F2 F2 key
VK_F3 F3 key
VK_F4 F4 key
VK_F5 F5 key
VK_F6 F6 key
VK_F7 F7 key
VK_F8 F8 key
VK_F9 F9 key
VK_F10 F10 key
VK_F11 F11 key
VK_F12 F12 key
VK_F13 F13 key
VK_F14 F14 key
VK_F15 F15 key

VK_F16 F16 key
VK_F17 F17 key
VK_F18 F18 key
VK_F19 F19 key
VK_F20 F20 key
VK_F21 F21 key
VK_F22 F22 key
VK_F23 F23 key
VK_F24 F24 key
VK_NUMLOCK Num Lock key
VK_SCROLL Scroll Lock key
VK_LSHIFT
Left Shift key (only used with GetAsyncKeyState and GetKeyState)
VK_RSHIFT
Right Shift key (only used with GetAsyncKeyState and GetKeyState)
VK_LCONTROL
Left Ctrl key (only used with GetAsyncKeyState and GetKeyState)
VK_RCONTROL
Right Ctrl key (only used with GetAsyncKeyState and GetKeyState)

VK_LMENU
Left Alt key (only used with GetAsyncKeyState and GetKeyState)
VK_RMENU
Right Alt key (only used with GetAsyncKeyState and GetKeyState)
VK_PROCESSKEY Process key
VK_ATTN Attn key
VK_CRSEL CrSel key
VK_EXSEL ExSel key
VK_EREOF
Erase EOF key
VK_PLAY Play key
VK_ZOOM Zoom key
VK_NONAME Reserved
for future use
VK_PA1 PA1 key
VK_OEM_CLEAR Clear key

 

你可能感兴趣的:(key)