How can i 开发者_高级运维run a method when someone used a keyboard shortcut like Ctrl+G ?
The application is a Windows forms application and i only want to capture it when the form is activated.
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.
Look at your OnKeyPress method on the Form or the KeyPress event for the Form.
Override the OnKeyPress like this...
protected override void OnKeyPress(KeyPressEventArgs e) { ... }
... and look at the KeyPressEventArgs to see what key was pressed and whether it was pressed with Ctrl.
This SO answer shows how to use the KeyPress event... Test for 'Ctrl' keydown in C#
精彩评论