1

I need a problem. I would like catch alt codes (ALT + 64 = @) in Window. My code is correct for shortcut with Control but when I changed for ALT, dont work and in Key property is value "System". This is my code:

Correct:

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

Error:

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

And my second question is how to simulate ALT+64 (multiple keys). Top example is only for ALT+6

Thanks

2 Answers 2

1

Since you are using WPF best way of handling keyboard shortcuts is through 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 } 

Add this to xaml

<Window.CommandBindings> <CommandBinding Command="self:CustomCommands.Exit" CanExecute="ExitCommand_CanExecute" Executed="ExitCommand_Executed" /> </Window.CommandBindings> 
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

3 Comments

This is OK, but i would like press this shortcut: ALT+53 this is ascii code for number 5
Since numbers aren't valid enum keys, microsoft named the number keys "DX" ("Key.D5") in your case.
Key.D5 is the number key with the number 5, which you'll find above the 'T' Key, or centered by the other numbers on the numpad, and for which you asked in your first comment to this answer. You can simply replace the "Key.S" from my answer with "Key.D5"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.