Running methods through configurable keyboard shortcuts

Asked

Viewed 1,576 times

1

I’m developing a system that has some functions (e.g., 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 the 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 form settings where it would be possible to change the shortcut from F12 to F10. In short, let the customer choose which function will be in each key. Currently F1 - Opens the box, F2 - starts a sale, F12 - Closes the system. But each customer wants the shortcut in different buttons. A form has been developed where it is possible for the user to choose which key to perform each function.

How to change the code so that the shortcut is customizable?

  • 1

    Ué, change the if and ready. if(e.keycode == TECLA_DE_FECHAR_O_SISTEMA).

  • I can’t change the code because I can’t guess which key the client will choose. Each one wants a different shortcut, the system is customizable!

1 answer

2

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


Focused

The first is the Form only catch Keys if it is focused, 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);
        }

Unfocused

In this case, even if the Form is minimized it can receive the command of the pressed key, using Global Hotkey with P/Invoke in a windows function. I recommend this article on Global Hotkey. Also has the code of the class Globalhotkey 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 in Event "Keyup". In this case I used a Textbox to get the Key that the user would want.

    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);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.