3
votes

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.

2
Some hint about whether this is a Winforms app, Silverlight, WPF, Console App, ASP.NET etc would helpMartin Peck
Do you want to do global keyboard hook and intercept the shortcut in any application, or just your specific application?Franci Penov
thanks for the hints. added more details :)Hannoun Yassir

2 Answers

4
votes

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.

1
votes

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#