2016-12-22 79 views
1

我需要一个问题。我想在窗口中捕获替代码(ALT + 64 = @)。我的代码是正确的快捷键与控制,但当我改变了ALT,不工作,并在Key属性值为“系统”。这是我的代码:带ALT的WPF快捷键

正确:

if (e.Key == Key.S && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)//CTRL+S

错误:

if (e.Key == Key.S 
    && (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) //ALT+S dont work - e.Key="System" 

我的第二个问题是如何模拟ALT + 64(多键)。顶级例子仅ALT + 6

感谢

回答

1

由于您使用的处理键盘快捷键WPF最好的办法是通过InputGesture

/// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     }  

     private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
     { 
      e.CanExecute = true; 
     } 

     private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e) 
     { 
      Console.WriteLine("Your implementation"); 
     } 

    } 


    public static class CustomCommands 
    { 
     public static readonly RoutedUICommand Exit = new RoutedUICommand 
       (
         "Exit", 
         "Exit", 
         typeof(CustomCommands), 
         new InputGestureCollection() 
           { 
             new KeyGesture(Key.S, ModifierKeys.Alt) 
           } 
       ); 

     //Define more commands here, just like the one above 
    } 

添加这XAML

<Window.CommandBindings> 
     <CommandBinding Command="self:CustomCommands.Exit" CanExecute="ExitCommand_CanExecute" Executed="ExitCommand_Executed" /> 
    </Window.CommandBindings> 
0

尝试此:

if(e.KeyboardDevice.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.S) 
+0

这是好的,但我想按下这个快捷键:ALT + 53这是ascii代码为5号 – bluray

+0

由于数字不是有效的枚举键,微软命名数字键“DX”(“Key.D5”)在您的案件。 – SnowballTwo

+0

它是如何工作的?什么是Key.D5? – bluray