C#中的KeyEventArgs [英] KeyEventArgs in C#

查看:706
本文介绍了C#中的KeyEventArgs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何抓住按下的键有疑问。这是我的代码,但我无法获得所按下的键。我正在使用KeyEventArgs声明新变量,然后进行比较。

I have a problem to how to catch which key is pressed. This is my code, but i cant get what key was pressed. I'm using KeyEventArgs for declaration of new variable and then comparing it.

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        KeyEventArgs k = null;
        if (e is KeyEventArgs)
        {
            k = (KeyEventArgs)e;
        }

        if (k.KeyCode == Keys.Enter)
        {
            // do something here
        }


    }


推荐答案

您需要添加:

 [component_name].KeyDown += new System.Windows.Forms.KeyEventHandler(this.Key_Pressed_Method);

到表单的构造函数中。然后,您可以在Key_Pressed_Method()方法中定义要执行的操作。

into the constructor of your Form. Then, you can define what you want to do in Key_Pressed_Method() method.

这篇关于C#中的KeyEventArgs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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