Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to Keys

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);
like image 739
hbk Avatar asked Sep 29 '13 11:09

hbk


People also ask

How do you convert a string to a key in Python?

Method 1: Splitting a string to generate key:value pair of the dictionary In this approach, the given string will be analysed and with the use of split() method, the string will be split in such a way that it generates the key:value pair for the creation of a dictionary. Below is the implementation of the approach.

What is a string key?

Keys are used to identify translatable strings within software code. This allows the key to be referenced once instead of each time the string requires translation to another language.

How do you convert a string to a list in Python?

To do this we use the split() method in string. The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string.


2 Answers

Keys key;
Enum.TryParse("Enter", out key);
like image 115
Muhammad Umar Avatar answered Sep 28 '22 08:09

Muhammad Umar


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);
}
like image 24
Kurubaran Avatar answered Sep 28 '22 08:09

Kurubaran