unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
  procedure   hotykey(var   Msg:   Tmessage);   message   WM_HOTKEY;
    { Private declarations }
  public
  {theHotKey: Integer;
  procedure r_hyg;
  procedure r_hyb; }
    { Public declarations }
  end;var
  Form1: TForm1;
  id,id2:Integer;implementation{$R *.dfm}
  procedure   TForm1.hotykey(var   msg:TMessage);
  begin     
  if   (msg.LParamLo=MOD_CONTROL)   and   (msg.LParamHi=81)   then
  begin     
  Button1.Click;
  end;     
    
  if   (msg.LParamLo=MOD_CONTROL)   and   (msg.LParamHi=82)   then
  begin     
  Button2.Click;
  end;     
  end; 
{procedure TForm1.HotKeyDown(var Msg: Tmessage);
begin if Msg.LParamHi = 77 then  //按下小键盘上的“M”
  begin
    r_hyg;
    exit;
  end;
   if Msg.LParamHi = 78 then  //按下小键盘上的“M”
  begin
    r_hyb;
    exit;
  end;end;  }
 {procedure TForm1.r_hyg;
begin
Timer1.Enabled:= True;
end;
procedure TForm1.r_hyb;
begin
Timer1.Enabled:= False;
end;  }
procedure TForm1.Timer1Timer(Sender: TObject);
begin
SetCursorPos(20,132);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:= True;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled:= False;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  id:=GlobalAddAtom('hotkey');
  RegisterHotKey(handle,id,mod_control,81);  id2:=GlobalAddAtom('hotkey2');
  RegisterHotKey(handle,id2,mod_control,82);
  end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotKey(handle,id);
  UnRegisterHotKey(handle,id2);
end;end.
这是完整的代码..编译的话 只需要在窗体上加一个TIMER控件 两个按钮控件就可以编译..麻烦高手帮看一下..错在什么位置.. 为什么不响应热键的注册 ,怎么修改才能响应呢?

解决方案 »

  1.   

    给你写了个DEMO, Ctrl + Aunit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
      public
        procedure HotKeyDown(var Msg:Tmessage); message WM_HOTKEY;
        { Public declarations }
      end;var
      Form1: TForm1;
      HotKeyId: ATOM;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      HotKeyId := GlobalAddAtom('MyHotKey')-$C000;
      RegisterHotKey(Handle, hotkeyid, MOD_CONTROL, 65);
    end;procedure TForm1.HotKeyDown(var Msg: Tmessage);
    begin
      if(Msg.LparamLo = MOD_CONTROL) and (Msg.LParamHi = 65) then
        ShowMessage('YES');
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      UnRegisterHotKey(handle, HotKeyId);
    end;end.
      

  2.   

    楼上的例子我写过..我原来的代码就是那样的.但是无法注册热键..不知道什么原因..
    我复制你的代码编译之后.没有弹出YES的字符串..我以为是笔记本的原因.就换了一个U口键盘.
    但是还是无法响应快捷键..希望高手解惑
      

  3.   

    楼主的代码就是照书上写的吧。我测试楼主的代码,窗口激活的情况下,正常运行,热键功能正常;
    窗口最小化或者是处于未激活状态,热键也正常。看了二楼的代码,
    HotKeyId := GlobalAddAtom('MyHotKey')-$C000;
    这个红色的部分是因为 HotKeyId的合法取之范围是0x0000到0xBFFF之间,GlobalAddAtom函数得到的值在0xC000到0xFFFF之间,所以减掉0xC000来满足调用要求。
      

  4.   

    你看下是不是别的软件有冲突 RegisterHotKey返回值
      

  5.   

    GlobalAddAtom('MyHotKey')-$C000;换个名字试试,找个不容易冲突的名字,另外检查一下GlobalAddAtom的函数返回值。