2016-12-22 16 views
1

問題があります。私は、ウィンドウ内でaltコード(ALT + 64 = @)をキャッチしたいと思います。私のコードは、コントロールのショートカットには正しいですが、私がALTのために変更したとき、仕事はしませんし、キーのプロパティは値 "システム"です。WPF with ALTショートカット

正しい:

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" 

そして、私の2番目の質問は、ALT + 64(複数のキー)をシミュレートする方法です。あなたがWPFを使用しているので、トップの例では、唯一のALT + 6のための

おかげ

答えて

1

でキーボードショートカットを処理する最善の方法は、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 
    } 

ている

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

てみてくださいXAMLするためにこれを追加これは:

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

これは問題ありませんが、このショートカットを押してください:ALT + 53数値5のASCIIコードです – bluray

+0

数値は有効な列挙型キーではないので、マイクロソフトでは数字キー "DX"( "Key.D5")場合。 – SnowballTwo

+0

どのように動作しますか? Key.D5とは何ですか? – bluray

関連する問題