tags:

views:

69

answers:

2

how can i run a method when someone user a keyboard shortcuts like CTRL + G ?

the application is a Windows forms application and i only want to capture them when the form is activated.

+1  A: 

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... http://stackoverflow.com/questions/1143567/test-for-ctrl-keydown

Martin Peck
This won't work if you have child controls that take keyboard focus.
Tergiver
+2  A: 

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.

Tergiver
+1: ProcessCmdKey is a great function too often overlooked.
Matthew Ferreira