简体   繁体   English

听键盘快捷键(c#)

[英]Listen keyboard shortcuts (c#)

How can i run a method when someone used a keyboard shortcut like Ctrl + G ? 当有人使用像Ctrl + G这样的键盘快捷键时,如何运行方法?

The application is a Windows forms application and i only want to capture it when the form is activated. 该应用程序是一个Windows窗体应用程序,我只想在窗体激活时捕获它。

Keyboard shortcuts are often referred to as "command keys". 键盘快捷键通常称为“命令键”。 If you want first-crack at keypresses (before your child controls), the best place is to override the ProcessCmdKey method on your form. 如果你想在按键时(在孩子控制之前)先破解,最好的地方是覆盖表单上的ProcessCmdKey方法。

Look at your OnKeyPress method on the Form or the KeyPress event for the Form. 查看表单上的OnKeyPress方法或表单的KeyPress事件。

Override the OnKeyPress like this... 像这样覆盖OnKeyPress ......

protected override void OnKeyPress(KeyPressEventArgs e) { ... }

... and look at the KeyPressEventArgs to see what key was pressed and whether it was pressed with Ctrl. ...并查看KeyPressEventArgs以查看按下了哪个键以及是否按下了Ctrl键。

This SO answer shows how to use the KeyPress event... Test for 'Ctrl' keydown in C# 这个SO答案显示了如何使用KeyPress事件... 在C#中测试'Ctrl'keydown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM