Example #1
0
        private IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            int message = wParam.ToInt32();

            if (nCode >= 0)
            {
                KBDLLHOOKSTRUCT kbInfo   = Marshal.PtrToStructure <KBDLLHOOKSTRUCT>(lParam);
                int             scancode = kbInfo.scanCode;

                if ((kbInfo.flags & 1) == 1) // Extended key
                {
                    scancode = (scancode & 0xff) + 0x80;
                }

                if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
                {
                    if ((pressedKeys[scancode] & 2) == 0)
                    {
                        pressedKeys[scancode] ^= 1;
                        pressedKeys[scancode] |= 4;
                    }
                    pressedKeys[scancode] |= 2;
                }

                if (message == WM_KEYUP || message == WM_SYSKEYUP)
                {
                    pressedKeys[scancode] &= 0xFD;
                }
            }

            return(WinAPI.CallNextHookEx(hookKB, nCode, wParam, lParam));
        }
        private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
        {
            string chars = "";

            if (nCode >= 0)
            {
                if (wParam.ToUInt32() == (int)WinAPI.WM.KEYDOWN ||
                    wParam.ToUInt32() == (int)WinAPI.WM.KEYUP ||
                    wParam.ToUInt32() == (int)WinAPI.WM.SYSKEYDOWN ||
                    wParam.ToUInt32() == (int)WinAPI.WM.SYSKEYUP)
                {
                    // Captures the character(s) pressed only on WM_KEYDOWN
                    // NOTE: Currently not used because it's causing a bug :( -ak

                    /*
                     * chars = InterceptKeys.VKCodeToString((uint)Marshal.ReadInt32(lParam),
                     *  (wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_KEYDOWN ||
                     *  wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_SYSKEYDOWN));
                     */

                    int vkCode = Marshal.ReadInt32(lParam);

                    Key key = System.Windows.Input.KeyInterop.KeyFromVirtualKey(vkCode);
                    hookedKeyboardCallbackAsync.BeginInvoke(wParam.ToUInt32(), vkCode, chars, null, null);

                    if (this.hotkeys.GetHotkey(key) != GlobalHotkeys.Purpose.None &&
                        this.form.ContainsForegroundWindow())
                    {
                        return((IntPtr)1);
                    }
                }
            }

            return(WinAPI.CallNextHookEx(hookId, nCode, wParam, lParam));
        }
        private IntPtr Callback(int code, IntPtr wParam, ref MSG lParam)
        {
            if (code == 0 && lParam.message == WM.DROPFILES)
            {
                POINT pos;
                WinAPI.DragQueryPoint(lParam.wParam, out pos);

                // 0xFFFFFFFF as index makes the method return the number of files
                uint n  = WinAPI.DragQueryFile(lParam.wParam, 0xFFFFFFFF, null, 0);
                var  sb = new System.Text.StringBuilder(1024);

                List <string> result = new List <string>();
                for (uint i = 0; i < n; i++)
                {
                    int len = (int)WinAPI.DragQueryFile(lParam.wParam, i, sb, 1024);
                    result.Add(sb.ToString(0, len));
                    sb.Length = 0;
                }
                WinAPI.DragFinish(lParam.wParam);
                if (OnDroppedFiles != null)
                {
                    OnDroppedFiles(result);
                }
                if (OnDroppedFilesWithPoint != null)
                {
                    OnDroppedFilesWithPoint(result, pos);
                }
                WinAPI.SetForegroundWindow(mainWindow);
            }
            return(WinAPI.CallNextHookEx(m_Hook, code, wParam, ref lParam));
        }
Example #4
0
 /// <summary>
 /// A callback function which will be called every Time a keyboard activity detected.
 /// </summary>
 /// <param name="nCode">
 /// [in] Specifies whether the hook procedure must process the message.
 /// If nCode is HC_ACTION, the hook procedure must process the message.
 /// If nCode is less than zero, the hook procedure must pass the message to the
 /// CallNextHookEx function without further processing and must return the
 /// value returned by CallNextHookEx.
 /// </param>
 /// <param name="wParam">
 /// [in] Specifies whether the message was sent by the current thread.
 /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
 /// </param>
 /// <param name="lParam">
 /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
 /// </param>
 /// <returns>
 /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
 /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
 /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
 /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
 /// procedure does not call CallNextHookEx, the return value should be zero.
 /// </returns>
 private static int SysMessageHookProc(int nCode, Int32 wParam, IntPtr lParam)
 {
     if (nCode == WinAPI.SC_SCREENSAVE)
     {
         return(-1);
     }
     else
     {
         return(WinAPI.CallNextHookEx(s_SysMessageHookHandle, nCode, wParam, lParam));
     }
 }
Example #5
0
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

                NewKeyboardEvent(wParam, hookStruct);
            }

            return(WinAPI.CallNextHookEx(_hookID, nCode, wParam, lParam));
        }
Example #6
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var  hookStruct = Marshal.PtrToStructure <KBDLLHOOKSTRUCT>(lParam);
                var  flags      = hookStruct.Flags;
                Keys vkCode     = (Keys)hookStruct.VkCode;

                if (wParam == (IntPtr)Constants.WM_KEYDOWN || (GetSystemKeyEvent &&
                                                               wParam == (IntPtr)Constants.WM_SYSKEYDOWN))
                {
                    NonStopKeyDownKey.Add(vkCode);
                    if (KeyDown?.Invoke(vkCode) == false)
                    {
                        return((IntPtr)1);
                    }
                }
                //new Thread(() =>
                //{
                //    for (int i = 0; i < keyDownCallbackEvents.Count; i++)
                //        if (keyDownCallbackEvents[i].Key == vkCode && keyDownCallbackEvents[i].Callback != null)
                //            keyDownCallbackEvents[i].Callback.DynamicInvoke(vkCode);
                //}) { IsBackground = true }.Start();

                if (wParam == (IntPtr)Constants.WM_KEYUP || (GetSystemKeyEvent &&
                                                             wParam == (IntPtr)Constants.WM_SYSTEMKEYUP))
                {
                    NonStopKeyUpKey.Add(vkCode);
                    if (KeyUp?.Invoke(vkCode) == false)
                    {
                        return((IntPtr)1);
                    }
                }
                //new Thread(() =>
                //{
                //    for (int i = 0; i < keyUpCallbackEvents.Count; i++)
                //    if (keyUpCallbackEvents[i].Key == vkCode && keyDownCallbackEvents[i].Callback != null)
                //        keyDownCallbackEvents[i].Callback.DynamicInvoke(vkCode);
                //})
                //{ IsBackground = true }.Start();
            }

            return(WinAPI.CallNextHookEx(_hookID, nCode, wParam, lParam));
        }
