コード例 #1
0
 /// <summary>
 /// If this function returns true, there will be no attemt to decode the key press
 /// via ToUnicode to reveal Strg+Alt+E = € .
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 bool CheckIsNoUnicodekey(KeyboardRawEventArgs e)
 {
     Keys[] NoUnicodeKeys = { Keys.Cancel,             Keys.Tab,            Keys.LineFeed,           Keys.Clear,
                              Keys.Enter,              Keys.Return,         Keys.ShiftKey,           Keys.ControlKey,        Keys.Menu,
                              Keys.Pause,              Keys.CapsLock,       Keys.Capital,            Keys.KanaMode,          Keys.HanguelMode,
                              Keys.HangulMode,         Keys.JunjaMode,      Keys.FinalMode,          Keys.KanjiMode,         Keys.HanjaMode,
                              Keys.Escape,             Keys.IMEConvert,     Keys.IMENonconvert,      Keys.IMEAceept,         Keys.IMEAccept,
                              Keys.IMEModeChange,      Keys.Space,          Keys.Prior,              Keys.PageUp,            Keys.Next,
                              Keys.PageDown,           Keys.End,            Keys.Home,               Keys.Left,              Keys.Up,          Keys.Right,
                              Keys.Down,               Keys.Select,         Keys.Print,              Keys.Execute,           Keys.PrintScreen,
                              Keys.Snapshot,           Keys.Insert,         Keys.Delete,             Keys.Help,
                              Keys.LWin,               Keys.RWin,           Keys.Apps,               Keys.Sleep,
                              Keys.Multiply,           Keys.Add,            Keys.Separator,          Keys.Subtract,          Keys.Decimal,
                              Keys.Divide,             Keys.F1,             Keys.F2,                 Keys.F3,                Keys.F4,          Keys.F5,     Keys.F6,
                              Keys.F7,                 Keys.F8,             Keys.F9,                 Keys.F10,               Keys.F11,         Keys.F12,    Keys.F13,
                              Keys.F14,                Keys.F15,            Keys.F16,                Keys.F17,               Keys.F18,         Keys.F19,    Keys.F20,
                              Keys.F21,                Keys.F22,            Keys.F23,                Keys.F24,               Keys.NumLock,     Keys.Scroll,
                              Keys.LShiftKey,          Keys.RShiftKey,      Keys.LControlKey,        Keys.RControlKey,
                              Keys.LMenu,              Keys.RMenu,          Keys.BrowserBack,        Keys.BrowserForward,
                              Keys.BrowserRefresh,     Keys.BrowserStop,    Keys.BrowserSearch,
                              Keys.BrowserFavorites,   Keys.BrowserHome,    Keys.VolumeMute,         Keys.VolumeDown,
                              Keys.VolumeUp,           Keys.MediaNextTrack, Keys.MediaPreviousTrack, Keys.MediaStop,
                              Keys.MediaPlayPause,     Keys.LaunchMail,     Keys.SelectMedia,        Keys.LaunchApplication1,
                              Keys.LaunchApplication2, Keys.ProcessKey,
                              Keys.Packet,             Keys.Attn,           Keys.Crsel,              Keys.Exsel,             Keys.EraseEof,    Keys.Play,
                              Keys.Zoom,               Keys.NoName,         Keys.Pa1,                Keys.OemClear,          Keys.KeyCode,     Keys.Shift,
                              Keys.Control,            Keys.Alt };
     return(NoUnicodeKeys.Contains((Keys)e.vkCode));
 }
コード例 #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("");
        }
コード例 #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("");
        }
コード例 #4
0
        public static string ParseViaMapKeycode(KeyboardRawEventArgs e)
        {
            uint r = NativeMethodsKeyboard.MapVirtualKey((uint)e.vkCode,
                                                         NativeMethodsKeyboard.MAPVK_VK_TO_CHAR);

            return(((char)r).ToString());
        }
