Delphi 注册快捷键

Posted studycode

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
View Code

 

以上是关于Delphi 注册快捷键的主要内容,如果未能解决你的问题,请参考以下文章

Delphi 注册快捷键

Delphi创建Windows快捷方式

Delphi创建Windows快捷方式

教程-Delphi中的GExperts搜索代码快捷键

delphi中用代码实现注册Ocx和Dll

delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)