Example #7
0
        protected override IntPtr OnHook(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                switch ((WinAPI.WindowMessage)wParam)
                {
                case WinAPI.WindowMessage.WM_KEYDOWN:
                    KeyDown?.Invoke(this, new KeyEventExtArgs((Keys)vkCode, UpDown.Down));
                    break;

                case WinAPI.WindowMessage.WM_KEYUP:
                    KeyUp?.Invoke(this, new KeyEventExtArgs((Keys)vkCode, UpDown.Up));
                    break;
                }
            }

            return(WinAPI.CallNextHookEx(hHook, nCode, wParam, lParam));
        }
Example #8
0
    private IntPtr Callback(int code, IntPtr wParam, ref MSG lParam)
    {
        if (lParam.message != WM.NCMBUTTONDOWN &&
            lParam.message != WM.MOUSEHWHEEL &&
            lParam.message != WM.MOUSEWHEEL &&
            lParam.message != WM.INPUT &&
            lParam.message != WM.MOUSEFIRST &&
            lParam.message != WM.NCMOUSEMOVE &&
            lParam.message != WM.NCMOUSELEAVE)
        {
            // UnityEngine.Debug.LogWarning("Callback outSide: " + code + ":" + wParam + ":" + lParam.message);
        }

        if (code == 0 && lParam.message == WM.DROPFILES)
        {
            UnityEngine.Debug.Log("Callback inSide: " + code + ":" + wParam);

            POINT pos;
            WinAPI.DragQueryPoint(lParam.wParam, out pos);

            UnityEngine.Debug.Log("Callback inSide: " + pos.ToString());
            // 0xFFFFFFFF as index makes the method return the number of files
            uint n  = WinAPI.DragQueryFile(lParam.wParam, 0xFFFFFFFF, null, 0);
            var  sb = new System.Text.StringBuilder(1024);

            List <string> result = new List <string>();
            for (uint i = 0; i < n; i++)
            {
                int len = (int)WinAPI.DragQueryFile(lParam.wParam, i, sb, 1024);
                result.Add(sb.ToString(0, len));
                sb.Length = 0;
            }

            WinAPI.DragFinish(lParam.wParam);
            if (OnDroppedFiles != null)
            {
                OnDroppedFiles(result, pos);
            }
        }

        return(WinAPI.CallNextHookEx(m_Hook, code, wParam, ref lParam));
    }
Example #9
0
        private IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            int message = wParam.ToInt32();

            if (nCode >= 0)
            {
                MSLLHOOKSTRUCT mInfo = Marshal.PtrToStructure <MSLLHOOKSTRUCT>(lParam);

                switch (message)
                {
                case WM_MOUSEWHEEL:
                    wheelState = (mInfo.mouseData > 0) ? 6 : 7;
                    MW_TICK    = mInfo.time;
                    break;

                case WM_MOUSEMOVE:
                    MOUSE_X = mInfo.x;
                    MOUSE_Y = mInfo.y;
                    break;
                }
            }

            return(WinAPI.CallNextHookEx(hookKB, nCode, wParam, lParam));
        }
Example #10
0
        /// <summary>
        /// The callback for the keyboard hook
        /// </summary>
        /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
        /// <param name="wParam">The event type</param>
        /// <param name="lParam">The keyhook event information</param>
        /// <returns></returns>
        public IntPtr HookCallback(int code, IntPtr wParam, ref WinAPI.KeyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                var key = (System.Windows.Forms.Keys)lParam.vkCode;

                if (_hookedKeys.Contains(key))
                {
                    var wpfKey    = InputUtil.WinformsToWPFKey(key);
                    var eventArgs = new GlobalKeyEventArgs(wpfKey, IsShiftPressed(), IsCtrlPressed(), IsAltPressed());

                    if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
                    {
                        KeyDown?.Invoke(this, eventArgs);
                    }
                    else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
                    {
                        KeyUp?.Invoke(this, eventArgs);
                    }
                }
            }

            return(WinAPI.CallNextHookEx(hookID, code, wParam, ref lParam));
        }
