Delphi 全局热键KeyPress 和 热键 API(RegisterHotKey、UnRegisterHotKey、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom)
Delphi 全局热键KeyPress 和 热键 API(RegisterHotKey、UnRegisterHotKey、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom)
1、热键按作用分为:全局、局部、系统级
- 全局和局部的:主窗体可设置KeyPress属性监控 ;
- ?1
self
.
KeyPreview:=
true
;
- 系统级:需要用到win API函数:RegisterHotKey、UnRegisterHotKey、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom
2、热键API(RegisterHotKey、UnRegisterHotKey、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom)
RegisterHotKey 原型:
?
1 2 3 4 5 6
|
BOOL RegisterHotKey( HWND hWnd, //响应该热键的窗口句柄 Int id, //该热键的唯一标识 UINT fsModifiers, //该热键的辅助按键 UINT vk //该热键的键值 ); |
fsModifiers 辅助按键的取值:
- MOD_ALT //Either ALT key must be held down.
- MOD_CONTROL //Either CTRL key must be held down.
- MOD_NOREPEAT //Changes the hotkey behavior so that the keyboard auto-repeat does not yield multiple hotkey notifications. Windows Vista: This flag is not supported.
- MOD_SHIFT //Either SHIFT key must be held down.
- MOD_WIN //Either WINDOWS key was held down.
GlobalAddAtom 原型:
?
1 2 3
|
ATOM GlobalAddAtom( LPCTSTR lpString //增加一个字符串到全局原子列表中,并返回一个唯一标识值。 ); |
GlobalFindAtom 原型:
?
1 2 3
|
ATOM GlobalFindAtom( LPCTSTR lpString // 在全局原子列表中查找是否存在指定字符串 ); |
返回值: 如果在全局原子中存在要查找的字符串,则返回此字符串对应的ID值。没有找到则返回0。
3、Delphi 示例:
?
1 2 3 4 5 6
|
HotKeyId: Integer ; //声明一个全局变量 HotKeyId := GlobalAddAtom( 'MyHotKey' ) //取得唯一标识ID RegisterHotKey(Handle, hotkeyid, MOD_ALT, VK_F9); //注册ALT+F9热键 RegisterHotKey(Handle, hotkeyid, 0 , VK_F9); //注册F9热键 UnRegisterHotKey(handle, HotKeyId); //注销HotKey, 释放资源。 GlobalDeleteAtom( 'MyHotKey' ); // |
?
1 2 3 4 5 6 7 8 9
|
procedure HotKeyDown( var Msg: Tmessage); message WM_HOTKEY; //声明 procedure TForm1 . HotKeyDown( var Msg: Tmessage); begin if (Msg . LparamLo = MOD_ALT) AND (Msg . LParamHi = VK_F9 then ) // 假设热键为 ALT+F9 begin //事件 end ; end ; |
4、注意事项:
当其他程序先注册了系统级的热键,则再注册热键无效。
赞 (0)