考虑没有为 VCL 消息提供窗口句柄的示例,我们使用WM_HOTKEY。此消息由 Windows 热键中的已注册窗口发送,允许程序响应它,即使没有输入焦点:
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
// Declare a event handler
procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
// Registering a hotkey Ctrl+Alt+F5
RegisterHotKey(Handle, 0, MOD_CONTROL or MOD_ALT, VK_F5);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Unregisters a hotkey
UnRegisterHotKey(Handle, 0);
end;
procedure TForm1.WMHotKey(var Msg: TWMHotKey);
begin
// This procedure is called when a window message WM_HOTKEY
inherited; // We give the form to process the message,
// if she already has its handler
Beep; // We perform additional actions
end;