2016-12-22 78 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