Example #11
0
        /// <summary>
        /// A callback function which will be called every Time a keyboard activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            bool handled = false;

            if (nCode >= 0)
            {
                //read structure KeyboardHookStruct at lParam
                WinAPI.KeyboardHookStruct MyKeyboardHookStruct = (WinAPI.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(WinAPI.KeyboardHookStruct));
                //raise KeyDown
                if (s_KeyDown != null && (wParam == WinAPI.WM_KEYDOWN || wParam == WinAPI.WM_SYSKEYDOWN))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    int          nkey    = e.KeyValue;
                    if (nkey == 160 || nkey == 161 || e.Shift)
                    {
                        KeyboardStatus.Shift = true;
                    }
                    if (nkey == 162 || nkey == 163 || e.Control)
                    {
                        KeyboardStatus.Ctrl = true;
                    }
                    if (nkey == 164 || nkey == 165 || e.Alt)
                    {
                        KeyboardStatus.Alt = true;
                    }
                    s_KeyDown.Invoke(null, e);
                    handled = e.Handled;
                }

                // raise KeyPress
                if (s_KeyPress != null && wParam == WinAPI.WM_KEYDOWN)
                {
                    bool isDownShift    = ((WinAPI.GetKeyState(WinAPI.VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (WinAPI.GetKeyState(WinAPI.VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    WinAPI.GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (WinAPI.ToAscii(MyKeyboardHookStruct.VirtualKeyCode,
                                       MyKeyboardHookStruct.ScanCode,
                                       keyState,
                                       inBuffer,
                                       MyKeyboardHookStruct.Flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpper(key);
                        }
                        KeyPressEventArgs e = new KeyPressEventArgs(key);
                        s_KeyPress.Invoke(null, e);
                        handled = handled || e.Handled;
                    }
                }

                // raise KeyUp
                if (s_KeyUp != null && (wParam == WinAPI.WM_KEYUP || wParam == WinAPI.WM_SYSKEYUP))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    int          nkey    = e.KeyValue;
                    if (nkey == 160 || nkey == 161 || e.Shift)
                    {
                        KeyboardStatus.Shift = false;
                    }
                    if (nkey == 162 || nkey == 163 || e.Control)
                    {
                        KeyboardStatus.Ctrl = false;
                    }
                    if (nkey == 164 || nkey == 165 || e.Alt)
                    {
                        KeyboardStatus.Alt = false;
                    }

                    s_KeyUp.Invoke(null, e);
                    handled = handled || e.Handled;
                }
            }
            UpdateLastBehavior(wParam);
            //if event handled in application do not handoff to other listeners
            if (handled)
            {
                return(-1);
            }

            //forward to other application
            return(WinAPI.CallNextHookEx(s_KeyboardHookHandle, nCode, wParam, lParam));
        }
Example #12
0
        public static IntPtr Callback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (MMain.mahou == null || nCode < 0)
            {
                return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
            }
            if (KMHook.ExcludedProgram() && !MahouUI.ChangeLayoutInExcluded)
            {
                return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
            }
            var vk  = Marshal.ReadInt32(lParam);
            var Key = (Keys)vk;

            SetModifs(Key, wParam);
            if (MahouUI.SnippetsEnabled)
            {
                if (KMHook.c_snip.Count > 0)
                {
                    if (MMain.mahou.SnippetsExpandType == "Tab" && Key == Keys.Tab && !shift && !alt && !win && !ctrl)
                    {
                        WinAPI.keybd_event((byte)Keys.F14, (byte)Keys.F14, (int)WinAPI.KEYEVENTF_KEYUP, 0);
                        return((IntPtr)1);                        // Disable event
                    }
                }
            }
            if (MahouUI.RemapCapslockAsF18)
            {
                bool _shift = !shift, _alt = !alt, _ctrl = !ctrl, _win = !win;
                if (Key == Keys.CapsLock)
                {
                    for (int i = 1; i != 5; i++)
                    {
                        var KeyIndex = (int)typeof(MahouUI).GetField("Key" + i).GetValue(MMain.mahou);
                        if (KeyIndex == 8)                         // Shift+CapsLock
                        {
                            _shift = shift;
                        }
                    }
                }
                uint mods = 0;
                if (alt)
                {
                    mods += WinAPI.MOD_ALT;
                }
                if (ctrl)
                {
                    mods += WinAPI.MOD_CONTROL;
                }
                if (shift)
                {
                    mods += WinAPI.MOD_SHIFT;
                }
                if (win)
                {
                    mods += WinAPI.MOD_WIN;
                }
                bool has = MMain.mahou.HasHotkey(new Hotkey(false, (uint)Keys.F18, mods, 77));
                if (has)
                {
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_SHIFT))
                    {
                        _shift = shift;
                    }
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_ALT))
                    {
                        _alt = alt;
                    }
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_CONTROL))
                    {
                        _ctrl = ctrl;
                    }
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_WIN))
                    {
                        _win = win;
                    }
                }
                var GJIME = false;
                if (vk >= 240 && vk <= 242)                 // GJ IME's Shift/Alt/Ctrl + CapsLock
                {
                    GJIME = true;
                }
                //			Debug.WriteLine(Key + " " +has + "// " + _shift + " " + _alt + " " + _ctrl + " " + _win + " " + mods + " >> " + (Key == Keys.CapsLock && _shift && _alt && _ctrl && _win));
                if ((Key == Keys.CapsLock || GJIME) && _shift && _alt && _ctrl && _win)
                {
                    var flags = (int)(KInputs.IsExtended(Key) ? WinAPI.KEYEVENTF_EXTENDEDKEY : 0);
                    if (wParam == (IntPtr)WinAPI.WM_KEYUP)
                    {
                        flags |= (int)WinAPI.KEYEVENTF_KEYUP;
                    }
                    WinAPI.keybd_event((byte)Keys.F18, (byte)Keys.F18, flags, 0);
                    return((IntPtr)1);                    // Disable event
                }
                //			Debug.WriteLine(Marshal.GetLastWin32Error());
            }
            return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
        }
Example #13
0
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                #region Process Keyboard Event

                if (IsKeyboardEvent(wParam))
                {
                    bool wasPressed =
                        (WinAPI.KeyboardMessages)wParam == WinAPI.KeyboardMessages.WM_KEYDOWN ||
                        (WinAPI.KeyboardMessages)wParam == WinAPI.KeyboardMessages.WM_SYSKEYDOWN;

                    Key key = KeyInterop.KeyFromVirtualKey(Marshal.ReadInt32(lParam));

                    if (KeyEvent != null)
                    {
                        RawKeyEventArgs args = new RawKeyEventArgs(key, wasPressed);

                        KeyEvent(this, args);

                        if (args.EatInput)
                        {
                            return(EatInputValue);
                        }
                    }
                }

                #endregion
                #region Process Mouse Event

                if (IsMouseEvent(wParam))
                {
                    MSLLHOOKSTRUCT hookStruct = Marshal.PtrToStructure <MSLLHOOKSTRUCT>(lParam);

                    Point       point = new Point(hookStruct.pt.x, hookStruct.pt.y);
                    bool        wasPressed;
                    int         xButton = hookStruct.mouseData >> 16;
                    MouseButton button;

                    switch ((WinAPI.MouseMessages)wParam)
                    {
                    case WinAPI.MouseMessages.WM_LBUTTONDOWN:
                        wasPressed = true;
                        button     = MouseButton.Left;
                        break;

                    case WinAPI.MouseMessages.WM_LBUTTONUP:
                        wasPressed = false;
                        button     = MouseButton.Left;
                        break;

                    case WinAPI.MouseMessages.WM_RBUTTONDOWN:
                        wasPressed = true;
                        button     = MouseButton.Right;
                        break;

                    case WinAPI.MouseMessages.WM_RBUTTONUP:
                        wasPressed = false;
                        button     = MouseButton.Right;
                        //return (IntPtr)1;
                        break;

                    case WinAPI.MouseMessages.WM_MBUTTONDOWN:
                        wasPressed = true;
                        button     = MouseButton.Middle;
                        break;

                    case WinAPI.MouseMessages.WM_MBUTTONUP:
                        wasPressed = false;
                        button     = MouseButton.Middle;
                        break;

                    case WinAPI.MouseMessages.WM_XBUTTONDOWN:
                        wasPressed = true;
                        button     = xButton == 1 ? MouseButton.XButton1 : MouseButton.XButton2;
                        break;

                    case WinAPI.MouseMessages.WM_XBUTTONUP:
                        wasPressed = false;
                        button     = xButton == 1 ? MouseButton.XButton1 : MouseButton.XButton2;
                        break;

                    default:
                        throw new Exception();
                    }

                    if (MouseEvent != null)
                    {
                        RawMouseButtonEventArgs args = new RawMouseButtonEventArgs(button, point, wasPressed);

                        MouseEvent(this, args);

                        if (args.EatInput)
                        {
                            return(EatInputValue);
                        }
                    }
                }

                #endregion
            }

            return(WinAPI.CallNextHookEx(_keyboardHookId, nCode, wParam, lParam));
        }
