• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

用DELPHI如何注册系统级热键

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  HotKeyId: array[0..12] of Integer; //热键数组, 这里准备定义 13 个热键

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
//注册热键
  for i := Low(HotKeyId) to High(HotKeyId) do
    HotKeyId[i] := GlobalAddAtom(PChar(IntToStr(i))); //热键命名可随意
  RegisterHotKey(Handle,HotKeyId[0],0,VK_F2);               //F2
  RegisterHotKey(Handle,HotKeyId[1],0,VK_UP);               //Up
  RegisterHotKey(Handle,HotKeyId[2],0,VK_DOWN);             //Down
  RegisterHotKey(Handle,HotKeyId[3],0,VK_LEFT);             //Left
  RegisterHotKey(Handle,HotKeyId[4],0,VK_RIGHT);            //Right
  RegisterHotKey(Handle,HotKeyId[5],0,VK_PRIOR);            //PageUp
  RegisterHotKey(Handle,HotKeyId[6],0,VK_NEXT);             //PageDown
  RegisterHotKey(Handle,HotKeyId[7],0,VK_OEM_PLUS);         //+
  RegisterHotKey(Handle,HotKeyId[8],0,VK_OEM_MINUS);        //-
  RegisterHotKey(Handle,HotKeyId[9],0,$31);                 //1
  RegisterHotKey(Handle,HotKeyId[10],0,$41);                //a
  RegisterHotKey(Handle,HotKeyId[11],0,VK_RETURN);          //Enter
  RegisterHotKey(Handle,HotKeyId[12],MOD_CONTROL,VK_RETURN); //Ctrl+Enter
end;

//热键
procedure TForm1.WMHotKey(var Msg: TWMHotKey);
begin
  if Msg.HotKey = HotKeyId[0] then ShowMessage('F2');
  if (Msg.HotKey=HotKeyId[1]) then ShowMessage('Up');
  if (Msg.HotKey=HotKeyId[2]) then ShowMessage('Down');
  if (Msg.HotKey=HotKeyId[3]) then ShowMessage('Left');
  if (Msg.HotKey=HotKeyId[4]) then ShowMessage('Right');
  if Msg.HotKey = HotKeyId[5] then ShowMessage('PageUp');
  if Msg.HotKey = HotKeyId[6] then ShowMessage('PageDown');
  if Msg.HotKey = HotKeyId[7] then ShowMessage('+');
  if Msg.HotKey = HotKeyId[8] then ShowMessage('-');
  if Msg.HotKey = HotKeyId[9] then ShowMessage('1');
  if Msg.HotKey = HotKeyId[10] then ShowMessage('a');
  if Msg.HotKey = HotKeyId[11] then ShowMessage('Enter');
  if Msg.HotKey = HotKeyId[12] then ShowMessage('Ctrl+Enter');
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
//注销热键
  for i := Low(HotKeyId) to High(HotKeyId) do
  begin
    UnRegisterHotKey(handle,HotKeyId[i]);
    GlobalDeleteAtom(HotKeyId[i]);
  end;
end;

end.

***************************************************

这里是一些常用按键的键值

***************************************************

RegisterHotKey(Handle, hotkeyid, MOD_ALT, VK_F8); 

热键的辅助按键包括Mod_Ctrl 、Mod_Alt、Mod_Shift,对于Windows兼容键盘还支持Windows
键,即其键面上有Windows标志的那个键,其值为Mod_win。

部分热键:       

MOD_ALT      Alt   键
MOD_CONTROL Ctrl 键
MOD_SHIFT    Shift 键       

Back           008                Enter          013      

Shift          016                Ctrl           017
Alt            018                Pause          019
Caps Lock      020                Esc            027
空格键         032                Page Up        033
Page Down      034                End            035
Home           036                方向左         037
方向上         038                方向右         039
方向下         040                Insert         045
Delete         046                0 - 9          048 - 57
A - Z          065 - 090          数字键盘0-9    096 - 105
数字键盘*      106                数字键盘+      107
数字键盘-      109                数字键盘.      110
数字键盘/      111                F1 - F12       112 - 123
Num Lock       144                Soroll Lock    145
;              186                =              187
,              188                -              189
.              190                /              191
~              192                [              219
\              220                ]              221
'              222

上面 的代码注册了一个热键:ALT+F8。当然如果你希望象TAKEIT那样,只用F8, 就这么写:

RegisterHotKey(Handle, hotkeyid, 0, VK_F8); 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap