因为我采用得是MVVM框架,在Window.xaml加入以下代码:
<Window.InputBindings>
<KeyBinding Key="F1" Command="{Binding KeyEventCommand}" CommandParameter="F1"/>
<KeyBinding Key="F5" Command="{Binding KeyEventCommand}" CommandParameter="F5"/>
</Window.InputBindings>
在ViewModel中加入自己要操作,par参数就是要传递的键盘参数
public RelayCommand<string> KeyEventCommand => new Lazy<RelayCommand<string>>(() =>
new RelayCommand<string>(parms => KeyEventCmds(parms))).Value;
private void KeyEventCmds(string par)
{
//逻辑代码
}