1

I want to prevent key combinations Window+D using xaml.

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

The first combination is working. When I press Alt+F10 keys, CommandAltf10 command is firing.

But when I press the Window+D keys, it does not fire CommandWindowD command. Why does not it work?

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • That combination is usually already registered to minimize all windows and show the desktop. – Cleptus Sep 12 '19 at 07:21
  • Possible duplicate of [Suppress certain Windows keyboard shortcuts](https://stackoverflow.com/questions/13324059/suppress-certain-windows-keyboard-shortcuts) – Isma Sep 12 '19 at 07:26
  • From the [docs](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey): *Keyboard shortcuts that involve the WINDOWS key are reserved for use by the operating system* so you can't use the native `RegisterHotKey` API to override these and even less a `KeyBinding`. – mm8 Sep 12 '19 at 08:39

1 Answers1

0

Duplicate of: Capture a keyboard keypress in the background

NOTE: It is impossible to handle this in XAML you need to create a global hotkey

I edited the solution to fit your needs:

  1. Import needed libraries at the top of your class:
// 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. Register the hotkey (in the constructor of your Window for instance):
// 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://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 8, (int) Keys.D);
  1. Handle the typed keys by adding the following method in your class:
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);
}
Prophet Lamb
  • 530
  • 3
  • 17