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

Posted

技术标签:

【中文标题】即使窗口隐藏在托盘中,也使用热键。德尔福可以吗?【英文标题】:Using hotkey even if window hide in tray. Is it possible in Delphi? 【发布时间】:2011-12-10 11:52:40 【问题描述】:

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

【问题讨论】:

请注意,系统范围的热键可能会与应用程序热键发生冲突。例如 ctrl+3 会阻止我的应用程序中的一个热键工作。为用户提供自定义系统范围热键的选项。 【参考方案1】:

要注册系统范围的热键,您必须使用 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;

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

【讨论】:

以上是关于即使窗口隐藏在托盘中,也使用热键。德尔福可以吗?的主要内容,如果未能解决你的问题,请参考以下文章

WPF:即使显式设置 WindowState,窗口也会保持最小化

WPF 在关闭程序了之后,托盘图标依然存在,请问怎么解决?

WPF中的全局热键?

我可以为 Adob​​e AIR 应用程序分配全局热键吗?

我的应用程序的 ctrl 热键

Win7 32/64以上隐藏托盘图标