2016-12-22 2 views
1

Ich brauche ein Problem. Ich möchte Alt-Codes (ALT + 64 = @) in Windows fangen. Mein Code ist korrekt für die Verknüpfung mit Control, aber wenn ich für ALT geändert habe, funktioniert nicht und in Key Eigenschaft ist Wert "System". Dies ist mein Code:WPF Verknüpfung mit ALT

Richtig:

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

Fehler:

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

Und meine zweite Frage ist, wie ALT + 64 (mehrere Schlüssel) zu simulieren. Top Beispiel ist nur für ALT + 6

Dank

Antwort

1

Da Sie WPF besten Weg mithilfe von Tastenkombinationen der Handhabung durch ist 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 
    } 

dieses Add

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

Versuchen Sie XAML dies:

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

Das ist in Ordnung, aber ich möchte diese Abkürzung drücken: ALT + 53 das ist ASCII-Code für Nummer 5 – bluray

+0

Da Zahlen keine gültigen Enum-Schlüssel sind, nannte Microsoft die Nummerntasten "DX" ("Key.D5") in Ihrem Fall. – SnowballTwo

+0

Wie funktioniert es? Was ist Key.D5? – bluray