用THotKey控件设置全局热键

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    HotKey1: THotKey;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
        procedure wmhotkey(var Msg: TMessage); message WM_HOTKEY;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   Key, Shift: Word; { TODO : 热键 }
implementation

{$R *.dfm}
function   ShiftStateToWord(Shift:TShiftState):Word;
begin
  Result:= 0;   // 重要,网上的别的代码这儿没设置
    if   ssShift   in   Shift   then     Result   :=MOD_SHIFT;
    if   ssCtrl   in   Shift   then   Result   :=Result   or   MOD_CONTROL;
    if   ssAlt   in   Shift   then   Result:=Result   or   MOD_ALT;
end;

procedure ShortCutToKey(ShortCut: TShortCut; var Key: Word; var Shift: TShiftState);
begin
  Key := ShortCut and not (scShift + scCtrl + scAlt);
  Shift := [];
  if ShortCut and scShift <> 0 then Include(Shift, ssShift);
  if ShortCut and scCtrl <> 0 then Include(Shift, ssCtrl);
  if ShortCut and scAlt <> 0 then Include(Shift, ssAlt);
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
var
  T: TShiftState;
begin
  ShortCutToKey(HotKey1.HotKey, Key, T);
  Shift := ShiftStateToWord(T);
  RegisterHotKey(Handle, GlobalAddAtom('MyHotKey') - $C000, Shift, Key);
end;

procedure TForm1.wmhotkey(var Msg: TMessage);
begin
  if (Msg.LparamLo = Shift) and (Msg.LParamHi = Key) then
    ShowMessage('OK ');

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  RegisterHotKey(Handle, GlobalAddAtom('hotkey'), MOD_WIN, VK_SPACE); //space

end;

end.

转载于:https://www.cnblogs.com/luckyso999/archive/2011/11/08/2240932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值