左Alt + C在WPF中不起作用 [英] Left alt + C not working in WPF

查看:82
本文介绍了左Alt + C在WPF中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Alt + C ShotCut编写了以下代码

  private   void  Window_KeyDown_1( object  sender,KeyEventArgs e)
{
if (e.Key == Key.C&&(Keyboard.Modifiers&(ModifierKeys.Alt))==(ModifierKeys.Alt))
MessageBox.Show( DONE);
}



它的工作效果为正确的alt

但它失败了Left alt请帮忙我来解决这个问题



我尝试了什么:



< pre lang =c#> private void Window_KeyDown_1( object sender,KeyEventArgs e)
{
if (e.Key == Key.C&&(Keyboard.Modifiers) &(ModifierKeys.Alt))==(ModifierKeys.Alt))
MessageBox.Show( DONE );
}

解决方案

您需要检查SystemKey是否为Alt键(右侧Alt键是通常实际上是Alt Gr(Alt Graphics)并且没有被识别为系统键,这就是为什么当左侧没有时工作的原因。


Eg尝试

  var  key =(e.Key == Key.System?e.SystemKey:e.Key); 
if (key == Key.C&&(Keyboard.Modifiers&(ModifierKeys.Alt))==(ModifierKeys.Alt))
MessageBox.Show( DONE);



(改编自Julien Lebosquain的解决方案,来自这篇文章 [ ^ ])


hi, I wrote the following code for Alt+C ShotCut

private void Window_KeyDown_1(object sender, KeyEventArgs e)
{
    if (e.Key == Key.C && (Keyboard.Modifiers & (ModifierKeys.Alt)) == (ModifierKeys.Alt))
        MessageBox.Show("DONE");
}


its working good for "right alt"
But it fails for "Left alt" please help me to solve the problem

What I have tried:

private void Window_KeyDown_1(object sender, KeyEventArgs e)
{
    if (e.Key == Key.C && (Keyboard.Modifiers & (ModifierKeys.Alt)) == (ModifierKeys.Alt))
        MessageBox.Show("DONE");
}

解决方案

You need to check SystemKey for the Alt key (the right "Alt" key is often actually "Alt Gr" (Alt Graphics) and not recognised as the System key which is why that one works when the left one doesn't).

E.g. Try

var key = (e.Key == Key.System ? e.SystemKey : e.Key);
if (key == Key.C && (Keyboard.Modifiers & (ModifierKeys.Alt)) == (ModifierKeys.Alt))
    MessageBox.Show("DONE");


(Adapted from a solution by Julien Lebosquain at this post[^])


这篇关于左Alt + C在WPF中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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