コード例 #5
0
 /// <summary>
 /// Raises the KeyEvent event.
 /// </summary>
 /// <param name="e">An instance of KeyboardRawEvent</param>
 public void OnKeyEvent(KeyboardRawEventArgs e)
 {
     if (KeyEvent != null)
     {
         KeyEvent(e);
     }
 }
コード例 #6
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("");
        }
コード例 #7
0
 bool CheckVkCodeIsModifier(KeyboardRawEventArgs e)
 {
     Keys[] ModifierKeys = { Keys.ControlKey, Keys.LControlKey, Keys.RControlKey,
                             Keys.ShiftKey,   Keys.LShiftKey,   Keys.RShiftKey,
                             Keys.LWin,       Keys.RWin,
                             Keys.Menu,       Keys.LMenu,       Keys.RMenu,
                             Keys.NumLock,    Keys.Scroll,
                             Keys.CapsLock };
     return(ModifierKeys.Contains((Keys)e.vkCode));
 }
コード例 #8
0
 /// <summary>
 /// Checks whether Alt, Shift, Control or CapsLock
 /// is enabled at the same time as another key.
 /// Modify the relevant sections and return type
 /// depending on what you want to do with modifier keys.
 /// </summary>
 private void CheckModifiers(KeyboardRawEventArgs e)
 {
     e.Shift     = CheckModifierDown(Keys.ShiftKey);
     e.Ctrl      = CheckModifierDown(Keys.ControlKey);
     e.Alt       = CheckModifierDown(Keys.Menu);
     e.Caps      = CheckModifierToggled(Keys.CapsLock);
     e.LWin      = CheckModifierDown(Keys.LWin);
     e.RWin      = CheckModifierDown(Keys.RWin);
     e.Numlock   = CheckModifierToggled(Keys.NumLock);
     e.Scrollock = CheckModifierToggled(Keys.Scroll);
     e.LShift    = CheckModifierDown(Keys.LShiftKey);
     e.RShift    = CheckModifierDown(Keys.RShiftKey);
     e.LCtrl     = CheckModifierDown(Keys.LControlKey);
     e.RCtrl     = CheckModifierDown(Keys.RControlKey);
     e.LAlt      = CheckModifierDown(Keys.LMenu);
     e.RAlt      = CheckModifierDown(Keys.RMenu);
 }
コード例 #9
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());
        }
コード例 #10
0
 public KeystrokeEventArgs(KeyboardRawEventArgs e)
     : base(e.Kbdllhookstruct)
 {
     this.Shift     = e.Shift;
     this.LShift    = e.LShift;
     this.RShift    = e.RShift;
     this.Ctrl      = e.Ctrl;
     this.LCtrl     = e.LCtrl;
     this.RCtrl     = e.RCtrl;
     this.Caps      = e.Caps;
     this.LWin      = e.LWin;
     this.RWin      = e.RWin;
     this.Alt       = e.Alt;
     this.LAlt      = e.LAlt;
     this.RAlt      = e.RAlt;
     this.Numlock   = e.Numlock;
     this.Scrollock = e.Scrollock;
     //this.Kbdllhookstruct = e.Kbdllhookstruct;
     this.keyState = e.keyState;
     this.Method   = e.Method;
 }
コード例 #11
0
        private void FixKeyStateArray(KeyboardRawEventArgs e)
        {
            if (e.Uppercase)
            {
                e.keyState[(int)Keys.ShiftKey] = 129;
            }
            return;

            e.keyState[(int)Keys.LShiftKey]   = (byte)(e.LShift ? 129 : 1);
            e.keyState[(int)Keys.RShiftKey]   = (byte)(e.RShift ? 129 : 1);
            e.keyState[(int)Keys.CapsLock]    = (byte)(e.Caps ? 129 : 1);
            e.keyState[(int)Keys.ControlKey]  = (byte)(e.Ctrl ? 129 : 1);
            e.keyState[(int)Keys.Menu]        = (byte)(e.Alt ? 129 : 1);
            e.keyState[(int)Keys.LWin]        = (byte)(e.LWin ? 129 : 1);
            e.keyState[(int)Keys.RWin]        = (byte)(e.RWin ? 129 : 1);
            e.keyState[(int)Keys.NumLock]     = (byte)(e.Numlock ? 129 : 1);
            e.keyState[(int)Keys.Scroll]      = (byte)(e.Scrollock ? 129 : 1);
            e.keyState[(int)Keys.LControlKey] = (byte)(e.LCtrl ? 129 : 1);
            e.keyState[(int)Keys.RControlKey] = (byte)(e.RCtrl ? 129 : 1);
            e.keyState[(int)Keys.LMenu]       = (byte)(e.LAlt ? 129 : 1);
            e.keyState[(int)Keys.RMenu]       = (byte)(e.RAlt ? 129 : 1);
        }
