PreviewKeyDown是不多见的Alt-修饰语 [英] PreviewKeyDown is not seeing Alt-modifiers

查看:208
本文介绍了PreviewKeyDown是不多见的Alt-修饰语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码是(应该是)拍摄按键。顶层窗口有一个

I have some code which is (supposed to be) capturing keystrokes. The top level window has a

Keyboard.PreviewKeyDown="Window_PreviewKeyDown"

子句和后盾CS文件包含:

clause and the backing CS file contains:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e) {
    if (e.KeyboardDevice.Modifiers == ModifierKeys.Control) {
        switch (e.Key) {
            case Key.L:
                btnPrev_Click(sender, new RoutedEventArgs());
                e.Handled = true;
                break;
            case Key.R:
                btnNext_Click(sender, new RoutedEventArgs());
                e.Handled = true;
                break;
        }
    }
}

现在工作正常,无论是<大骨节病> CTRL <大骨节病>→和<大骨节病> CTRL <大骨节病>研究调用相关功能。

Now that works fine, both CTRLL and CTRLR call the relevant functions.

当我改变修改器检查使用 ModifierKeys.Alt ,它停止工作。换句话说,无论是<大骨节病> ALT <大骨节病>→和<大骨节病> ALT <大骨节病>研究调用的函数。

As soon as I change the modifer check to use ModifierKeys.Alt, it stops working. In other words, neither ALTL and ALTR call the functions.

我缺少的是在这里吗?

推荐答案

在的情况下, Alt键修改 e.Key 收益 Key.System 和真正的关键是在 e.SystemKey 。您可以使用下面的代码段始终得到正确的按键:

In case of an Alt modifier, e.Key returns Key.System and the real key is in e.SystemKey. You can use the following piece of code to always get the correct pressed key:

Key key = (e.Key == Key.System ? e.SystemKey : e.Key);

这篇关于PreviewKeyDown是不多见的Alt-修饰语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