How to insert control key in c#

Asked

Viewed 1,615 times

0

Someone could help me, wanted to create a command with the keys CTRL + Z or something to save, edit or send data, as a shortcut to a button created on Form.

  • Windows Forms??

  • Yes, Visual Studio.

  • Does the answer meet the request? Does it need something to be improved?

1 answer

2

  1. Change the property KeyPreview of form as true.

  2. Set actions for shortcuts in the event KeyDown of form

    Example:

    private void MainForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.Z)            
            Desfazer();            
    
        if (e.Control && e.KeyCode == Keys.S)
            Salvar();            
    
        //Assim por diante
    }
    

It is also possible to override the method ProcessCmdKey

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{
    if (keyData == (Keys.Control | Keys.S)) 
    {
        Salvar();
        return true;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}
  • Thanks for your attention I’ll test here.

Browser other questions tagged

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