Example #14
0
        private int currentKeyState = 1; //1=Down, 2=Up

        /// <summary>
        /// Captures the key.
        /// </summary>
        /// <param name="nCode">The n code.</param>
        /// <param name="wp">The wp.</param>
        /// <param name="lp">The lp.</param>
        /// <returns>IntPtr.</returns>
        /// <remarks>...</remarks>
        private IntPtr CaptureKey(int nCode, IntPtr wp, IntPtr lp)
        {
            if (currentKeyState == 2)
            {
                currentKeyState = 0;
            }
            if (currentKeyState == 1)
            {
                currentKeyState = 2;
            }
            if (currentKeyState == 0)
            {
                currentKeyState = 1;
            }

            if (nCode >= 0 && Settings.Current.EnableHotkeys)
            {
                var objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));

                try {
                    if (currentKeyState == 2 && (
                            objKeyInfo.vkCode == KeyInterop.VirtualKeyFromKey(Key.LWin)))
                    {
                        foreach (Window window in Application.Current.Windows)
                        {
                            if (window.GetType() == typeof(Screen))
                            {
                                Application.Current.Dispatcher.Invoke(new Action(() => {
                                    if (window.IsVisible)
                                    {
                                        window.Hide();
                                    }
                                    else
                                    {
                                        window.Show();
                                        window.Activate();
                                    }
                                }));
                            }
                        }

                        return((IntPtr)1);
                    }
                    if (currentKeyState == 2 && (
                            objKeyInfo.vkCode == KeyInterop.VirtualKeyFromKey(Key.RWin)))
                    {
                        if (Screen.StartBar.IsOpened)
                        {
                            Screen.StartBar.CloseToolbar();
                        }
                        else
                        {
                            Screen.StartBar.OpenToolbar();
                        }

                        return((IntPtr)1);
                    }
                }
                catch { return(WinAPI.CallNextHookEx(ptrHook, nCode, wp, lp)); }
            }
            return(WinAPI.CallNextHookEx(ptrHook, nCode, wp, lp));
        }
Example #15
0
        public static IntPtr Callback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (MMain.mahou == null || nCode < 0)
            {
                return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
            }
            if (KMHook.ExcludedProgram() && !MahouUI.ChangeLayoutInExcluded)
            {
                return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
            }
            var vk  = Marshal.ReadInt32(lParam);
            var Key = (Keys)vk;

            if (MahouUI.BlockAltUpNOW)
            {
                if ((wParam == (IntPtr)WinAPI.WM_SYSKEYUP || wParam == (IntPtr)WinAPI.WM_KEYUP) &&
                    (Key == Keys.LMenu || Key == Keys.RMenu || Key == Keys.Menu))
                {
                    Debug.WriteLine("ihihihihihihihih-hihih-hi blocked alt :)))))");
                    MahouUI.BlockAltUpNOW = false;
                    return((IntPtr)1);
                }
            }
            SetModifs(Key, wParam);
            if ((alt || alt) && (shift || shift_r) && Key == Keys.Tab)               // mysterious Alt+Shift+Tab, sends Shift Up before Tab down!?
            {
                KMHook.skip_kbd_events++;
            }
            Debug.WriteLine("Alive" + vk + " :: " + wParam);
            #region Redefines
            if (redefines.len > 0)
            {
                var modsstr = KMHook.GetModsStr(ctrl, ctrl_r, shift, shift_r, alt, alt_r, win, win_r).Replace("+", " ").Replace("  ", "").ToLower();
                for (int i = 0; i != redefines.len; i++)
                {
                    if (Key == redefines[i].k)
                    {
                        Logging.Log("[REDEF] > Redefined: " + redefines[i].k + " => " + redefines[i].v);
                        var rli  = redefines_excl_mods[i].ToLower();
                        var redy = true;
                        if (rli == "all" && !(!shift && !alt && !ctrl && !win && !shift_r && !alt_r && !ctrl_r && !win_r))
                        {
                            redy = false;
                        }
                        else
                        {
                            var srli = rli.Split(' ');
                            for (int y = 0; y != srli.Length; y++)
                            {
                                if (!String.IsNullOrEmpty(srli[y]) && modsstr.Contains(srli[y]))
                                {
                                    Logging.Log("[REDEF] > Contains the modifier: " + srli[y]);
                                    redy = false;
                                    break;
                                }
                            }
                        }
                        if (redy)
                        {
                            KMHook.KeybdEvent(redefines[i].v, (wParam.ToInt32() == WinAPI.WM_KEYDOWN || wParam.ToInt32() == WinAPI.WM_SYSKEYDOWN) ? 0 : 2);
                            return((IntPtr)1);
                        }
                        else
                        {
                            Logging.Log("[REDEF] > Redefine cancelled: by mods: " + redefines_excl_mods[i] + " (" + modsstr + ")");
                        }
                    }
                }
            }
            #endregion
            #region Mahou.mm Tray Hotkeys
            var x = new Tuple <bool, bool, bool, bool, bool, bool, bool, Tuple <bool, int> >(alt, alt_r, shift, shift_r, ctrl, ctrl_r, win, new Tuple <bool, int>(win_r, vk));
