当我的程序不活动时,如何处理键盘快捷键?

时间:2010-11-06 22:11:56

标签: delphi events shortcut taction

如果我像这样使用它,可以用于多个事件吗?

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Clipbrd;

type
  TForm4 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure WMHotkey(var Message: TWMHotKey); message WM_HOTKEY;
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

const
  MY_ID = 123;
  MY_ID1 = 123;
  MY_ID2 = 123;

{$R *.dfm}

procedure TForm4.FormCreate(Sender: TObject);
begin
  RegisterHotKey(Handle, MY_ID, MOD_CONTROL, ord('1'));
   RegisterHotKey(Handle, MY_ID1, MOD_CONTROL, ord('2'));
    RegisterHotKey(Handle, MY_ID2, MOD_CONTROL, ord('3'));
end;

procedure TForm4.FormDestroy(Sender: TObject);
begin
  UnregisterHotKey(Handle, MY_ID);
  UnregisterHotKey(Handle, MY_ID1);
  UnregisterHotKey(Handle, MY_ID2);
end;

procedure TForm4.WMHotkey(var Message: TWMHotKey);
begin
  if Message.HotKey = MY_ID then
  begin

    if not AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true) then
      RaiseLastOSError;

    try
      Clipboard.AsText := 'text1';
      SendMessage(GetFocus, WM_PASTE, 0, 0);
    finally
      AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
    end;

    if Message.HotKey = MY_ID1 then
  begin

    if not AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true) then
      RaiseLastOSError;

    try
      Clipboard.AsText := 'text2';
      SendMessage(GetFocus, WM_PASTE, 0, 0);
    finally
      AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
    end;

  if Message.HotKey = MY_ID2 then
  begin

    if not AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true) then
      RaiseLastOSError;

    try
      Clipboard.AsText := 'text3';
      SendMessage(GetFocus, WM_PASTE, 0, 0);
    finally
      AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
    end;

  end;
end;

end;
end;
end.

3 个答案:

答案 0 :(得分:5)

使用RegisterHotKey功能。如果您希望应用程序不可见,您可能需要my answer to a similar question中的所有详细信息。

试试这个:

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Clipbrd;

type
  TForm4 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure WMHotkey(var Message: TWMHotKey); message WM_HOTKEY;
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

const
  MY_ID = 123;

{$R *.dfm}

procedure TForm4.FormCreate(Sender: TObject);
begin
  RegisterHotKey(Handle, MY_ID, MOD_CONTROL, ord('1'));
end;

procedure TForm4.FormDestroy(Sender: TObject);
begin
  UnregisterHotKey(Handle, MY_ID);
end;

procedure TForm4.WMHotkey(var Message: TWMHotKey);
begin
  if Message.HotKey = MY_ID then
  begin

    if not AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true) then
      RaiseLastOSError;

    try
      Clipboard.AsText := 'This is my own text!';
      SendMessage(GetFocus, WM_PASTE, 0, 0);
    finally
      AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
    end;

  end;
end;

end.

当然,您需要使用此方法并对其进行修改,以使其适合您的特定情况。 (也就是说,您可能想要的东西比在Ctrl + 1上打印“这是我自己的文本!”的应用程序更多,但没有别的。)

答案 1 :(得分:2)

为了补充Andreas的答案,您可以将RegisterHotKey函数与WM_HOTKEY窗口消息结合使用。

试试这段代码

type
  TForm17 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    HotKey1 : Integer;
    HotKey2 : Integer;
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public
    { Public declarations }
  end;

var
  Form17: TForm17;

implementation

{$R *.dfm}

{ TForm17 }

procedure TForm17.FormCreate(Sender: TObject);
const
  MOD_CONTROL = $0002;//0x0002
begin
  // Register Ctrl + 1 hotkey
  HotKey1 := GlobalAddAtom('Hotkey1');
  RegisterHotKey(Handle, HotKey1, MOD_CONTROL, Ord('1'));
  // Register  Ctrl + 2 hotkey
  HotKey2 := GlobalAddAtom('Hotkey2');
  RegisterHotKey(Handle, HotKey2, MOD_CONTROL, Ord('2'));
end;

procedure TForm17.FormDestroy(Sender: TObject);
begin
  //unregister the hotkeys
  UnRegisterHotKey(Handle, HotKey1);
  GlobalDeleteAtom(HotKey1);
  UnRegisterHotKey(Handle, HotKey2);
  GlobalDeleteAtom(HotKey2);
end;

procedure TForm17.WMHotKey(var Msg: TWMHotKey); 
begin
  if Msg.HotKey = HotKey1 then
  begin
    ShowMessage('Ctrl + 1 was pressed');
    //do your stuff
  end
  else
  if Msg.HotKey = HotKey2 then
  begin
    ShowMessage('Ctrl + 2 was pressed');
    //do your stuff
  end;
end;

答案 2 :(得分:1)

正如其他人所说,它是RegisterHotKey函数。但是,正确实现要设计的应用程序需要将键盘钩子和DLL注入到应用程序中。

我建议你看一下TypePilot应用程序。它允许您键入或复制/粘贴包含您键入的某些快捷方式的任何文本。例如。您可以输入“thnk”,这将被应用程序替换为“谢谢”。