How to convert string to Keys

26,401

Solution 1

Key comparision is done with enumerations, So what you have to do is a String to Enum conversion.

if (e.Modifiers == (Keys)Enum.Parse(typeof(Keys), "keys1", true)
    && e.KeyCode == (Keys)Enum.Parse(typeof(Keys), "keys2", true))
{
    string keyPressed = e.KeyCode.ToString();
    MessageBox.Show(keyPressed);
}

Solution 2

Keys key;
Enum.TryParse("Enter", out key);

Solution 3

I'll suggest you not to store keys in config as "ControlKey,N", rather store its value.

 Keys openKey = Keys.ControlKey | Keys.N;
 int value = (int)openKey;//95

It integer representation is 95, So store in app.config as

<add key="open" value="95">//ControlKey|N

Keys open = (Keys)int.Parse(cm.GetValueString("open").ToString());

Here open will be Keys.ControlKey | Keys.N

Then you can compare easily against e.KeyData

if(e.KeyData == open)
{
    //Control + N pressed
}
Share:
26,401
hbk
Author by

hbk

BOYCOTT on russia - don't buy, sell, support

Updated on July 09, 2022

Comments

  • hbk
    hbk almost 2 years

    try to implement combination of keypressing for my programm currently can detect required keypressed (in this post described how) but only predefined in code, but I want to store setting in condig file then read it and use if pressed.

    Now can store it, and read as string - currently try to convert readed string to Keys, using next code:

    Storing in config file:

    <add key="open" value="ControlKey,N"
    <add key="close" value="ControlKey,Q" />
    <add key="clear" value="ControlKey,D" />
    <add key="settings" value="ControlKey,S" />
    <add key="colorKey" value="ControlKey,K" />
    <add key="fontKey" value="ShiftKey,T" />
    <add key="defaultKey" value="ControlKey,P" />
    

    and using it

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(Keys));
            string[] keyValueTemp;
    
            keyValueTemp = cm.GetValueString("open").ToString().Split(',');
            string key1 = keyValueTemp[0];
            string key2 = keyValueTemp[1];
    
            Keys keys1 = (Keys)converter.ConvertFromString(key1);
            Keys keys2 = (Keys)converter.ConvertFromString(key2);
    
            if (ModifierKeys == keys1 && e.KeyCode == keys2)
            {
                string keyPressed = e.KeyCode.ToString();
                MessageBox.Show(keyPressed);
            }
        }
    

    But, has next result -

    enter image description here So - as you see - this convert control Key to Shiftkey, also try to use code if (ModifierKeys.ToString() == keyValueTemp[0] && e.KeyCode.ToString() == keyValueTemp[1]), but it's not work too.

    if use this code

            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.N)
            {
                string keyPressed = e.KeyCode.ToString();
                MessageBox.Show(keyPressed);
            }
    

    all works

    Q: how can i convert string to Keys and compare it with keyPressed events?

    EDIT

    So found my mistake

    Keys key = (Keys)converter.ConvertFromString(keyValueTemp[0]);
    Keys key2 = (Keys)converter.ConvertFromString(keyValueTemp[1]);
    if (e.Modifiers == key && e.KeyCode == key2)
       {
           MessageBox.Show(e.KeyCode.ToString());
       }
    

    forget to add e - from event handler

    another way - as written by AccessDenied

     Keys key = (Keys)Enum.Parse(typeof(Keys), keyValueTemp[0], true);
    
  • hbk
    hbk over 10 years
    yep - it's work perfect, thanks, also found another solution - see EDIT
  • Kurubaran
    Kurubaran over 10 years
    @Kirill Glad it helped :)
  • hbk
    hbk over 10 years
    but what wrong if i store my key in string like ControlKey,N ?
  • Sriram Sakthivel
    Sriram Sakthivel over 10 years
    Don't you think you're doing additional work by splititing it up with comma into two entries, parsing it to two Keys and checking with modifier and KeyCode. Isn't it better to store the value and compare against e.KeyData