简体   繁体   English

C#keydown事件存储在char变量中

[英]C# keydown event storing in char variables

I'm wondering how I could just store the pressed key in a char variable (C#). 我想知道如何将按下的键存储在char变量(C#)中。
Could anyone help me? 有人可以帮我吗?

with kind regards, dutchjelly 亲切的问候,dutchjelly

(is it possible to do this?) (是否有可能做到这一点?)

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        char c = e.KeyChar;
        if(c = 'A')
        {
            do something
        }
    }

You could use something like this: 您可以使用如下形式:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    char c = Convert.ToChar(e.KeyCode);
    if (c == 69)
    {
        do something
    }
}

where 69 represents an ASCII code (in this case E , you can search for other on google). 其中69代表ASCII码 (在这种情况下为E ,您可以在Google上搜索其他字符 )。

You can do the following: 您可以执行以下操作:

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    Keys k = e.KeyCode; //This is your key code
}

The same way you could store the pressed keys to a List or some structure of your need: 您可以将按下的键存储到列表或所需的某些结构的相同方法:

List<Keys> keys = new List<Keys>();
keys.Add(k);

EDIT: 编辑:

In the case you just want to get the char value of what the user has pressed you can do this: 在这种情况下,您只想获取用户所按内容的char值,您可以这样做:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char c = e.KeyChar;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM