key

How can I convert string virtual key code to virtual key code?


I have created xml file in which I have added all keys as nodes and its respective virtual key codes as a value. I am fetching those values and storing it in string format. I need output as: When I press key 0 its corresponding value is 0x30 But my PressKey function uses Keybd_event which takes virtual key code as an argument.

public bool Presskey(byte buttonVirtualKey)
{
   keybd_event(buttonVirtualKey, 0, 0, 0);
}

I need to convert the string formatted virtual key code("0x30") to byte value(0x30).

Any type of help would be appreciated.


Solution

  • We cannot convert string of form "0x30" to bytes because the Convert class expects a string of the form "30" to be able to convert. Also, the Convert class uses decimal base and not hexa-decimal. So, you first have to convert "30" to decimal value, and then use the decimal value in Convert.ToBytes(int) function, eg:

    int decValue = int.Parse("30", System.Globalization.NumberStyles.HexNumber);
    Convert.ToByte(decValue);