コード例 #12
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);
        }
コード例 #13
0
 bool CheckIsFunctionKey(KeyboardRawEventArgs e)
 {
     return((int)Keys.F1 <= e.vkCode && e.vkCode <= (int)Keys.F24);
 }
コード例 #14
0
 bool CheckIsNumericFromNumbers(KeyboardRawEventArgs e)
 {
     return(CheckIsNumericFromNumbers(e.vkCode));
 }
コード例 #15
0
 bool CheckIsAlpha(KeyboardRawEventArgs e)
 {
     return(CheckIsAlpha(e.vkCode));
 }
コード例 #16
0
        /// <summary>
        /// Gets a KeyboardRawKeyEvent, parses it and forwards it via
        /// the KeystrokeEvent
        /// </summary>
        /// <param name="e"></param>
        void hook_KeyEvent(KeyboardRawEventArgs raw_e)
        {
            KeystrokeEventArgs e = new KeystrokeEventArgs(raw_e);

            e.IsAlpha              = CheckIsAlpha(e);
            e.IsNumericFromNumpad  = CheckIsNumericFromNumpad(e);
            e.IsNumericFromNumbers = CheckIsNumericFromNumbers(e);
            e.IsNoUnicodekey       = CheckIsNoUnicodekey(e);
            e.IsFunctionKey        = CheckIsFunctionKey(e);
            e.ModifierToggledEvent = CheckVkCodeIsModifier(e);

            Log.e("KP", "   alpha:" + e.IsAlpha.ToString());

            if (e.Method == KeyUpDown.Down)
            {
                ApplyDeadKey(e);
                if (e.IsAlpha && e.OnlyShiftOrCaps)
                {
                    e.KeyString         = ParseChar(e);
                    e.ShouldBeDisplayed = true;
                    e.StrokeType        = KeystrokeType.Text;
                    e.Deletable         = true;
                    Log.e("KP", "   IsAlpha and OnlyShiftOrCaps > ParseChar");
                }
                else if (e.IsNumeric && e.NoModifiers)
                {
                    e.KeyString         = ParseNumeric(e);
                    e.ShouldBeDisplayed = true;
                    e.StrokeType        = KeystrokeType.Text;
                    e.Deletable         = true;
                    Log.e("KP", "   e.IsNumeric && e.NoModifiers > ParseNumeric");
                }
                else if (e.ModifierToggledEvent) // vkcode is modifier
                {
                    e.ShouldBeDisplayed = false;
                    AddModifier((Keys)e.vkCode, e);
                    e.StrokeType = KeystrokeType.Modifiers;
                    Log.e("KP", "   e.ModifierToggledEvent > AddModifier " + ((Keys)e.vkCode).ToString());
                }
                else if (e.IsNoUnicodekey && e.NoModifiers)
                {
                    ParseTexttViaSpecialkeysParser(e);
                    e.Deletable = IsDeletableSpecialKey(e.Key);
                    Log.e("KP", "   e.IsNoUnicodekey && e.NoModifiers > ParseTexttViaSpecialkeysParser ");
                }
                else if (e.IsNoUnicodekey && !e.NoModifiers) // Shortcut
                {
                    ParseShortcutViaSpecialkeysParser(e);
                    Log.e("KP", "   e.IsNoUnicodekey && !e.NoModifiers > ParseShortcutViaSpecialkeysParser ");
                }
                else if (e.NoModifiers) // Simple Key, but not alphanumeric (first try special then unicode)
                {
                    Log.e("KP", "   e.NoModifiers > try SpecialkeysParser.ToString ");
                    try
                    {
                        e.KeyString = SpecialkeysParser.ToString((Keys)e.vkCode);
                    }
                    catch (NotImplementedException)
                    {
                        Log.e("KP", "   e.NoModifiers 2> try KeyboardLayoutParser.ParseViaToUnicode ");
                        e.KeyString = KeyboardLayoutParser.ParseViaToUnicode(e);
                        BackupDeadKey(e);
                    }
                    e.ShouldBeDisplayed         = true;
                    e.StrokeType                = KeystrokeType.Text;
                    e.RequiresNewLineAfterwards = (Keys)e.vkCode == Keys.Return;
                }
                else if (e.OnlyShiftOrCaps) //  special char, but only Shifted, eg ;:_ÖÄ'*ÜP
                // (e.IsNoUnicodekey is always false here -> could be a unicode key combinatin)
                {
                    e.KeyString = KeyboardLayoutParser.ParseViaToUnicode(e);
                    BackupDeadKey(e);
                    Log.e("KP", "   e.OnlyShiftOrCaps > try KeyboardLayoutParser.ParseViaToUnicode ");

                    if (e.KeyString != "")
                    {
                        e.ShouldBeDisplayed = true;
                        e.StrokeType        = KeystrokeType.Text;
                        e.Deletable         = true;
                    }
                    else
                    {
                        // Is no unicode key combination? maybe a shortcut then: Shift + F2
                        ParseShortcutViaSpecialkeysParser(e);
                        Log.e("KP", "   e.OnlyShiftOrCaps 2> ParseShortcutViaSpecialkeysParser ");
                    }
                }
                else if (!e.NoModifiers && !e.OnlyShiftOrCaps) // Special Char with Strg + Alt
                // or shortcut else
                {
                    // could be something like the german @ (Ctrl + Alt + Q)
                    // Temporary disabled because ToUnicode returns more often values than it should
                    e.KeyString = ""; //KeyboardLayoutParser.ParseViaToUnicode(e);
                    Log.e("KP", "   !e.NoModifiers && !e.OnlyShiftOrCapss > KeyboardLayoutParser.ParseViaToUnicode");
                    // all other special char keycodes do not use Shift
                    if (e.KeyString != "" && !e.Shift && !e.IsNoUnicodekey)
                    {
                        e.ShouldBeDisplayed = true;
                        e.StrokeType        = KeystrokeType.Text;
                        e.Deletable         = true;
                    }
                    else // Shortcut
                    {
                        ParseShortcutViaSpecialkeysParser(e);
                        string possibleChar = KeyboardLayoutParser.ParseViaToUnicode(e);
                        BackupDeadKey(e);
                        if (possibleChar != "" && !CheckIsAlpha(possibleChar) &&
                            !CheckIsNumeric(possibleChar) &&
                            CheckIsASCII(possibleChar))
                        {
                            e.KeyString += " (" + possibleChar + ")";
                        }
                        Log.e("KP", "   !e.NoModifiers && !e.OnlyShiftOrCapss 2> ParseShortcutViaSpecialkeysParser ");
                    }
                }
                Log.e("KP", "   str:" + e.KeyString);
            }

            if (e.Method == KeyUpDown.Up)
            {
                if (e.ModifierToggledEvent) // vkcode is modifier
                {
                    e.ShouldBeDisplayed = false;
                    RemoveModifier((Keys)e.vkCode, e);
                    e.StrokeType = KeystrokeType.Modifiers;
                }
                Log.e("KP", "   code:" + ((Keys)e.vkCode).ToString());

                // only react to modifiers on key up, nothing else
            }

            if (e.StrokeType != KeystrokeType.Undefined)
            {
                OnKeystrokeEvent(e);
            }
        }