1

I am doing my graduation work and I need a little help. So far I've done most of my work and now I need someone to help me with the shortcuts.

This is how my work looks like (for now): enter image description here

I need to make shortcuts for those buttons so that user doesn't have to click on button with mouse. I wish to make it possible that when user presses F1 on the keybord - it's like he pressed button1 with mouse. F2 stands for button2 and so on. Shortcuts as CTRL+ some key are also acceptable. I only need example how to make couple buttons, and i will make others :) Thanks

W0lfw00ds
  • 2,018
  • 14
  • 23
user3696764
  • 11
  • 1
  • 2
  • 2
    you should look here: http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application – hillel_guy Jun 01 '14 at 13:00

2 Answers2

1

Set Form1's KeyPreview-property to true in the designer or in the code:

this.KeyPreview = true;

Then add KeyUp-action to your form, which accepts all keys (even Delete etc):

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Delete)
   {
      button1ClickMethod();
   }
}

Then separate your buttons' actions to individual methods, like button1ClickMethod(), and avoid using the Button1_Click(null, null);. Call this button1ClickMethod() in the KeyUp-event when a desired key is up.

To use combinations, you can use this:

// If CTRL and F1 were pressed
if (e.Control && e.KeyCode == Keys.F1)
{
   MessageBox.Show("Shortcut CTRL + F1 was pressed!");
}

You can also check if the Shift was pressed at the same time with e.Shift-property, same way like the e.Control.

W0lfw00ds
  • 2,018
  • 14
  • 23
-1

KeyPress event will help you

Sample code ;

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        //Call the event according to key
        // if(e.KeyChar == "")
        //btnSave_Click(null,null)
    }
hbaktir
  • 11
  • 7