将按键组合与按钮相关联 [英] Associate a key combination to a button

查看:73
本文介绍了将按键组合与按钮相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按一个组合键(Ctrl + Tab)和一个按钮. 我已经写了这段代码,但是当我按下组合键时不要按一下按钮,尽管如果我按下按钮,然后按de组合键也就可以了.
我不知道会发生什么.
这是代码

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

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

推荐答案

您应该只使用表单KeyUp事件,就像这样

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

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


这篇关于将按键组合与按钮相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