Delphi 注册快捷键
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, HTTPApp; type TForm1 = class(TForm) btn1: TButton; edt1: TEdit; edt2: TEdit; procedure btn1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY; //声明 procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; HotKeyId_ALT_F9,HotKeyId_F9: Integer; //声明一个全局变量 implementation {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); var lv_sMsg: string; begin lv_sMsg := Trim(edt1.Text); edt2.Text := HttpEncode(UTF8Encode(lv_sMsg)); end; procedure TForm1.FormCreate(Sender: TObject); begin //注: HotKeyId的合法取之范围是0x0000到0xBFFF之间, GlobalAddAtom函数得到的值 //在0xC000到0xFFFF之间,所以减掉0xC000来满足调用要求。 HotKeyId_ALT_F9 := GlobalAddAtom('HotKeyId_ALT_F9') - $C000; //取得唯一标识ID RegisterHotKey(Handle, HotKeyId_ALT_F9, MOD_ALT, VK_F9); //注册ALT+F9热键 HotKeyId_F9 := GlobalAddAtom('HotKeyId_F9') - $C000; //取得唯一标识ID RegisterHotKey(Handle, HotKeyId_F9, 0, VK_F9); //注册F9热键 end; procedure TForm1.HotKeyDown(var Msg: Tmessage); begin if (Msg.LparamLo = MOD_ALT) AND (Msg.LParamHi = VK_F9) then // 假设热键为 ALT+F9 begin //事件 ShowMessage('按压ALT+F9'); end; if (Msg.LparamLo = 0) AND (Msg.LParamHi = VK_F9) then // 假设热键为 ALT+F9 begin //事件 ShowMessage('按压F9'); end; end; procedure TForm1.FormDestroy(Sender: TObject); var m,n: integer; s1,s2: string; begin UnRegisterHotKey(handle, HotKeyId_ALT_F9); //注销HotKey, 释放资源。 UnRegisterHotKey(handle, HotKeyId_F9); m := GlobalFindAtom('HotKeyId_ALT_9'); s1 := IntToStr(m); n := GlobalDeleteAtom(8); s2 := IntToStr(n); end; end.