Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to press a key combination (ctrl + tab) and a button clik.
I have written this code, but when I press the key combination not to clik the button, although if I press the button and after that I press de key combination then it clik.
I don´t know what happen.
this is the code
C#
public Form1()
        {
            InitializeComponent();

            InicializarEventosTeclas();

        }

private void ctr_KeyUp(object sender, KeyEventArgs e)
        {
           
           
           if (e.Control && e.KeyCode == Keys.Tab) 
            {
                button7_Click(sender, e);
            }

        }
       
private void InicializarEventosTeclas() 
        {
           this.button7.KeyUp += new KeyEventHandler(ctr_KeyUp);
            
        }
Posted
Updated 3-Jan-12 23:36pm
v2

1 solution

You should just use the forms KeyUp event, something like this

C#
public Form1()
        {
                InitializeComponent();
                this.KeyUp += new KeyEventHandler(Form1_KeyUp);
        }

        void Form1_KeyUp(object sender, KeyEventArgs e)
        {
                if (e.Control && e.KeyCode == Keys.Tab)
                {
                    button1_Click(sender, null);
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
                MessageBox.Show("You pressed Ctrl + Tab");
        }


Hope this helps
 
Share this answer
 
v2
Comments
santiti 4-Jan-12 5:36am    
Sorry, it´s practicly the same and doesn´t function. I still don´t get to press the key combination and the button click if befor that I didn´t press the button.
Wayne Gaylard 4-Jan-12 5:47am    
The code I posted was straight from Visual Studio and it worked as is. Don't forget to register the button click event as well which I did in the designer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900