Executing methods through configurable keyboard shortcuts

1

I'm developing a system that has some functions (eg F12 -> Close the system). In the system has the form of parameters, and in this form I would like the client to choose which shortcut for each function. detail: each function is a method in source code, so I just need to reference a shortcut to a method at runtime.

if(e.keycode == Keys.F12)
{
   FecharSistema();
}

This code and how the system works today, but now has a configurations form where it would be possible to change the shortcut from F12 to F10. In short, let the customer choose which function they will have on each key. Currently F1 - Opens the box, F2 - starts a sale, F12 - Closes the system. But each client wants the shortcut on different buttons. A form has been developed where it is possible for the user to choose which key will perform each function.

How do I change the code for the shortcut to be customizable?

    
asked by anonymous 28.01.2015 / 18:02

1 answer

2

There are two possible cases , use whatever you find most convenient to your project.

Focusing

The first is the Form only catch Keys if it is with focus, if it is minimized it will not catch. This case is simpler.

    // Coloque no .cs do seu Form
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if(keyData == Keys.F12) // Compara a tecla pressionada
               FecharSistema(); //Realiza o método desejado

            return base.ProcessCmdKey(ref msg, keyData);
        }

Focusless

In this case, even if the Form is minimized, it can receive the command of the key pressed, using Global Hotkey with P / Invoke in Windows function . I recommend this article on Global Hotkey . You also have the GlobalHotkey class code described in the article.

public partial class Form1 : Form
{
  GlobalHotkey hk1;
  public Form1()
  {
     this.hk1 = new GlobalHotkey(0, Keys.F12, this); // Registra o F12 para este formulário, "this".
     this.hk1.Register();
  }

  protected override void WndProc(ref Message m)
  {
      if (m.Msg == Hotkeys.Constants.WM_HOTKEY_MSG_ID) // Compara se a mensagem é uma tecla registrada
      {
           if ((IntPtr)this.hk1.GetHashCode() == m.WParam) // Verifica se a tecla pressionada é a que registramos em nosso código.
             FecharSistema();
      }
      base.WndProc(ref m);    
  }

If you want a customizable system , create a control and the Event " KeyUp ". In this case I used a Textbox to get the Key that the user would like.

    Keys KeyPersonalizada;

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
       KeyPersonalizada = e.KeyCode;
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
         if(keyData == KeyPersonalizada) // Compara a tecla pressionada é igual a personalizada
             FecharSistema(); //Realiza o método desejado

         return base.ProcessCmdKey(ref msg, keyData);
    }
    
29.01.2015 / 01:18