//				Debug.WriteLine("x_hk: " + Hotkey.tray_hk_to_string(x));
//				Debug.WriteLine("dhk_wait: " +dhk_tray_wait);
//				Debug.WriteLine("dhk_hk: " +dhk_tray_hk);
            if (dhk_tray_wait)
            {
                var hk       = Hotkey.tray_hk_parse(dhk_tray_hk);
                var UpOrDown = OnUpOrDown((Keys)hk.Rest.Item2, wParam);
                if (UpOrDown)
                {
                    var eq = Hotkey.cmp_hotkey(hk, x);
                    //				Debug.WriteLine("dhk_eq: "+eq);
                    if (eq)
                    {
                        Logging.Log("[TR_HK] > Executing action of (double)hotkey: " + dhk_tray_hk_real + " on second hotkey: " + dhk_tray_hk);
                        KMHook.DoSelf(dhk_tray_act, "tray_hotkeys_double");
                        dhk_unset();
                        KMHook.SendModsUp(15, false);                         // less overkill when whole hotkey is being hold
                        return((IntPtr)1);
                    }
                }
            }
            else
            {
                for (int i = 0; i != MahouUI.tray_hotkeys.len; i++)
                {
                    var hk       = Hotkey.tray_hk_parse(MahouUI.tray_hotkeys[i].k);
                    var UpOrDown = OnUpOrDown((Keys)hk.Rest.Item2, wParam);
//					Debug.WriteLine((UpOrDown ? "UP":"DOWN") + " key: " +Key);
                    if (UpOrDown)
                    {
                        if (Hotkey.cmp_hotkey(hk, x))
                        {
                            var d = Hotkey.tray_hk_is_double(MahouUI.tray_hotkeys[i].k);
                            if (d.Item1)
                            {
                                dhk_tray_wait    = true;
                                dhk_tray_hk      = d.Item3;
                                dhk_tray_act     = MahouUI.tray_hotkeys[i].v.Item1;
                                dhk_tray_hk_real = MahouUI.tray_hotkeys[i].k;
                                if (dhk_timer != null)
                                {
                                    dhk_timer.Stop();
                                    dhk_timer.Dispose();
                                }
                                dhk_timer          = new Timer();
                                dhk_timer.Interval = d.Item2;
                                dhk_timer.Tick    += (_, __) => { Debug.WriteLine("Unset timer dhk! " + dhk_timer.Interval + "ms"); dhk_unset(); dhk_timer.Stop(); dhk_timer.Dispose(); };
                                dhk_timer.Start();
                            }
                            else
                            {
                                Logging.Log("[TR_HK] > Executing action of hotkey: " + MahouUI.tray_hotkeys[i].k);
                                dhk_unset();
                                if (MahouUI.tray_hotkeys[i].v.Item2.Contains("hk|c") || MahouUI.tray_hotkeys[i].v.Item2.Contains("hk|s"))
                                {
                                    var altl = ((hk.Item1 || hk.Rest.Item2 == (int)Keys.LMenu) && !hk.Item2 &&                                     // l alt not r alt
                                                !hk.Item3 && !hk.Item4 &&
                                                !hk.Item5 && !hk.Item6 &&
                                                !hk.Item7 && !hk.Rest.Item1);
                                    var altr = (!hk.Item1 && (hk.Item2 || hk.Rest.Item2 == (int)Keys.RMenu) &&
                                                !hk.Item3 && !hk.Item4 &&
                                                !hk.Item5 && !hk.Item6 &&
                                                !hk.Item7 && !hk.Rest.Item1);
                                    if (altl || altr)
                                    {
                                        if (Locales.ActiveWindowClassName(40).Contains("Chrome_WidgetWin"))
                                        {
                                            MahouUI.chrome_window_alt_fix();
                                        }
                                    }
                                    else
                                    {
                                        KMHook.SendModsUp(15, false);                                         // less overkill when whole hotkey is being hold
                                    }
                                    if (altl)
                                    {
                                        KMHook.KeybdEvent(Keys.LMenu, 0);
                                        KMHook.KeybdEvent(Keys.LMenu, 2);
                                    }
                                    if (altr)
                                    {
                                        KMHook.KeybdEvent(Keys.RMenu, 0);
                                        KMHook.KeybdEvent(Keys.RMenu, 2);
                                    }
                                    if ((!hk.Item1 && !hk.Item2 &&
                                         !hk.Item3 && !hk.Item4 &&
                                         !hk.Item5 && !hk.Item6 &&
                                         (hk.Item7 || hk.Rest.Item2 == (int)Keys.LWin) && !hk.Rest.Item1))
                                    {
                                        KMHook.KeybdEvent(Keys.LWin, 0);
                                        KMHook.KeybdEvent(Keys.LWin, 2);
                                    }
                                    if ((!hk.Item1 && !hk.Item2 &&
                                         !hk.Item3 && !hk.Item4 &&
                                         !hk.Item5 && !hk.Item6 &&
                                         !hk.Item7 && (hk.Rest.Item1 || hk.Rest.Item2 == (int)Keys.RWin)))
                                    {
                                        KMHook.KeybdEvent(Keys.RWin, 0);
                                        KMHook.KeybdEvent(Keys.RWin, 2);
                                    }
                                }
                                KMHook.DoSelf(MahouUI.tray_hotkeys[i].v.Item1, "tray_hotkeys");
                                return((IntPtr)1);
                            }
                        }
                    }
                }
            }
            #endregion
            if (MahouUI.SnippetsEnabled)
            {
                if (KMHook.c_snip.Count > 0)
                {
                    var t = MMain.mahou.SnippetsExpandType == "Tab";
                    if (t && Key == Keys.Tab && !shift && !alt && !win && !ctrl && !shift_r && !alt_r && !ctrl_r && !win_r)
                    {
                        WinAPI.keybd_event((byte)Keys.F14, (byte)Keys.F14, 0, 0);
                        return((IntPtr)1);                        // Disable event
                    }
                    if (!t && MMain.mahou.SnippetsExpandType != "Space" && wParam == (IntPtr)WinAPI.WM_KEYDOWN || wParam == (IntPtr)WinAPI.WM_SYSKEYDOWN)
                    {
                        var ms = KMHook.GetModsStr(ctrl, ctrl_r, shift, shift_r, alt, alt_r, win, win_r);
                        ms += Key;
                        var othmatch = ms == MMain.mahou.SnippetsExpKeyOther;
                        Debug.WriteLine("Checking SnippetsExpOther: [" + ms + "] == [" + MMain.mahou.SnippetsExpKeyOther + "] => " + othmatch);
                        if (othmatch)
                        {
                            KMHook.ClearModifiers();
                            WinAPI.keybd_event((byte)Keys.F20, (byte)Keys.F20, 0, 0);
                            return((IntPtr)1);
                        }
                    }
                }
            }
            if (MahouUI.RemapCapslockAsF18)
            {
                bool _shift = !shift, _alt = !alt, _ctrl = !ctrl, _win = !win, _shift_r = !shift_r, _alt_r = !alt_r, _ctrl_r = !ctrl_r, _win_r = !win_r;
                if (Key == Keys.CapsLock)
                {
                    for (int i = 1; i != 5; i++)
                    {
                        var KeyIndex = (int)typeof(MahouUI).GetField("Key" + i).GetValue(MMain.mahou);
                        if (KeyIndex == 8)                           // Shift+CapsLock
                        {
                            _shift   = shift;
                            _shift_r = shift_r;
                        }
                    }
                }
                uint mods = 0;
                if (alt || alt_r)
                {
                    mods += WinAPI.MOD_ALT;
                }
                if (ctrl || ctrl_r)
                {
                    mods += WinAPI.MOD_CONTROL;
                }
                if (shift || shift_r)
                {
                    mods += WinAPI.MOD_SHIFT;
                }
                if (win || win_r)
                {
                    mods += WinAPI.MOD_WIN;
                }
                bool has = MMain.mahou.HasHotkey(new Hotkey(false, (uint)Keys.F18, mods, 77));
                if (has)
                {
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_SHIFT))
                    {
                        _shift = shift;
                    }
                    _shift_r = shift_r;
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_ALT))
                    {
                        _alt = alt;
                    }
                    _alt_r = alt_r;
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_CONTROL))
                    {
                        _ctrl = ctrl;
                    }
                    _ctrl_r = ctrl_r;
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_WIN))
                    {
                        _win = win;
                    }
                    _win_r = win_r;
                }
                var GJIME = false;
                if (vk >= 240 && vk <= 242)                 // GJ IME's Shift/Alt/Ctrl + CapsLock
                {
                    GJIME = true;
                }
                //			Debug.WriteLine(Key + " " +has + "// " + _shift + " " + _alt + " " + _ctrl + " " + _win + " " + mods + " >> " + (Key == Keys.CapsLock && _shift && _alt && _ctrl && _win));
                if ((Key == Keys.CapsLock || GJIME) && _shift && _alt && _ctrl && _win && _shift_r && _alt_r && _ctrl_r && _win_r)
                {
                    var flags = (int)(KInputs.IsExtended(Key) ? WinAPI.KEYEVENTF_EXTENDEDKEY : 0);
                    if (wParam == (IntPtr)WinAPI.WM_KEYUP)
                    {
                        flags |= (int)WinAPI.KEYEVENTF_KEYUP;
                    }
                    WinAPI.keybd_event((byte)Keys.F18, (byte)Keys.F18, flags, 0);
                    return((IntPtr)1);                    // Disable event
                }
                //			Debug.WriteLine(Marshal.GetLastWin32Error());
            }
            return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
        }
