按Windows修改键时,Keyboard.Modifiers为None

时间:2017-02-21 16:24:38

标签: c# wpf key-bindings

只要单击或双击背面的按钮,Surface触控笔就会发送信号WindowsKey + F20 / 19,我想在我的应用程序中捕获它。

我已经看到some questions关于检测何时按下Windows键,看起来在没有PInvoke的C#中这是不可能的。

但是,我不确定这是否也适用于Windows Key作为修饰符。看起来似乎如此,但我想肯定地知道。

ModifierKeys enum文档列出了Windows密钥,似乎没有关于此密钥的特殊说明。

所以我已将主窗口的OnPreviewKeyDown连接到此代码:

private void MainWindow_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.SystemKey == Key.F10 &&
         e.KeyboardDevice.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Windows)
      {
        Console.WriteLine("Succes!");
    }
}

如果我听alt修饰键,此代码有效,但是如果我按Windows+F10 e.KeyboardDevice.Modifiers等于None,那么if条件就会失败。

我有什么遗漏或者我应该去PInvoke路线吗?

1 个答案:

答案 0 :(得分:1)

以下代码应该能够检测到Windows+F10

private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.SystemKey == Key.F10 && (Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin)))
    {
        Console.WriteLine("Succes!");
    }
}