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、注意事项:
当其他程序先注册了系统级的热键,则再注册热键无效。
创建时间:2020.07.28 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
2019-07-28 [原创] Java 流布局管理器 FlowLayout
2019-07-28 Java Swing 窗体屏幕居中