自动注册全局热键

  • A+
所属分类:[开发技巧]

unit Unit1;

interface

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

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
procedure ShowWindow;
procedure HotKey(var Msg: TMessage); Message WM_HOTKEY;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
var
Aatoms: array[0..25] of ATOM;
const
Awords: array[0..25] of char = ('A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z');

procedure TForm1.HotKey(var Msg: TMessage);
var
I: Integer;
begin
for I := Low(Aatoms) to High(Aatoms) do
if (Msg.LParamHi = Word(aWords[I])) and (Msg.LParamLo = MOD_CONTROL + MOD_SHIFT) then
begin
ShowWindow;
Break;
end;

end;

procedure TForm1.ShowWindow;
begin
if Showing then
Hide
else
begin
Show;
Application.Restore;
SetForegroundWindow(Handle);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
begin
for I := Low(Aatoms) to High(Aatoms) do
begin
Aatoms[I] := GlobalFindAtom(pchar('Aatom' + IntToStr(I)));
if Aatoms[I] = 0 then
begin
Aatoms[I] := GlobalAddAtom(Pchar('Aatom' + IntToStr(I)));
RegisterHotKey(Handle, Aatoms[I], MOD_CONTROL + MOD_SHIFT, Word(Awords[I]));
Caption := 'GlobalHotKey is: Ctrl+Shift+' + Awords[I];
Break;
end;
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
I: Integer;
begin
for I := Low(Aatoms) to High(Aatoms) do
begin
Aatoms[I] := GlobalFindAtom(pchar('Aatom' + IntToStr(I)));
if Aatoms[I] <> 0 then
begin
UnRegisterHotKey(Handle, Aatoms[I]);
GlobalDeleteAtom(Aatoms[I]);
end;
end;

end;

end.

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: