简体   繁体   中英

C# keydown event storing in char variables

I'm wondering how I could just store the pressed key in a char variable (C#).
Could anyone help me?

with kind regards, 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).

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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