问题描述
我有一个位于托盘中的应用程序,我想定义多个热键,这些热键将在我的程序中触发事件.
I have a application that lives in the tray and I would like to define serveral hotkeys which will trigger events in my program.
我从@AaronLS 在这个问题中的出色回答中找到了灵感:使用 C# 设置全局热键
I found inspiration from the excellent answer by @AaronLS in this question: Set global hotkeys using C#
如果我注册一个已经由另一个应用程序定义的热键 RegisterHotKey
将返回 false 而不会注册它.那么如何覆盖或替换热键呢?
If I register a hot key that are already defined by another application RegisterHotKey
will return false and not register it. So how can I override or replace a hot key?
推荐答案
表面上,你不能.那是设计使然.想象一下,如果程序可以随意劫持其他程序的热键 - 那将不会令人愉快.
Ostensibly, you can't. That's by-design. Imagine if programs could hijack other program's hotkeys at-will - that wouldn't be pleasant.
Win32 UnregisterHotKey
函数 要求调用者与使用 RegisterHotKey
注册的线程相同,这使得另一个程序无法取消注册另一个程序的热键:
The Win32 UnregisterHotKey
function requires the caller to be the same thread that registered using RegisterHotKey
, which makes it impossible for another program to unregister another program's hotkeys:
UnregisterHotKey
函数:
释放先前由调用线程注册的热键.
您可以做的是使用 Win32 将代码注入远程进程(假设您自己的进程具有执行此操作的权限),然后在该进程的 UI 线程中调度代码,然后调用 UnregisterHotKey
- 但这是另一个问题.
What you can do is use Win32 to inject code into a remote process (assuming your own process has permsision to do this) then schedule code in that process's UI thread that then calls UnregisterHotKey
- but that's another question.
这篇关于替换全局热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!