C#强制注册/覆盖热键

问题描述 投票:-2回答:1

我想强制注册/覆盖热键...所以这段代码工作正常但是当热键已经注册到其他应用程序时失败了...

所以我想覆盖热键有可能吗?

[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. 
}
c# windows winapi hotkeys
1个回答
-1
投票

我同意IInspectable(显然是雷蒙德)。你应该避免这种情况。但是,如果你绝对必须这样做,我相信Windows Hooks使用SetWindowHookEx是要走的路。

系统级钩子是严肃的东西,在实现它们时应该小心,因为它们会影响系统的性能。

有关详细信息和工作代码,请查看herehere

请再次注意:它有点矫枉过正,就像使用大锤(或者我会说拆除锤子)来破解螺母一样。但有时你必须做你必须做的事!

© www.soinside.com 2019 - 2024. All rights reserved.