C#强制注册/覆盖热键
我想强制注册/覆盖热键…所以这段代码工作正常,但失败的时候,热键已经注册为其他应用程序 所以我想覆盖热键,有可能吗C#强制注册/覆盖热键,c#,windows,winapi,hotkeys,C#,Windows,Winapi,Hotkeys,我想强制注册/覆盖热键…所以这段代码工作正常,但失败的时候,热键已经注册为其他应用程序 所以我想覆盖热键,有可能吗 [DllImport("user32", SetLastError=true)] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport("user32", SetLastError = true)] private static e
[DllImport("user32", SetLastError=true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}
public ExampleForm()
{
InitializeComponent();
int id = 0; // The id of the hotkey.
RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift, Keys.A.GetHashCode()); // Register Shift + A as global hotkey.
}
我同意IInspectable(显然是Raymond)。你应该避免这样。但是,如果您一定要这样做,我相信Windows钩子使用SetWindowHookEx
是一个不错的选择
系统级钩子是很重要的东西,在实现它们时应该小心,因为它们会影响系统的性能。
有关详细信息和工作代码,请参见和
请再次注意:这是一种过度杀伤力,就像用大锤(或者我会说是拆卸锤)敲碎一个螺母一样。但有时候你得做你该做的 当你说“它失败了”时,到底发生了什么?您的程序是否出错?如果是,它是否显示某个错误消息?如果热键属于另一个应用程序,则取消注册热键将无法正常工作,因为它预期调用线程先前已注册了该键-情况并非如此。你为什么要这样做?特别值得引用文章IInspectable linked中的这句话——“当两个程序像这样“决斗”时,你无法预测哪一个会赢,但你可以100%肯定地预测谁会输:用户。”