Example #16
0
        /// <summary>
        ///     A callback function which will be called every Time a mouse activity detected.
        /// </summary>
        /// <param name="nCode">
        ///     [in] Specifies whether the hook procedure must process the message.
        ///     If nCode is HC_ACTION, the hook procedure must process the message.
        ///     If nCode is less than zero, the hook procedure must pass the message to the
        ///     CallNextHookEx function without further processing and must return the
        ///     value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        ///     [in] Specifies whether the message was sent by the current thread.
        ///     If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        ///     [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        ///     If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        ///     If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        ///     and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        ///     hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        ///     procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private IntPtr MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Marshall the data from callback.
                var mouseHookStruct =
                    (WinAPI.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(WinAPI.MouseLLHookStruct));

                var   button     = MouseButtons.None;
                short mouseDelta = 0;
                var   clickCount = 0;
                var   upDown     = MouseEventExtArgs.UpDown.None;
                var   mouseUp    = false;

                //detect button clicked
                switch ((WinAPI.WindowMessage)wParam)
                {
                case WinAPI.WindowMessage.WM_LBUTTONDOWN:
                    upDown     = MouseEventExtArgs.UpDown.Down;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_LBUTTONUP:
                    upDown     = MouseEventExtArgs.UpDown.Up;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_LBUTTONDBLCLK:
                    button     = MouseButtons.Left;
                    clickCount = 2;
                    break;

                case WinAPI.WindowMessage.WM_RBUTTONDOWN:
                    upDown     = MouseEventExtArgs.UpDown.Down;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_RBUTTONUP:
                    upDown     = MouseEventExtArgs.UpDown.Up;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_RBUTTONDBLCLK:
                    button     = MouseButtons.Right;
                    clickCount = 2;
                    break;

                case WinAPI.WindowMessage.WM_MOUSEWHEEL:
                    //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta.
                    //One wheel click is defined as WHEEL_DELTA, which is 120.
                    //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                    if (mouseDelta > 0)
                    {
                        upDown = MouseEventExtArgs.UpDown.Up;
                    }
                    if (mouseDelta < 0)
                    {
                        upDown = MouseEventExtArgs.UpDown.Down;
                    }
                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                    //and the low-order word is reserved. This value can be one or more of the following values.
                    //Otherwise, MouseData is not used.
                    break;
                }

                //generate event
                //                if (CurrentMouseArgs != null)
                //
                //                    UpdateCount += 1;
                CurrentMouseArgs = new MouseEventExtArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta)
                {
                    Wheel = mouseDelta != 0, UpOrDown = upDown
                };
                MouseChanged = true;

                // Raise it
                OnMouseChangedEvent(this, CurrentMouseArgs);
            }
            //call next hook
            return(WinAPI.CallNextHookEx(hMouseHook, nCode, wParam, lParam));
        }
