我需要一个问题。我想在Window中捕获alt代码(ALT + 64 = @)。我的代码对于使用Control的快捷方式是正确的,但是当我为ALT更改时,不工作并且在Key属性中是值" System"。这是我的代码:
正确:
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
由于
答案 0 :(得分: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>
答案 1 :(得分:0)
试试这个:
if( e.KeyboardDevice.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.S )