Missing method Retreive Key Character

Dec 21, 2012 at 12:47 PM

I know this not a discussion but a contribution but her it goes anyway:

Hi when working with this control I reached the limitation of not knowing the exact key for the OEM keys, This changes with the Keyboard Layout (language). So I added these methods to extract the correct character and place it on the virtual keyboard UI:


        static extern int ToUnicode(uint virtualKeyCode, uint scanCode,
            byte[] keyboardState,
            [Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
            StringBuilder receivingBuffer,
            int bufferSize, uint flags);



 public static string GetCharsFromKeys(VirtualKeyCode keys, bool shift, bool altGr)
            var buf = new StringBuilder(256);
            var keyboardState = new byte[256];
            if (shift)
                keyboardState[(int)VirtualKeyCode.SHIFT] = 0xff;
            if (altGr)
                keyboardState[(int)VirtualKeyCode.CONTROL] = 0xff;
                keyboardState[(int)VirtualKeyCode.MENU] = 0xff;
            ToUnicode((uint)keys, 0, keyboardState, buf, 256, 0);
            return buf.ToString();


This way I can extract all the correct character for all keys and place it on UI, disregarding the globalization.