1

is there a way to find out which Keys or combination of Keys have to be pressed in order to obtain a specific character. It is easy for some character like '1', 'a', but not so easy for others, like'.', '_' or 'ü'. Any idea ? Thanks.

user1472131
  • 411
  • 1
  • 5
  • 15
  • Maybe this helps http://msdn.microsoft.com/en-us/library/ms646329 – V4Vendetta Jun 26 '13 at 07:38
  • possible duplicate of [How to convert a character in to equivalent System.Windows.Input.Key Enum value?](http://stackoverflow.com/questions/544141/how-to-convert-a-character-in-to-equivalent-system-windows-input-key-enum-value) – Philip Gullick Jun 26 '13 at 07:52
  • To Philip: not a duplicate System.Windows.Input.Key != System.Windows.Forms.Keys, but interesting link. Thanks – user1472131 Jun 26 '13 at 08:01
  • @user1472131, then can you please make your question more clear? I am struggling to find what you are asking. – Philip Gullick Jun 26 '13 at 08:06
  • This is not in general possible. It greatly depends on the active keyboard layout and a character may require *many* keypresses. Not just modifier keys like Shift and Ctrl but also dead keys, like the kind that are used to generate diacritics. Even special input like holding down Alt and pressing the keypad keys. Or an IME, common in East Asia. Not explaining why you need this prevents you from getting a better alternative. – Hans Passant Jun 26 '13 at 10:14

2 Answers2

0

Here's a full listing of 'special characters'. These are actually character entities, in which a specific code is given for one character.

This is mostly used in HTML in which the code will provide the sequence, yet on the client side they will see the character entity.

Over more here is a possible duplicate answer to yours, How to convert from Virtual Key codes to System.Windows.Forms.Keys

Edit There is also another duplicate with characters such as "." here: How to convert a character in to equivalent System.Windows.Input.Key Enum value?

Community
  • 1
  • 1
Philip Gullick
  • 995
  • 7
  • 22
0

Thanks to the links you posted I came to a solution that I post here. The input is a character, the output is a list of System.Windows.Forms.Keys. The first Keys is the actual key, the other keys in the list (if any) are the modifier keys.

    [DllImport("user32.dll")]
    static extern short VkKeyScan(char ch);

    public static List<Keys> FromCharacterToKeys(char c)
    {
        var key = BitConverter.GetBytes(VkKeyScan(c));
        if (key[1] > 0)
        {
            var list = new List<Keys> {(Keys) key[0]};
            list.AddRange(ConvertModifierByte(key[1]));
            return list;
        }
        else
        {
            return new List<Keys> { (Keys)key[0] }; 
        }    
    }

    private static List<Keys> ConvertModifierByte(byte modifier)
    {
        switch ((short)modifier)
        {
            case 1:
                return new List<Keys> { Keys.Shift };
            case 2:
                return new List<Keys> { Keys.Control };
            case 4:
                return new List<Keys> { Keys.Alt };
            case 6:
                return new List<Keys> { Keys.Control, Keys.Alt };
            default:
                return new List<Keys> { Keys.None };
        }
    }
user1472131
  • 411
  • 1
  • 5
  • 15