注册全局热键






注册全局热键(实例热键显示和隐藏窗体) ctrl+alt+a
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure HotKeyDown( var Msg: TMessage); message WM_HOTKEY;
  public
    { Public declarations }
    HotKeyId: Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
 


procedure TForm1.HotKeyDown( var Msg: TMessage);
begin
  {
    Alt键 MOD_ALT
    Ctrl键 MOD_CONTROL
    Shift键 MOD_SHIFT
    其他更多键在输入时代码会有提示
    }

  if (Msg.LparamLo = MOD_ALT + MOD_CONTROL) and(Msg.LParamHi = VkKeyScan( 'a')) then
        Visible := not Visible;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  HotKeyId := GlobalAddAtom( 'MyHotKey');
  RegisterHotKey(Handle, HotKeyId,  MOD_ALT + MOD_CONTROL, VkKeyScan( 'a')); { Ctrl+Alt+a }
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnRegisterHotKey(Handle, HotKeyId); // 注销HotKey, 释放资源。
  DeleteAtom(HotKeyId);
end;

end.




附件列表

     

    你可能感兴趣的:(注册)