Example #17
0
 /// <summary>
 /// Callback function
 /// </summary>
 private IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam)
 {
     // parse system messages
     if (nCode >= 0)
     {
         if (WinAPI.MouseMessages.WM_LBUTTONDOWN == (WinAPI.MouseMessages)wParam)
         {
             if (LeftButtonDown != null)
             {
                 LeftButtonDown((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_LBUTTONUP == (WinAPI.MouseMessages)wParam)
         {
             if (LeftButtonUp != null)
             {
                 LeftButtonUp((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_RBUTTONDOWN == (WinAPI.MouseMessages)wParam)
         {
             if (RightButtonDown != null)
             {
                 RightButtonDown((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_RBUTTONUP == (WinAPI.MouseMessages)wParam)
         {
             if (RightButtonUp != null)
             {
                 RightButtonUp((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_MOUSEMOVE == (WinAPI.MouseMessages)wParam)
         {
             if (MouseMove != null)
             {
                 MouseMove((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_MOUSEWHEEL == (WinAPI.MouseMessages)wParam)
         {
             if (MouseWheel != null)
             {
                 MouseWheel((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_LBUTTONDBLCLK == (WinAPI.MouseMessages)wParam)
         {
             if (DoubleClick != null)
             {
                 DoubleClick((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_MBUTTONDOWN == (WinAPI.MouseMessages)wParam)
         {
             if (MiddleButtonDown != null)
             {
                 MiddleButtonDown((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
         if (WinAPI.MouseMessages.WM_MBUTTONUP == (WinAPI.MouseMessages)wParam)
         {
             if (MiddleButtonUp != null)
             {
                 MiddleButtonUp((WinAPI.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.MSLLHOOKSTRUCT)));
             }
         }
     }
     return(WinAPI.CallNextHookEx(hookID, nCode, wParam, lParam));
 }
Example #18
0
        protected override IntPtr OnHook(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Marshall the data from callback.
                WinAPI.MouseLLHookStruct mouseHookStruct = (WinAPI.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(WinAPI.MouseLLHookStruct));

                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                int          clickCount = 0;
                UpDown       upDown     = UpDown.None;

                //detect button clicked
                switch ((WinAPI.WindowMessage)wParam)
                {
                case WinAPI.WindowMessage.WM_LBUTTONDOWN:
                    upDown     = UpDown.Down;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_LBUTTONUP:
                    upDown     = UpDown.Up;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_LBUTTONDBLCLK:
                    button     = MouseButtons.Left;
                    clickCount = 2;
                    break;

                case WinAPI.WindowMessage.WM_RBUTTONDOWN:
                    upDown     = UpDown.Down;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_RBUTTONUP:
                    upDown     = UpDown.Up;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WinAPI.WindowMessage.WM_RBUTTONDBLCLK:
                    button     = MouseButtons.Right;
                    clickCount = 2;
                    break;

                case WinAPI.WindowMessage.WM_MOUSEWHEEL:
                    //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta.
                    //One wheel click is defined as WHEEL_DELTA, which is 120.
                    //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                    if (mouseDelta > 0)
                    {
                        upDown = UpDown.Up;
                    }
                    if (mouseDelta < 0)
                    {
                        upDown = UpDown.Down;
                    }
                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                    //and the low-order word is reserved. This value can be one or more of the following values.
                    //Otherwise, MouseData is not used.
                    break;
                }
                var args = new MouseEventExtArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta,
                    upDown);

                MouseEvent.Invoke(this, args);
            }
            //call next hook
            return(WinAPI.CallNextHookEx(hHook, nCode, wParam, lParam));
        }
Example #19
0
        /// <summary>
        /// A callback function which will be called every Time a mouse activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private static int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Marshall the data from callback.
                WinAPI.MouseLLHookStruct mouseHookStruct = (WinAPI.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(WinAPI.MouseLLHookStruct));

                //detect button clicked
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                int          clickCount = 0;
                bool         mouseDown  = false;
                bool         mouseUp    = false;

                switch (wParam)
                {
                case WinAPI.WM_LBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WinAPI.WM_LBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WinAPI.WM_LBUTTONDBLCLK:
                    button     = MouseButtons.Left;
                    clickCount = 2;
                    break;

                case WinAPI.WM_RBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WinAPI.WM_RBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WinAPI.WM_RBUTTONDBLCLK:
                    button     = MouseButtons.Right;
                    clickCount = 2;
                    break;

                case WinAPI.WM_MOUSEWHEEL:
                    //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta.
                    //One wheel click is defined as WHEEL_DELTA, which is 120.
                    //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);

                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                    //and the low-order word is reserved. This value can be one or more of the following values.
                    //Otherwise, MouseData is not used.
                    break;
                }

                //generate event
                MouseEventExtArgs e = new MouseEventExtArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta);

                //Mouse up
                if (s_MouseUp != null && mouseUp)
                {
                    s_MouseUp.Invoke(null, e);
                }

                //Mouse down
                if (s_MouseDown != null && mouseDown)
                {
                    s_MouseDown.Invoke(null, e);
                }

                //If someone listens to click and a click is heppened
                if (s_MouseClick != null && clickCount > 0)
                {
                    s_MouseClick.Invoke(null, e);
                }

                //If someone listens to click and a click is heppened
                if (s_MouseClickExt != null && clickCount > 0)
                {
                    s_MouseClickExt.Invoke(null, e);
                }

                //If someone listens to double click and a click is heppened
                if (s_MouseDoubleClick != null && clickCount == 2)
                {
                    s_MouseDoubleClick.Invoke(null, e);
                }

                //Wheel was moved
                if (s_MouseWheel != null && mouseDelta != 0)
                {
                    s_MouseWheel.Invoke(null, e);
                }

                //If someone listens to move and there was a change in coordinates raise move event
                if ((s_MouseMove != null || s_MouseMoveExt != null) && (m_OldX != mouseHookStruct.Point.X || m_OldY != mouseHookStruct.Point.Y))
                {
                    m_OldX = mouseHookStruct.Point.X;
                    m_OldY = mouseHookStruct.Point.Y;
                    if (s_MouseMove != null)
                    {
                        s_MouseMove.Invoke(null, e);
                    }

                    if (s_MouseMoveExt != null)
                    {
                        s_MouseMoveExt.Invoke(null, e);
                    }
                }

                if (e.Handled)
                {
                    return(-1);
                }
            }
            UpdateLastBehavior(wParam);
            //call next hook
            return(WinAPI.CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
Example #20
0
        public static IntPtr Callback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (MMain.mahou == null || nCode < 0)
            {
                return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
            }
            if (KMHook.ExcludedProgram() && !MahouUI.ChangeLayoutInExcluded)
            {
                return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
            }
            var vk  = Marshal.ReadInt32(lParam);
            var Key = (Keys)vk;

            if (MahouUI.BlockAltUpNOW)
            {
                if ((wParam == (IntPtr)WinAPI.WM_SYSKEYUP || wParam == (IntPtr)WinAPI.WM_KEYUP) &&
                    (Key == Keys.LMenu || Key == Keys.RMenu || Key == Keys.Menu))
                {
                    Debug.WriteLine("ihihihihihihihih-hihih-hi blocked alt :)))))");
                    MahouUI.BlockAltUpNOW = false;
                    return((IntPtr)1);
                }
            }
            SetModifs(Key, wParam);
            Debug.WriteLine("Alive" + vk);
            #region Mahou.mm Tray Hotkeys
            if (wParam == (IntPtr)WinAPI.WM_KEYDOWN || wParam == (IntPtr)WinAPI.WM_SYSKEYDOWN)
            {
                for (int i = 0; i != MahouUI.tray_hotkeys.len; i++)
                {
                    var x = new Tuple <bool, bool, bool, bool, bool, bool, bool, Tuple <bool, int> >(alt, alt_r, shift, shift_r, ctrl, ctrl_r, win, new Tuple <bool, int>(win_r, vk));
                    if (Hotkey.cmp_hotkey(Hotkey.tray_hk_parse(MahouUI.tray_hotkeys[i].k), x))
                    {
                        Logging.Log("[TR_HK] > Executing action of hotkey: " + MahouUI.tray_hotkeys[i].k);
                        KMHook.DoSelf(MahouUI.tray_hotkeys[i].v, "tray_hotkeys");
                        KMHook.SendModsUp(15, false);                         // less overkill when whole hotkey is being hold
                        return((IntPtr)1);
                    }
                }
            }
            #endregion
            if (MahouUI.SnippetsEnabled)
            {
                if (KMHook.c_snip.Count > 0)
                {
                    if (MMain.mahou.SnippetsExpandType == "Tab" && Key == Keys.Tab && !shift && !alt && !win && !ctrl && !shift_r && !alt_r && !ctrl_r && !win_r)
                    {
                        WinAPI.keybd_event((byte)Keys.F14, (byte)Keys.F14, (int)WinAPI.KEYEVENTF_KEYUP, 0);
                        return((IntPtr)1);                        // Disable event
                    }
                }
            }
            if (MahouUI.RemapCapslockAsF18)
            {
                bool _shift = !shift, _alt = !alt, _ctrl = !ctrl, _win = !win, _shift_r = !shift_r, _alt_r = !alt_r, _ctrl_r = !ctrl_r, _win_r = !win_r;
                if (Key == Keys.CapsLock)
                {
                    for (int i = 1; i != 5; i++)
                    {
                        var KeyIndex = (int)typeof(MahouUI).GetField("Key" + i).GetValue(MMain.mahou);
                        if (KeyIndex == 8)                         // Shift+CapsLock
                        {
                            _shift = shift;
                        }
                        _shift_r = shift_r;
                    }
                }
                uint mods = 0;
                if (alt || alt_r)
                {
                    mods += WinAPI.MOD_ALT;
                }
                if (ctrl || ctrl_r)
                {
                    mods += WinAPI.MOD_CONTROL;
                }
                if (shift || shift_r)
                {
                    mods += WinAPI.MOD_SHIFT;
                }
                if (win || win_r)
                {
                    mods += WinAPI.MOD_WIN;
                }
                bool has = MMain.mahou.HasHotkey(new Hotkey(false, (uint)Keys.F18, mods, 77));
                if (has)
                {
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_SHIFT))
                    {
                        _shift = shift;
                    }
                    _shift_r = shift_r;
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_ALT))
                    {
                        _alt = alt;
                    }
                    _alt_r = alt_r;
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_CONTROL))
                    {
                        _ctrl = ctrl;
                    }
                    _ctrl_r = ctrl_r;
                    if (Hotkey.ContainsModifier((int)mods, (int)WinAPI.MOD_WIN))
                    {
                        _win = win;
                    }
                    _win_r = win_r;
                }
                var GJIME = false;
                if (vk >= 240 && vk <= 242)                 // GJ IME's Shift/Alt/Ctrl + CapsLock
                {
                    GJIME = true;
                }
                //			Debug.WriteLine(Key + " " +has + "// " + _shift + " " + _alt + " " + _ctrl + " " + _win + " " + mods + " >> " + (Key == Keys.CapsLock && _shift && _alt && _ctrl && _win));
                if ((Key == Keys.CapsLock || GJIME) && _shift && _alt && _ctrl && _win && _shift_r && _alt_r && _ctrl_r && _win_r)
                {
                    var flags = (int)(KInputs.IsExtended(Key) ? WinAPI.KEYEVENTF_EXTENDEDKEY : 0);
                    if (wParam == (IntPtr)WinAPI.WM_KEYUP)
                    {
                        flags |= (int)WinAPI.KEYEVENTF_KEYUP;
                    }
                    WinAPI.keybd_event((byte)Keys.F18, (byte)Keys.F18, flags, 0);
                    return((IntPtr)1);                    // Disable event
                }
                //			Debug.WriteLine(Marshal.GetLastWin32Error());
            }
            return(WinAPI.CallNextHookEx(_LLHook_ID, nCode, wParam, lParam));
        }