即使窗口隐藏在托盘中也可以使用热键。德尔福可以吗?

发布于 2024-12-10 09:45:47 字数 571 浏览 0 评论 0原文

我需要将表单隐藏到系统托盘中,但同时我想使用热键,例如“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;

I need to hide a form to the system tray, but in the same time I want to use hotkey, such a "ctrl+3" to get text from tEdit on my hiden form being inserted to Firefox SendText(edit1.Text); // in such method'. I know how to insert text, but i don't know anything about hotkeys/ Any suggestions? Thank you. Code of text inserting below

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴随着你 2024-12-17 09:45:48

要注册系统范围的热键,您必须使用 RegisterHotKeyUnRegisterHotKey< /a> 函数。

检查此示例

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;

只需小心您选择的组合键,因为可以在其他应用程序内部使用。例如,Firefox 使用组合 Ctrl Number 来切换选项卡。

To Register a system wide hotkey you must use the RegisterHotKey and UnRegisterHotKey functions.

Check this sample

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;

Just be carefull about the key combination which you choose, because can be used internally for another app. for example the combination Ctrl Number is used by Firefox to switch the tabs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文