Exemple #1
0
        public static string ParseViaMapKeycode(KeyboardRawEventArgs e)
        {
            uint r = NativeMethodsKeyboard.MapVirtualKey((uint)e.vkCode,
                                                         NativeMethodsKeyboard.MAPVK_VK_TO_CHAR);

            return(((char)r).ToString());
        }
Exemple #2
0
        public static string ParseViaToAscii(KeyboardRawEventArgs e)
        {
            byte[] inBuffer   = new byte[2];
            int    buffertype = NativeMethodsKeyboard.ToAscii(e.vkCode,
                                                              e.Kbdllhookstruct.scanCode,
                                                              e.keyState,
                                                              inBuffer,
                                                              e.Alt ? 1 : 0);

            if (buffertype < 0) // deadkey
            {
            }
            else if (buffertype == 1) // one char in inBuffer[0]
            {
                char key = (char)inBuffer[0];
                return(key.ToString());
            }
            else if (buffertype == 2) // two chars in inBuffer
            {
                char key  = (char)inBuffer[0];
                char key2 = (char)inBuffer[1];
                return(key.ToString() + key2.ToString());
            }
            else if (buffertype == 0)
            {
                // no translation
            }
            return("");
        }
Exemple #3
0
        public static string ProcessDeadkeyWithNextKey(KeyboardRawEventArgs dead, KeyboardRawEventArgs e)
        {
            Log.e("KP", "    ProcessDeadkeyWithNextKey ");
            StringBuilder inBuffer   = new StringBuilder(128);
            int           buffertype = NativeMethodsKeyboard.ToUnicode(dead.vkCode,
                                                                       dead.Kbdllhookstruct.scanCode,
                                                                       dead.keyState,
                                                                       inBuffer,
                                                                       128,
                                                                       (uint)(dead.Alt ? 1 : 0));

            Log.e("KP", "      FirstBuffertype " + buffertype.ToString());
            buffertype = NativeMethodsKeyboard.ToUnicode(e.vkCode,
                                                         e.Kbdllhookstruct.scanCode,
                                                         e.keyState,
                                                         inBuffer,
                                                         128,
                                                         (uint)(e.Alt ? 1 : 0));
            Log.e("KP", "      SecondBuffertype " + buffertype.ToString());
            Log.e("KP",
                  String.Format("   ToUnicode: bl {0} str {1} alt {2} vk {3}", buffertype,
                                inBuffer.ToString(), e.Alt, e.vkCode));

            if (buffertype >= 1) // buffertype chars in inBuffer[0..buffertype]
            {
                return(inBuffer.ToString(0, buffertype));
            }
            else if (buffertype == 0)
            {
                // no translation
            }
            return("");
        }
Exemple #4
0
        public static string ParseViaToUnicode(KeyboardRawEventArgs e)
        {
            StringBuilder inBuffer   = new StringBuilder(128);
            int           buffertype = NativeMethodsKeyboard.ToUnicode(e.vkCode,
                                                                       e.Kbdllhookstruct.scanCode,
                                                                       e.keyState,
                                                                       inBuffer,
                                                                       128,
                                                                       (uint)(e.Alt ? 1 : 0));

            Log.e("KP", "    FirstBuffertype " + buffertype.ToString());

            Log.e("KP",
                  String.Format("   ToUnicode: bl {0} str {1} alt {2} vk {3}", buffertype,
                                inBuffer.ToString(), e.Alt, e.vkCode));
            string keystate = "";

            for (int i = 0; i < e.keyState.Length; i++)
            {
                if (e.keyState[i] != 0)
                {
                    keystate += " " + ((WindowsVirtualKey)i).ToString() + ":" + e.keyState[i];
                }
            }

            Log.e("KP", "             : " + keystate);

            // call ToUnicode again, otherwise it will destoy the dead key for the rest of the system
            int buffertype2 = NativeMethodsKeyboard.ToUnicode(e.vkCode,
                                                              e.Kbdllhookstruct.scanCode,
                                                              e.keyState,
                                                              inBuffer,
                                                              128,
                                                              (uint)(e.Alt ? 1 : 0));

            Log.e("KP", "    SecondBuffertype " + buffertype2.ToString() + " & deadkey");

            if (buffertype < 0) // deadkey
            {
                // return DEADKEY, so the next key can try to assemble the deadkey
                //return "DEADKEY";
                return(buffertype2 >= 1 ? inBuffer.ToString(0, 1) : "");
            }
            else if (buffertype2 < 0) // type two dead keys in a row
            {
                Log.e("KP", "    TwoDeadKeysInARow " + buffertype2.ToString() + " & deadkey");
                return(buffertype >= 1 ? inBuffer.ToString(0, 1) : "");
            }
            else if (buffertype2 >= 1) // buffertype chars in inBuffer[0..buffertype]
            {
                return(inBuffer.ToString(0, buffertype2));
            }
            else if (buffertype2 == 0)
            {
                // no translation
            }
            return("");
        }
