首页  编辑  

响应热键

Tags: /超级猛料/API.Windows应用程序接口/消息和事件相关/   Date Created:
RegisterHotKey函数原型及说明:
BOOL RegisterHotKey(
 HWND hWnd,         // window to receive hot-key notification
 int id,            // identifier of hot key
 UINT fsModifiers,  // key-modifier flags
 UINT vk            // virtual-key code);
参数 id为你自己定义的一个ID值,对一个线程来讲其值必需在0x0000 - 0xBFFF范围之内,对DLL来讲其值必需在0xC000 - 0xFFFF 范围之内,在同一进程内该值必须唯一
参数 fsModifiers指明与热键联合使用按键,可取值为:MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT
参数 vk指明热键的虚拟键码
首先(举个例子):  
 RegisterHotKey(handle,globaladdatom('hot key'),MOD_ALT,vk_f12);
然后在form中声明一个函数(过程):
 procedure hotkey(var msg:tmessage);message wm_hotkey;
过程如下:
procedure TForm1.hotkey(var msg:tmessage);
begin
 if (msg.LParamHi=VK_F12) and (msg.LParamLo=MOD_ALT) then
 begin
  form1.show;
  SetForegroundWindow(handle);
 end;
end;
这样,不管你在什么地方,窗口就会显示出来。
当然,你要GlobalDeleteAtom;
unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
    TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
    private
        { Private declarations }
        aatom: atom;
        procedure hotkey(var msg: tmessage); message wm_hotkey;
    public
        { Public declarations }
    end;

var
    Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
    aatom := globaladdatom('hot key');
    RegisterHotKey(handle, aatom, MOD_ALT, vk_f12);
end;

procedure TForm1.hotkey(var msg: tmessage);
begin
    if (msg.LParamHi = vk_f12) and (msg.LParamLo = MOD_ALT) then
        SetForegroundWindow(handle);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    globalDeleteatom(aatom);
end;

end.