在xmal.cs文件中实现,或者重写OnPreviewKeyDown()方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | string codeValue = "" ; int InputCount = 0; protected override void OnPreviewKeyDown(KeyEventArgs e) { int keyValue = Convert.ToInt32(e.Key); if (keyValue == 156) keyValue = Convert.ToInt32(e.SystemKey); if (e.Key == Key.Up) { e.Handled = true ; ScrollToVerticalOffset( "up" ); } if (e.Key == Key.Down) { e.Handled = true ; ScrollToVerticalOffset( "down" ); }<br><br> <br> //获取组合键,如Shift+F1【操作:按下Shift,同时按下F1】<br> //获取数字组合键,如200,201等【操作:依次按2 0 0 】 if (e.Key.ToString().Contains( "NumPad" ) || (keyValue >= 34 && keyValue <= 43)) { string keyCode = e.Key.ToString().Substring(e.Key.ToString().Length - 1, 1); if ( int .TryParse(keyCode, out int tmp)) //如果转换失败(为false)时输出括号内容 { codeValue += keyCode; InputCount += 1; } else { codeValue = "" ; InputCount = 0; } if (Core.Definition.QuickKeyManager.Instance.Exec( this .GetType(), 0, codeValue)) { e.Handled = true ; codeValue = "" ; InputCount = 0; } if (InputCount == 3) { codeValue = "" ; InputCount = 0; } return ; } if (Core.Definition.QuickKeyManager.Instance.Exec( this .GetType(), keyValue)) e.Handled = true ; codeValue = "" ; InputCount = 0; base .OnPreviewKeyDown(e);<br>} |
if (e.KeyStates == Keyboard.GetKeyStates(Key.F1) && Keyboard.Modifiers == ModifierKeys.Shift)
{
codeValue = "Shift+F1";
}
Key_Down事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | protected override void OnKeyDown(KeyEventArgs e) { if (vm.AuthorizeDialogModel.IsAuthorityCard) { Key key = (e.Key == Key.System ? e.SystemKey : e.Key); CodeHelper.AnalysisKey(e.Key); //分析按键 } else { switch (e.Key) { case Key.Escape: if ( this .DataContext is ViewModels.Dialogs.AuthorizeDialogViewModel vm) vm.CloseCommand?.Execute(); e.Handled = true ; break ; case Key.F1: server.Focus(); e.Handled = true ; break ; case Key.F5: port.Focus(); e.Handled = true ; break ; case Key.Enter: if (server.IsFocused) { port.Focus(); } else if ((port.IsFocused || cardNumber.IsFocused) && btn.IsEnabled) { btn_Click(btn, null ); } e.Handled = true ; break ; } } base .OnKeyDown(e); } |
Key curretnKey = e.Key == Key.ImeProcessed ? e.ImeProcessedKey : e.Key;
if (QuickKeyManager.Instance.Exec(GetType(), Convert.ToInt32(curretnKey)))
{
e.Handled = true;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)