在程序没有获得焦点的情况下,不使用HOOK如何得知用户是否按下回车键?

解决方案 »

  1.   

    用RegisterHotKey:unit Unit1;interfaceuses
      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
        { Private declarations }
      public
        { Public declarations }
        id: Integer;
        procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.WMHotKey (var Msg : TWMHotKey);
    begin
      if Msg.HotKey = id then begin
        ShowMessage('按了回车键!');
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      if GlobalFindAtom('MyHotkey') = 0 then begin
        id := GlobalAddAtom('MyHotkey');
        RegisterHotKey(Handle, id, 0, VK_RETURN);
      end;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      UnRegisterHotKey(Handle,id);
      GlobalDeleteAtom(id);
    end;end.