Exemple #5
0
 /// <summary>
 /// Unregisters the winapi hook for global key events
 /// </summary>
 private void UnregisterKeyboardHook()
 {
     if (hookID == IntPtr.Zero)
     {
         return;
     }
     NativeMethodsKeyboard.UnhookWindowsHookEx(hookID);
     hookID = IntPtr.Zero;
 }
Exemple #6
0
        /// <summary>
        /// Uses the winapi to check if the VK key modifiercode is pressed
        /// </summary>
        /// <param name="modifiercode"></param>
        /// <returns></returns>
        private bool CheckModifierDown(Keys modifiercode)
        {
            // SHORT NativeMethodsKeyboard.GetKeyState(int)
            // The return value specifies the status of the specified virtual key, as follows:

            // - If the high-order bit is 1, the key is down; otherwise, it is up.
            // - If the low-order bit is 1, the key is toggled. A key, such as
            //   the CAPS LOCK key, is toggled if it is turned on. The key is
            //   off and untoggled if the low-order bit is 0. A toggle key's
            //   indicator light (if any) on the keyboard will be on when the
            //   key is toggled, and off when the key is untoggled.
            return((NativeMethodsKeyboard.GetKeyState((int)modifiercode) & 0x8000) != 0);
        }
Exemple #7
0
 /// <summary>
 /// Registers the function HookCallback for the global key events winapi
 /// </summary>
 private void RegisterKeyboardHook()
 {
     if (hookID != IntPtr.Zero)
     {
         return;
     }
     proc = new NativeMethodsKeyboard.HookHandlerDelegate(HookCallback);
     using (Process curProcess = Process.GetCurrentProcess())
     {
         using (ProcessModule curModule = curProcess.MainModule)
         {
             hookID = NativeMethodsKeyboard.SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                                                             NativeMethodsKeyboard.GetModuleHandle(curModule.ModuleName), 0);
         }
     }
 }
Exemple #8
0
        public static string Parse(KeyboardRawEventArgs e)
        {
            StringBuilder sb     = new StringBuilder(128);
            int           lParam = 0;

            // Bits in lParam
            // 16-23	Scan code.
            // 24	Extended-key flag. Distinguishes some keys on an enhanced keyboard.
            // 25	"Do not care" bit. The application calling this function sets this
            //      bit to indicate that the function should not distinguish between left
            //      and right CTRL and SHIFT keys, for example.

            lParam = e.Kbdllhookstruct.scanCode << 16;

            int result = NativeMethodsKeyboard.GetKeyNameText(lParam, sb, 128);

            return(sb.ToString());
        }
Exemple #9
0
        /// <summary>
        /// Processes the key event captured by the hook.
        /// </summary>
        private IntPtr HookCallback(int nCode,
                                    IntPtr wParam,
                                    ref NativeMethodsKeyboard.KBDLLHOOKSTRUCT lParam)
        {
            if (nCode >= 0)
            {
                KeyboardRawEventArgs e = new KeyboardRawEventArgs(lParam);
                e.keyState = new byte[256];
                NativeMethodsKeyboard.GetKeyboardState(e.keyState);

                if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
                {
                    e.Method = KeyUpDown.Down;
                }
                else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
                {
                    e.Method = KeyUpDown.Up;
                }

                if (e.Method != KeyUpDown.Undefined)
                {
                    CheckModifiers(e);
                    FixKeyStateArray(e);
                    Log.e("KE", "EVENT " + e.Method.ToString() + " shift:" + e.Uppercase.ToString());
                    OnKeyEvent(e);
                }

                /*System.Diagnostics.Debug.WriteLine(
                 *  String.Format("Key: sc {0} vk {1} ext {2} fl {3}, {4}", lParam.scanCode,
                 *      lParam.vkCode, lParam.dwExtraInfo, lParam.flags, e.Method));
                 */
            }
            //Pass key to next application
            IntPtr ret = NativeMethodsKeyboard.CallNextHookEx(hookID, nCode, wParam, ref lParam);

            return(ret);
        }
Exemple #10
0
 /// <summary>
 /// Uses the winapi to check weather the caps/numlock/scrolllock is activated
 /// </summary>
 /// <param name="modifiercode"></param>
 /// <returns></returns>
 private bool CheckModifierToggled(Keys modifiercode)
 {
     return((NativeMethodsKeyboard.GetKeyState((int)modifiercode) & 0x0001) != 0);
 }