3

我需要在系统托盘中隐藏一个表单,但同时我想使用热键,例如“ctrl+3”从 tEdit 获取文本,将我的隐藏表单插入到 Firefox SendText(edit1.Text); // 在这种方法中'。我知道如何插入文本,但我对热键一无所知/有什么建议吗?谢谢你。下面插入的文本代码

procedure SendText(const Value: WideString);
var
  I: Integer;
  S: WideString;
  TI: TInput;
  KI: TKeybdInput;
const
  KEYEVENTF_UNICODE = $0004;
begin
  S := WideUpperCase(Value); 
  TI.Itype := INPUT_KEYBOARD;
  for I := 1 to Length(S) do
  begin
    KI.wVk := 0;
    KI.dwFlags := KEYEVENTF_UNICODE;
    KI.wScan := Ord(S[I]);
    TI.ki := KI;
    SendInput(1, TI, SizeOf(TI));
  end;
end;
4

1 回答 1

11

要注册系统范围的热键,您必须使用RegisterHotKeyUnRegisterHotKey功能。

检查这个样本

type
  TForm125 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    HotKey1 : Integer;
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  public

  end;

var
  Form125: TForm125;

implementation

{$R *.dfm}


procedure TForm125.FormCreate(Sender: TObject);
begin
  HotKey1 := GlobalAddAtom('MyAppHotkey1');//create a unique value for identify the hotkey
  if not RegisterHotKey(Handle, HotKey1, MOD_CONTROL, VK_F1) then //register the hotkey CTRL + F1
   ShowMessage('Sorry can not register the hotkey');
end;

procedure TForm125.FormDestroy(Sender: TObject);
begin
  UnRegisterHotKey(Handle, HotKey1);//unregister the hotkey
  GlobalDeleteAtom(HotKey1);//remove the atom
end;

procedure TForm125.WMHotKey(var Msg: TWMHotKey);
begin
  if Msg.HotKey = HotKey1 then
    ShowMessage('Hello'); // do your stuff
end;

请注意您选择的组合键,因为可以在内部用于另一个应用程序。例如Ctrl Number,Firefox 使用该组合来切换选项卡。

于 2011-10-19T01:19:24.123 回答