private IntPtr OnKeyPressed(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (ignore_next_key_press)
            {
                ignore_next_key_press = false;
                return(CallNextHookEx(win_api_hook_id, nCode, wParam, lParam));
            }
            if (nCode >= 0)
            {
                Dictionary <System.Windows.Forms.Keys, Key> bindings = new Dictionary <System.Windows.Forms.Keys, Key>
                {
                    { System.Windows.Forms.Keys.LWin, Key.Modifier },
                    { System.Windows.Forms.Keys.J, Key.LeftMouseButton },
                    { System.Windows.Forms.Keys.K, Key.RightMouseButton },
                    { System.Windows.Forms.Keys.N, Key.ScrollDown },
                    { System.Windows.Forms.Keys.H, Key.ScrollUp },
                    { System.Windows.Forms.Keys.Oemcomma, Key.ScrollLeft },
                    { System.Windows.Forms.Keys.OemPeriod, Key.ScrollRight },
                    { System.Windows.Forms.Keys.A, Key.CalibrateLeft },
                    { System.Windows.Forms.Keys.D, Key.CalibrateRight },
                    { System.Windows.Forms.Keys.W, Key.CalibrateUp },
                    { System.Windows.Forms.Keys.S, Key.CalibrateDown },
                    { System.Windows.Forms.Keys.Escape, Key.StopCalibration },
                    { System.Windows.Forms.Keys.Space, Key.Accessibility_SaveCalibration },
                };

                System.Windows.Forms.Keys key_code = (System.Windows.Forms.Keys)Marshal.ReadInt32(lParam);
                Key key = Key.Unbound;
                if (bindings.ContainsKey(key_code))
                {
                    key = bindings[key_code];
                }
                KeyState key_state;

                if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
                {
                    key_state = KeyState.Down;
                }
                else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
                {
                    key_state = KeyState.Up;
                }
                else
                {
                    return(CallNextHookEx(win_api_hook_id, nCode, wParam, lParam));
                }

                if (receiver.OnKeyPressed(key, key_state, Helpers.IsModifier(key_code), this))
                {
                    return(new IntPtr(1));
                }
            }

            return(CallNextHookEx(win_api_hook_id, nCode, wParam, lParam));
        }
Пример #2
0
        public void OnKeyPressed(object sender, Interceptor.KeyPressedEventArgs e)
        {
            // Console.WriteLine(e.Key);
            // Console.WriteLine(e.State);

            lock (Helpers.locker)
            {
                e.Handled = true;

                // Make sure capslock is always disabled when it is used as a modifier.
                if (Options.Instance.key_bindings[Key.Modifier] == Interceptor.Keys.CapsLock)
                {
                    UncheckCapsLock();
                }

                // Interceptor.KeyState is a mess. Different Keys produce different KeyState when pressed and released.
                bool     is_e0_key = (e.State & Interceptor.KeyState.E0) != 0;
                KeyState key_state;
                if (e.State == Interceptor.KeyState.E0 || e.State == Interceptor.KeyState.Down)
                {
                    key_state = KeyState.Down;
                }
                else if ((e.State & Interceptor.KeyState.Up) != 0)
                {
                    key_state = KeyState.Up;
                }
                else
                {
                    e.Handled = false;
                    return;
                }

                // Hardcoded stop-word is Win+Del.
                if (e.Key == Interceptor.Keys.WindowsKey)
                {
                    if (key_state == KeyState.Down)
                    {
                        is_win_pressed = true;
                    }
                    else if (key_state == KeyState.Up)
                    {
                        is_win_pressed = false;
                    }
                }
                if (e.Key == Interceptor.Keys.Delete &&
                    key_state == KeyState.Down && is_win_pressed)
                {
                    Environment.Exit(0);
                    return;
                }

                if (key_state == KeyState.Down && read_key_callback != null)
                {
                    read_key_callback(new ReadKeyResult {
                        is_e0_key = is_e0_key, key = e.Key
                    });
                    read_key_callback = null;
                    e.Handled         = true;
                    return;
                }

                // Convert |Interceptor.Keys| to |eye_tracking_mouse.Key|
                var key_bindings = Options.Instance.key_bindings;
                Key key          = Key.Unbound;
                if (key_bindings.bindings.ContainsValue(e.Key))
                {
                    key = key_bindings.bindings.First(pair =>
                    {
                        return(pair.Value == e.Key);
                    }).Key;
                }

                if (key == Key.Modifier && key_bindings.is_modifier_e0 != is_e0_key)
                {
                    key = Key.Unbound;
                }

                e.Handled = receiver.OnKeyPressed(key, key_state, Helpers.IsModifier(e.Key), this);
            }
        }