【问题标题】:How to prevent Windows+D key combination Wpf如何防止 Windows+D 组合键 Wpf
【发布时间】:2019-09-12 07:08:18
【问题描述】:

我想防止组合键 Window+D 使用 xaml。

<Window.InputBindings>
    <KeyBinding Modifiers="Alt" Key="F10" Command="{Binding CommandAltF10}"></KeyBinding>
    <KeyBinding Modifiers="Windows" Key="D" Command="{Binding CommandWindowD}"></KeyBinding>
</Window.InputBindings>

第一个组合有效。当我按下Alt+F10 键时,CommandAltf10 命令正在触发。

但是当我按下Window+D 键时,它不会触发CommandWindowD 命令。为什么它不起作用?

【问题讨论】:

  • 该组合通常已经注册以最小化所有窗口并显示桌面。
  • 来自docs涉及WINDOWS键的键盘快捷键保留供操作系统使用,因此您不能使用本机RegisterHotKey API覆盖这些,甚至更少KeyBinding

标签: c# wpf xaml


【解决方案1】:

重复:Capture a keyboard keypress in the background

注意:在 XAML 中无法处理此问题,您需要创建全局热键

我根据您的需要编辑了解决方案:

  1. 在班级顶部导入所需的库:
// DLL libraries used to manage hotkeys
[DllImport("user32.dll")] 
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
//Add a field in your class that will be a reference for the hotkey in your code:
const int MYACTION_HOTKEY_ID = 1;
  1. 注册热键(例如在窗口的构造函数中):
// Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
// Compute the addition of each combination of the keys you want to be pressed
// WIN KEY: 8 see: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 8, (int) Keys.D);
  1. 通过在您的类中添加以下方法来处理键入的键:
protected override void WndProc(ref Message m) {
    if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID) {
        // My hotkey has been typed
        // in your case you want to return in order to not trigger the event
        return;
    }
    base.WndProc(ref m);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多