捕获按键

来源:互联网 发布:网易股票行情数据 编辑:程序博客网 时间:2024/04/29 13:59

这两天给产线上做了一个烧录工具,用扫描枪录入,扫描枪都会带有一个回车符,这样通过捕获回车符,让输入焦点依次被选中。

keyDown/keyUP/keyPress这三个都可以捕获回车。

但还是有些不同的

 private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox2.Text.ToString().Trim().Length == 0)
            {
                textBox2.Focus();
            }
            else
            {
                if (e.KeyChar == System.Convert.ToChar(13))
                {
                    if (textBox2.Text.ToString().Trim().Length > 0)
                    {
                        textBox1.Focus();
                        e.Handled = true;
                    }
                }
            }
        }


private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            bool ret=KeysConverter.Equals(e.KeyData,Keys.Space);
            if (ret)
            {
                MessageBox.Show("OK");
                e.Handled = true;
            }
            else
            {
                MessageBox.Show("Key value:"+e.KeyValue);
            }
        }


发现KeysConverter这个辅助类很好用。

0 0
原创粉丝点击