C#强制注册/覆盖热键

时间:2018-08-29 13:35:45

标签: 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 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. 
}

1 个答案:

答案 0 :(得分:-1)

我同意IInspectable(显然是Raymond)。您应该避免这种情况。但是,如果您绝对必须这样做,我相信使用SetWindowHookEx的Windows Hooks是可行的方法。

系统级挂钩很重要,在实现它们时应格外小心,因为它们会影响系统的性能。

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

再次注意:这是一个过大的杀伤力,就像用大锤(或者我要说是拆除锤)来开裂螺母一样。但是有时候你必须做你必须做的事!

相关问题