Accent Problem fixed (áéô etc)

Feb 6, 2015 at 2:52 PM
Hi,

i found a problem in this solution and the input of accent (dead keys) like áéò etc...

my solution looks like this:

you need to change the method TryGetCharFromKeyboardState in the KeyboardNativeMethods class:



        private static int lastVKCode = 0;
        private static int lastScanCode = 0;
        private static byte[] lastKeyState = new byte[255];
        private static bool lastIsDead = false;

        private static void ClearKeyboardBuffer(int vk, int sc, uint hkl)
        {
            var sb = new StringBuilder(10);
            int rc;

            do
            {
                byte[] lpKeyStateNull = new Byte[255];
                rc = ToUnicodeEx(vk, sc, lpKeyStateNull, sb, sb.Capacity, 0, hkl);
            }
            while (rc < 0);
        }

        /// <summary>
        /// Translates a virtual key to its character equivalent using a specified keyboard layout
        /// </summary>
        /// <param name="lParam"></param>
        /// <param name="virtualKeyCode"></param>
        /// <param name="scanCode"></param>
        /// <param name="fuState"></param>
        /// <param name="dwhkl"></param>
        /// <param name="ch"></param>
        /// <returns></returns>
        internal static bool TryGetCharFromKeyboardState(IntPtr lParam, int virtualKeyCode, int scanCode, int fuState, uint dwhkl, out char ch)
        {
            StringBuilder pwszBuff = new StringBuilder(64);
            KeyboardState keyboardState = KeyboardState.GetCurrent();
            byte[] currentKeyboardState = keyboardState.GetNativeState();

            int result = ToUnicodeEx(virtualKeyCode, scanCode, currentKeyboardState, pwszBuff, pwszBuff.Capacity,
                                         fuState, dwhkl);

            bool isDead = false;

            ch = (char) 0;

            switch (result)
            {
                case -1:// Dead keys (^,`...)
                    isDead = true;
                    // We must clear the buffer because ToUnicodeEx messed it up, see below.
                    ClearKeyboardBuffer(virtualKeyCode, scanCode, dwhkl);
                    break;
                case 0:
                    break;
                // Single character in buffer
                case 1:
                    ch = pwszBuff[0];
                    break;
            }

            if (lastVKCode != 0 && lastIsDead)
            {
                var sbTemp = new StringBuilder(5);
                ToUnicodeEx(lastVKCode, lastScanCode, lastKeyState, sbTemp, sbTemp.Capacity, (int)0, dwhkl);
                lastVKCode = 0;

                return true;
            }

            // Save these
            lastScanCode = scanCode;
            lastVKCode = virtualKeyCode;
            lastIsDead = isDead;
            lastKeyState = (byte[])currentKeyboardState.Clone();

            return (ch != (char) 0);
        }
Thanks to https://gist.github.com/Ciantic/471698#file-keyboardlistener-cs whose implementation helped me solve the problem...

Regards