1

I've been looking over here, but usually question is other way roud "how to convert virtualkey to char". So I need to ask how to convert char into virtualkeycode?

I can perfectly fine work with F1, Ctrl etc., but can't figure out how to convert A to VK_A etc. in some convenient way.

I'm trying to let external configuration file hold some variables which then have to be used as virtualkeycodes in actual application.

I can recognize pairs like

  Alt+F2, Shift+Control+F1 ... etc.

As those are just Enum 0-12 and then VirtualKeyCode.F1 + index

But I can't figure out those

  Alt+F, Shift+Control+A ... etc.

I'm probably missing something very straightforward but unfortunately i can't see it.

Thanks

Edit:

Thanks to help from here I'm now able to convert "Ctrl+X" or "Shift-A" into VirtualKeyCode with this bit of code (still more like testing code)

    public static void ParseKeys(string text)
    {
        Key key = 0;           
        Key mod = 0;
        int current = 0;
        string[] result;
        string[] separators = new string[] { "+", "-" };
        result = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);

        foreach (string entry in result)
        {
            if (entry.Trim() == Keys.Control.ToString() || entry.Trim() == "Ctrl")
                mod = Key.LeftCtrl;

            if (entry.Trim() == Keys.Alt.ToString())
                mod = Key.LeftAlt;

            if (entry.Trim() == Keys.Shift.ToString())
                mod = Key.LeftShift;

            if (entry.Trim() == Keys.LWin.ToString() && current != result.Length - 1)
                mod = Key.LWin;

            current++;
        }

        KeysConverter keyconverter = new KeysConverter();
        key = (Key)keyconverter.ConvertFrom(result.GetValue(result.Length - 1));

        var vmod = KeyInterop.VirtualKeyFromKey(mod);

        System.Diagnostics.Trace.WriteLine((VirtualKeyCode)vmod + " = " + (VirtualKeyCode)key);

    }

usage

    ParseKeys("Alt+X");

I found out I would need to handle combinations like "Alt+Ctrl+X" or "Ctrl+X+Q" but indeed this code is for A+B combo. Can somebody please suggest some elegant solution to have at the end this:

    VirtualKeyCode[] outsequence = { VirtualKeyCode.A , VirtualKeyCode.B, VirtualKeycode.C } 

? Thanks!

fluffypuffy
  • 103
  • 1
  • 1
  • 6
  • check this question: https://stackoverflow.com/questions/8139420/textbox-convert-sent-key-alt-enter-enter – Alvaro Alves Aug 24 '18 at 19:18
  • @AlvaroAlves What does that have to do with VirtualKey Codes? – NetMage Aug 24 '18 at 19:33
  • I don't think you are missing anything straightforward. The fundamental answer is it isn't possible, depending on how you get the virtual keys you don't know the current shift states or keyboard layout. See this [answer](https://stackoverflow.com/q/41911499/2557128) for more discussion about the difficulties. Ultimately you could use `TranslateMessage` and intercept the resuling `WM_Char`. – NetMage Aug 24 '18 at 19:41
  • @NetMage why would keyboard layout matter? A is just A no? So we can't in c# convert char to virtualkeycode? – fluffypuffy Aug 24 '18 at 19:44
  • As pointed out in the answer I linked, "SHIFT+5 gets '&' or '*' ", _depending on keyboard layout_. For example, the US International keyboard changes the ' key (and others) to dead keys until you press another key, and combines the two keys unto an accented character if possible. This [article](https://en.wikipedia.org/wiki/Keyboard_layout#QWERTY-based_Latin-script_keyboard_layouts) has further explanation. – NetMage Aug 24 '18 at 20:32
  • Also, if CAPS LOCK is on or SHIFT is down, `a` is `A` but otherwise `a` is `a`. – NetMage Aug 24 '18 at 20:33
  • This [article](https://en.wikipedia.org/wiki/Keyboard_layout#QWERTY-based_Latin-script_keyboard_layouts) has further explanation, such as: "for example, when the ⇧ Shift and numeric 2 keys are pressed simultaneously on a US keyboard; "@" is generated, and the key is engraved appropriately. On a UK keyboard this key combination generates the double-quote character, and UK keyboards are so engraved." – NetMage Aug 24 '18 at 20:37
  • @NetMage ok I got that issue, anyways it's probably usecase I'm ok with, so simply what I'm seeking out is how to convert something like string[] keypair ["ctrl","K"] into virtualkeycode[] keypair = {virtualkeycode.CONTROL, virtualkeycode.VK_K } ... I know how to do it with function keys and system keys... not with alpha/nums – fluffypuffy Aug 24 '18 at 20:46
  • I believe this [question](https://stackoverflow.com/q/33523071/2557128) and the `VkKeyScan` API call may prove helpful. – NetMage Aug 24 '18 at 21:08
  • 1
    The virtual key for letters is the same as their ASCII code. As long as your app doesn't stray too far from Western Europe and the Americas. Do note that this functionality is already built into the framework, used to set the shortcut for toolstrip and menu items for example. Makes it a one-liner: var key = (Keys)new KeysConverter().ConvertFrom("Ctrl+Shift+A"); – Hans Passant Aug 24 '18 at 22:16
  • @HansPassant this is exactly what I'm looking for! But unfortunately it seems like in VS 2017 does not know anything like KeysConverter, unless I do : public class KeysConverter : System.ComponentModel.TypeConverter, System.Collections.IComparer which doesnt seems right. Or is it? Thanks! – fluffypuffy Aug 25 '18 at 20:55
  • 1
    So, edited OP for some new observations. Thanks @HansPassant for that keysconverter, it helped me to go one step up! :) Anyway, need to expand my functionality a bit, not really sure how now. – fluffypuffy Aug 29 '18 at 09:52

0 Answers0