简体   繁体   中英

How to convert KeyEventArgs.KeyCode to Enum Keys

Hi im currently searching on how to convert the keycode in the event of Keydown in the KeyEventArgs of a certain control is there a way to do it? i've found a way to convert Keys Enum to Int I want it in the other way..

You can simply use casting to get the Enum value of an int, although if you have KeyEventArgs then you already have both ( KeyCode and KeyValue ).

void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    // You can cast a Keys enum to an int 
    // (although you already have the int value in e.KeyValue)
    int keyInt = (int) e.KeyCode;

    // You can cast an int to a Keys enum 
    // (although you already have the enum value in e.KeyCode)
    Keys keyKeys = (Keys)keyInt;
}

If what you are wanting is to take an int and convert it to a Keys Enumeration you can use the System.Enum.ToObject Method.

Keys key = (Keys)System.Enum.ToObject(typeof(Keys), 65 ); //Will be A

Though without seeing your code it is very hard to make a determination exactly what you are trying to do.

From above link:

Converts a specified integer value to an enumeration member.

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