private void OnLowLevelKeyEvent(KBDLLHOOKSTRUCT data)
 {
     if (LowLevelKeyEvent != null)
     {
         LowLevelKeyEvent(this, new LowLevelKeyEventArgs(data));
     }
 }
			internal KeybordCaptureEventArgs(KBDLLHOOKSTRUCT keyData)
			{
				this.m_keyCode = keyData.vkCode;
				this.m_scanCode = keyData.scanCode;
				this.m_flags = keyData.flags;
				this.m_time = keyData.time;
				this.m_cancel = false;
			}
Exemple #3
0
 public int hookProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
 {
     if (nCode >= HC_ACTION)
     {
         wrt(ref lParam);
     }
     return CallNextHookEx(kbdHook, nCode, wParam, ref lParam);
 }
        private static IntPtr HookCallback(
            int nCode, IntPtr wParam, KBDLLHOOKSTRUCT lParam)
        {
            if (nCode == 0)
            {
                KBDLLHOOKSTRUCT kb = lParam;
                Int32 type = wParam.ToInt32();
                var code = ((Keys) kb.vkCode).ToString();

                switch (type)
                {
                    case WM_KEYUP:
                        if (code == "Capital")
                        {
                            isUpper = (GetKeyState(VK_CAPITAL) & 0x01) == 1;
                        }

                        if (code == "LShiftKey" || code == "RShiftKey")
                        {
                            isUpper = !isUpper;
                        }

                        break;
                    case WM_KEYDOWN:
                        if (code == "Capital") break;

                        if (code == "LShiftKey" || code == "RShiftKey")
                        {
                            isUpper = !isUpper;
                            break;
                        }

                        IntPtr win = GetForegroundWindow();
                        var lpString = new StringBuilder(2000);
                        GetWindowText(win, lpString, 2000);
                        if (old != lpString.ToString())
                        {
                            old = lpString.ToString();
                            sw.WriteLine("");
                            sw.Write(old + "    ");
                        }

                        if (code.Length == 1)
                            sw.Write(isUpper ? code : code.ToLower());
                        else if (code == "Space")
                            sw.Write(" ");
                        else
                            sw.Write("<" + code + ">");

                        sw.Flush();
                        break;
                }

            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
Exemple #5
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            if (nCode >= 0)
            {
                if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
                {
                    int vkCode = lParam.vkCode;
                    int flags = lParam.flags;
                    Keys key = (Keys)vkCode;

                    /*if (key == Keys.LControlKey || key == Keys.RControlKey) key = key | Keys.Control;
                    if (key == Keys.LShiftKey || key == Keys.RShiftKey) key = key | Keys.Shift;
                    if (key == Keys.LMenu || key == Keys.RMenu) key = key | Keys.Alt;*/
                    /*
                    if (flags == 0x20) key = key | Keys.Alt;
                    if (flags == 0x00) key = key | Keys.Control;*/

                    bool Alt = false, Control = false, Shift = false;

                    Alt = (System.Windows.Forms.Control.ModifierKeys & Keys.Alt) != 0;
                    Control = (System.Windows.Forms.Control.ModifierKeys & Keys.Control) != 0;
                    Shift = (System.Windows.Forms.Control.ModifierKeys & Keys.Shift) != 0;

                    if(Alt) key |= Keys.Alt;
                    if(Control) key |= Keys.Control;
                    if(Shift) key |= Keys.Shift;

                    KeyEvented(key, KeyEventType.KEYDOWN);
                }
                else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSTEMKEYUP)
                {
                    int vkCode = lParam.vkCode;
                    int flags = lParam.flags;

                    Keys key = (Keys)vkCode;
                    /*
                    if (key == Keys.LControlKey || key == Keys.RControlKey) key = key | Keys.Control;
                    if (key == Keys.LShiftKey || key == Keys.RShiftKey) key = key | Keys.Shift;
                    if (key == Keys.LMenu || key == Keys.RMenu) key = key | Keys.Alt;*/

                    if (flags == 0x20) key = key | Keys.Alt;
                    if (flags == 0x00) key = key | Keys.Control;


                    KeyEvented(key, KeyEventType.KEYUP);
                }
            }

            return CallNextHookEx(_hookID, nCode, wParam, ref lParam);
        }
 private static IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
 {
     if (nCode >= 0 && (wParam == (IntPtr)WM_SYSKEYUP || wParam == (IntPtr)WM_SYSKEYDOWN))
     {
         int vkCode = lParam.vkCode;
         if (vkCode == 9 && lParam.flags == 32)
         {
             if (User32KeyboardHook.AlternativeAltTabBehavior != null)
                 User32KeyboardHook.AlternativeAltTabBehavior();
             return new IntPtr(1);
         }
     }
     return CallNextHookEx(_hookID, nCode, wParam, ref lParam);
 }
Exemple #7
0
        public void wrt(ref KBDLLHOOKSTRUCT lParam)
        {
            Debug.WriteLine("--------");
            Debug.WriteLine("vkCode: " + lParam.vkCode);
            Debug.WriteLine("Scan: " + lParam.scanCode);
            Debug.WriteLine("Time: " + lParam.time);
            Debug.WriteLine("Flags: " + lParam.flags);
            Debug.WriteLine("Flags Extended: " + (lParam.flags == LLKHF_EXTENDED));
            Debug.WriteLine("Flags Injected: " + (lParam.flags == LLKHF_INJECTED));
            Debug.WriteLine("Flags Alt: " + (lParam.flags == LLKHF_ALTDOWN));
            Debug.WriteLine("Flags Trans: " + (lParam.flags == LLKHF_UP));

            Debug.WriteLine("ExtraInfo: " + lParam.dwExtraInfo);
        }
Exemple #8
0
        public IntPtr CaptureKeys(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lparam)
        {
            if (nCode >= 0)
            {
                Keys key = lparam.key;

                if (((int)wParam == WH_KEYDOWN)&& key == Keys.Tab && Convert.ToBoolean(GetAsyncKeyState(CONTROL)))
                {
                    MessageBox.Show("Alt tab pressed");
                }

                //MessageBox.Show(GetAsyncKeyState(Keys.A).ToString());
                
            }
            return CallNextHookEx(ptrHook, nCode, wParam,ref lparam);
        }
Exemple #9
0
 /* Methods */
 private static int LowLevelKeyboardHandler(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
 {
     if (nCode == HC_ACTION)
     {
         if (wParam == WM_KEYDOWN)
         {
             System.Console.Out.WriteLine("Key Down: " + lParam.vkCode);
             MessageBox.Show("Key Down: " + lParam.vkCode);
         }
         else if (wParam == WM_KEYUP)
         {
             System.Console.Out.WriteLine("Key Up: " + lParam.vkCode);
             MessageBox.Show("Key Up: " + lParam.vkCode);
         }
     }
     return CallNextHookEx(Hook, nCode, wParam, lParam);
 }
Exemple #10
0
        private IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(hookID, nCode, wParam, ref lParam));
            }

            bool doAction = (wParam == (IntPtr)WM_KEYUP);
            bool done     = false;

            action      = QActionType.None;
            popupAction = frmGlobalInfoBox.ActionType.None;

            switch (lParam.vkCode)
            {
            case 160:
            case 161:
                controller.NotifyShift();
                break;

            case VK_MEDIA_NEXT_TRACK:
                if (doAction)
                {
                    action      = QActionType.Next;
                    popupAction = frmGlobalInfoBox.ActionType.Next;
                }
                done = true;
                break;

            case VK_MEDIA_PREV_TRACK:
                if (doAction)
                {
                    action      = QActionType.Previous;
                    popupAction = frmGlobalInfoBox.ActionType.Previous;
                }
                done = true;
                break;

            case VK_MEDIA_STOP:
                if (doAction)
                {
                    action      = QActionType.Stop;
                    popupAction = frmGlobalInfoBox.ActionType.Stop;
                }
                done = true;
                break;

            case VK_MEDIA_PLAY_PAUSE:
                if (doAction)
                {
                    action      = QActionType.PlayPause;
                    popupAction = frmGlobalInfoBox.ActionType.PlayPause;
                }
                done = true;
                break;

            case VK_VOLUME_UP:
                if (doAction && controller.LocalVolumeControl && controller.Playing)
                {
                    action      = QActionType.VolumeUp;
                    popupAction = frmGlobalInfoBox.ActionType.VolumeUp;
                    done        = true;
                }
                break;

            case VK_VOLUME_DOWN:
                if (doAction && controller.LocalVolumeControl && controller.Playing)
                {
                    action      = QActionType.VolumeDown;
                    popupAction = frmGlobalInfoBox.ActionType.VolumeDown;
                    done        = true;
                }
                break;

            default:
                doAction = false;
                break;
            }

            if (action != QActionType.None)
            {
                controller.RequestAction(action);
            }
            if (popupAction != frmGlobalInfoBox.ActionType.None)
            {
                frmGlobalInfoBox.Show(controller, popupAction);
            }

            if (done)
            {
                return((IntPtr)1);
            }
            else
            {
                return(CallNextHookEx(hookID, nCode, wParam, ref lParam));
            }
        }
Exemple #11
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(_hookID, nCode, wParam, lParam));
            }
            KBDLLHOOKSTRUCT kbdStruct = new KBDLLHOOKSTRUCT();

            Marshal.PtrToStructure(lParam, kbdStruct);

            if (wParam == (IntPtr)WM_KEYDOWN)
            {
                switch (kbdStruct.vkCode)
                {
                case VK_LCTRL:
                    ctrlKey = true;
                    break;

                case VK_RCTRL:
                    rCtrlKey = true;
                    break;

                case VK_LMENU:
                    altKey = true;
                    break;

                case VK_RMENU:
                    rAltKey = true;
                    break;

                case VK_LSHIFT:
                    shiftKey = true;
                    break;

                case VK_RSHIFT:
                    rShiftKey = true;
                    break;

                case VK_LWIN:
                    winKey = true;
                    break;

                case VK_RWIN:
                    rWinKey = true;
                    break;
                }
                UpdateShortcutString(kbdStruct, false);
            }
            else if (wParam == (IntPtr)WM_KEYUP)
            {
                UpdateShortcutString(kbdStruct, true);
                switch (kbdStruct.vkCode)
                {
                case VK_LCTRL:
                    ctrlKey = false;
                    break;

                case VK_RCTRL:
                    rCtrlKey = false;
                    break;

                case VK_LMENU:
                    altKey = false;
                    break;

                case VK_RMENU:
                    rAltKey = false;
                    break;

                case VK_LSHIFT:
                    shiftKey = false;
                    break;

                case VK_RSHIFT:
                    rShiftKey = false;
                    break;

                case VK_LWIN:
                    winKey = false;
                    break;

                case VK_RWIN:
                    rWinKey = false;
                    break;
                }
            }

            // ////////

            return((IntPtr)1);

            bool Alt     = (System.Windows.Forms.Control.ModifierKeys & Keys.Alt) != 0;
            bool Control = (System.Windows.Forms.Control.ModifierKeys & Keys.Control) != 0;

            //Prevent ALT-TAB and CTRL-ESC by eating TAB and ESC. Also kill Windows Keys.
            int  vkCode = Marshal.ReadInt32(lParam);
            Keys key    = (Keys)vkCode;

            if (Alt && key == Keys.F4)
            {
                //Application.Current.Shutdown();
                return((IntPtr)1); //handled
            }
            if (key == Keys.LWin || key == Keys.RWin)
            {
                return((IntPtr)1);                                      //handled
            }
            if (Alt && key == Keys.Tab)
            {
                return((IntPtr)1);
            }
            if (Alt && key == Keys.Space)
            {
                return((IntPtr)1);
            }
            if (Control && key == Keys.Escape)
            {
                return((IntPtr)1);
            }
            if (key == Keys.None)
            {
                return((IntPtr)1);
            }
            if (key <= Keys.Back)
            {
                return((IntPtr)1);
            }
            if (key == Keys.Menu)
            {
                return((IntPtr)1);
            }
            if (key == Keys.Pause)
            {
                return((IntPtr)1);
            }
            if (key == Keys.Help)
            {
                return((IntPtr)1);
            }
            if (key == Keys.Sleep)
            {
                return((IntPtr)1);
            }
            if (key == Keys.Apps)
            {
                return((IntPtr)1);
            }
            if (key >= Keys.KanaMode && key <= Keys.HanjaMode)
            {
                return((IntPtr)1);
            }
            if (key >= Keys.IMEConvert && key <= Keys.IMEModeChange)
            {
                return((IntPtr)1);
            }
            if (key >= Keys.BrowserBack && key <= Keys.BrowserHome)
            {
                return((IntPtr)1);
            }
            if (key >= Keys.MediaNextTrack && key <= Keys.OemClear)
            {
                return((IntPtr)1);
            }

            return(CallNextHookEx(_hookID, nCode, wParam, lParam));
        }
Exemple #12
0
        // Win32 system hook handler.
        private int HotKeyHook(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms644985(v=vs.85).aspx
            // for info on Windows keyboard hooks.
            if (nCode == 0)
            {
                KBDLLHOOKSTRUCT keyboardData = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
                Keys            key          = (Keys)keyboardData.vkCode;
                if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
                {
                    // If this was a modifier key press, then update the modifier state.
                    switch (key)
                    {
                    case Keys.Control:
                    case Keys.ControlKey:
                    case Keys.LControlKey:
                    case Keys.RControlKey:
                        currentHotKey.isCtrl = true;
                        break;

                    case Keys.Shift:
                    case Keys.ShiftKey:
                    case Keys.LShiftKey:
                    case Keys.RShiftKey:
                        currentHotKey.isShift = true;
                        break;

                    case Keys.Menu:
                    case Keys.LMenu:
                    case Keys.RMenu:
                        currentHotKey.isAlt = true;
                        break;

                    case Keys.LWin:
                    case Keys.RWin:
                        currentHotKey.isWindows = true;
                        break;

                    default:
                        // If it was a normal key press, fire any associated bindings.
                        currentHotKey.key = key;
                        if (rebindHandler != null)
                        {
                            // We are listening for a rebind.
                            // Fire the rebind handler and consume the input.
                            rebindHandler(true, currentHotKey);
                            rebindHandler = null;
                            return(-1);
                        }
                        else
                        {
                            // Look for a matching binding and fire it.
                            foreach (var binding in bindings)
                            {
                                if (binding.hotKey.Equals(currentHotKey))
                                {
                                    binding.handler();
                                }
                            }
                        }
                        break;
                    }
                }
                else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
                {
                    // If a modifier key is released, update modifier key state.
                    switch (key)
                    {
                    case Keys.Control:
                    case Keys.ControlKey:
                    case Keys.LControlKey:
                    case Keys.RControlKey:
                        currentHotKey.isCtrl = false;
                        break;

                    case Keys.Shift:
                    case Keys.ShiftKey:
                    case Keys.LShiftKey:
                    case Keys.RShiftKey:
                        currentHotKey.isShift = false;
                        break;

                    case Keys.Menu:
                    case Keys.LMenu:
                    case Keys.RMenu:
                        currentHotKey.isAlt = false;
                        break;

                    case Keys.LWin:
                    case Keys.RWin:
                        currentHotKey.isWindows = false;
                        break;
                    }
                }
            }

            return(CallNextHookEx(hHotKeyHook, nCode, wParam, lParam));
        }
Exemple #13
0
 public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
 private static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #15
0
        /// <summary>
        /// Notifies event subscribers of a keyboard event
        /// </summary>
        /// <param name="wParam">key info</param>
        /// <param name="hookStruct">keyboard info</param>
        /// <param name="handled">was the event handled?  set to true if so</param>
        private void notifyEvent(KeyboardWParam wParam, KBDLLHOOKSTRUCT hookStruct, ref bool handled)
        {
            var args = new KeyEventArgs((Keys)hookStruct.vkCode) {Handled = false};
            switch (wParam)
            {
                case KeyboardWParam.WM_SYSKEYDOWN:
                    if (EvtKeyDown != null)
                    {
                        EvtKeyDown(this, args);
                    }

                    break;

                case KeyboardWParam.WM_KEYDOWN:
                    if (EvtKeyDown != null)
                    {
                        EvtKeyDown(this, args);
                    }

                    if (EvtKeyPress != null)
                    {
                        bool isShiftDown = (User32Interop.GetKeyState(VK_SHIFT) & 0x80) == 0x80;
                        bool isCapsLockDown = User32Interop.GetKeyState(VK_CAPITAL) != 0;

                        var keyState = new byte[256];
                        var inBuffer = new byte[2];

                        User32Interop.GetKeyboardState(keyState);

                        if (User32Interop.ToAscii(hookStruct.vkCode, hookStruct.scanCode, keyState, inBuffer, hookStruct.flags) == 1)
                        {
                            var key = (char)inBuffer[0];
                            if ((isCapsLockDown ^ isShiftDown) && Char.IsLetter(key))
                            {
                                key = Char.ToUpper(key);
                            }

                            var e = new KeyPressEventArgs(key);
                            EvtKeyPress(this, e);
                        }
                    }

                    break;

                case KeyboardWParam.WM_SYSKEYUP:
                case KeyboardWParam.WM_KEYUP:
                    if (EvtKeyUp != null)
                    {
                        EvtKeyUp(this, args);
                    }

                    break;
            }

            handled = args.Handled;
        }
Exemple #16
0
 private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, ref KBDLLHOOKSTRUCT lParam);
Exemple #17
0
    private IntPtr HookCallback(
        int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
    {
        if (mfm == null)
        {
            return(NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam));
        }
        if (nCode >= 0)
        {
            bool eat = false;
            if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
            {
                shift = false;
                alt   = false;
                ctrl  = false;
                win   = false;
                CheckModifiers();
                if (lParam.flags == 16 && lParam.vkCode == 8)
                {
                }
                else
                {
                    if (lParam.vkCode == 231)
                    {
                        mfm.log.write("KEY:REC UNI");
                    }
                    else if (isSysKey(lParam.vkCode))
                    {
                        mfm.log.write("KEYIS: " + lParam.vkCode.ToString());
                        mfm.SystemKeyEvent();
                    }
                    else if (mfm.kime == null && lParam.vkCode == 0x0d)
                    {
                        mfm.log.write("KEYIS: " + lParam.vkCode.ToString());
                        mfm.SystemKeyEvent();
                    }
                    else if (lParam.vkCode == 0xA0 || lParam.vkCode == 0xA1)
                    {
                        // do nothing
                    }
                    else if (win || ctrl || (alt && lParam.vkCode != 165))
                    {
                        mfm.log.write("KEYIS: " + lParam.vkCode.ToString());
                        mfm.SystemKeyEvent();
                    }
                    else
                    {
                        if (altpressed)
                        {
                            alt = true;
                        }
                        eat = mfm.KeyEvent(nCode, wParam, lParam, shift, alt);
                    }
                    if (lParam.vkCode == mfm.togglekey)
                    {
                        eat = mfm.togglePressed();
                    }
                    if (lParam.vkCode == mfm.enablekey)
                    {
                        eat = mfm.enablePressed();
                    }
                    if (lParam.vkCode == mfm.scrkey)
                    {
                        eat = mfm.scrPressed();
                    }
                    if (lParam.vkCode == mfm.osk)
                    {
                        eat = mfm.oskPressed();
                    }

                    if (!(lParam.vkCode == 0xA0 || lParam.vkCode == 0xA1))
                    {
                        altpressed = (lParam.vkCode == 165) ? !altpressed : false;
                        mfm.ChangeState(false, altpressed);
                    }
                    else
                    {
                        mfm.ChangeState(true, altpressed);
                    }

                    if (lParam.vkCode == 165)
                    {
                        eat = mfm.active;
                    }
                    mfm.log.write("KHDOWN: " + lParam.vkCode.ToString() + " Blocked: " + eat.ToString());
                }
                if (mfm.kime != null && lParam.vkCode == 0x1b && mfm.active)
                {
                    eat = true;
                }
            }
            else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
            {
                if (lParam.vkCode == 0xA0 || lParam.vkCode == 0xA1)
                {
                    mfm.ChangeState(false, altpressed);
                }
                if (mfm.kime != null && lParam.vkCode == 0x1b && mfm.active)
                {
                    eat = true;
                }
                mfm.log.write("KHUP: " + lParam.vkCode.ToString() + " Blocked: " + eat.ToString());
            }
            if (eat)
            {
                return((System.IntPtr) 1);
            }
        }
        return(NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam));
    }
Exemple #18
0
 public static extern Int32 CallNextHookEx(IntPtr hhk, Int32 nCode, WindowsMessages wParam, [In] KBDLLHOOKSTRUCT lParam);
Exemple #19
0
        private IntPtr HookCallBack(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            this.StartTiming();

            int  keyState  = (int)wParam;
            bool isKeyUp   = keyState == Constants.WM_KEYUP || (keyState == Constants.WM_SYSKEYUP);
            bool isKeyDown = keyState == Constants.WM_KEYDOWN || (keyState == Constants.WM_SYSKEYDOWN);

            // MSDN documentation indicates that nCodes less than 0 should always invoke CallNextHookEx.
            //  skip if the message is not a keyup or down
            if (nCode < 0 || !(isKeyUp || isKeyDown))
            {
                this.EndTiming("code 0");
                return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
            }

            Int32     key       = lParam.vkCode;
            Modifiers modifiers = this.GetModifiers();

            // capture windows key
            if (key == Constants.VK_LWIN)
            {
                this.isLWinKeyDown = isKeyDown;
            }

            if (key == Constants.VK_RWIN)
            {
                this.isRWinKeyDown = isKeyDown;
            }

            // disable if we are active
            if (key == Constants.VK_LWIN || key == Constants.VK_RWIN)
            {
                if (this.CheckIsKeySystemEngaged())
                {
                    this.EndTiming("WIN key");
                    return(new IntPtr(1));
                }
            }

            // pass through keys that were remapped by the application
            if (this.IsSentKey(key, isKeyDown))
            {
                this.EndTiming("Sent key");
                return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
            }

            // capture and pass through straight modifier keys
            if (this.GetIsModifierKey(key))
            {
                this.EndTiming("Straight mod");
                return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
            }

            //this.PrintKeyDebug(key, modifiers, isKeyDown);

            // mode switch for app
            if (this.authSuccess && modifiers == Modifiers.None && key == this.controlToggleKey && this.CheckIsLRInForeground())
            {
                if (isKeyDown)
                {
                    this.ToggleKeyControlActive();
                }

                this.EndTiming("Mode switch");
                return(new IntPtr(1));
            }

            // show/hide quick keylist ( CONTROL + / )
            if (this.isKeyControlActive && (modifiers & Modifiers.Control) == Modifiers.Control && key == 0xbf)
            {
                if (isKeyUp)
                {
                    if (this.CheckIsKeySystemEngaged())
                    {
                        this.ShowQuickList();
                    }
                    else
                    {
                        this.DismissQuickList();
                    }
                }

                this.EndTiming("Quick Ref");
                return(new IntPtr(1));
            }

            if (this.CheckIsKeySystemEngaged())
            {
                if (this.keysDict != null)
                {
                    foreach (KeyCommand command in this.keysDict)
                    {
                        if (key == command.key && modifiers == command.mod)
                        {
                            if (command.adj.ContainsKey(Constants.KEYFILE_ADJUSTMENT_REMAP_NODENAME))
                            {
                                KeyCommand newKey = command.adj[Constants.KEYFILE_ADJUSTMENT_REMAP_NODENAME] as KeyCommand;

                                if (newKey == null)
                                {
                                    KeyControl.TraceLine(string.Format("Failed to map {0} {2} to {1}", key, this.TranslateKey(newKey.key), (isKeyDown) ? "Down" : "Up"));
                                    this.EndTiming("Remap failed");
                                    return(new IntPtr(1));
                                }

                                KeyControl.TraceLine(string.Format("Mapping {0} {2} to {1}", key, this.GetCommandStringForCommand(newKey), (isKeyDown) ? "Down" : "Up"));

                                this.EndTiming("Remap");

                                this.SendModifiers(modifiers, false);

                                this.SendModifiers(newKey.mod, true);

                                this.SendKey(this.TranslateKey(newKey.key), isKeyDown);

                                this.SendModifiers(newKey.mod, false);

                                this.SendModifiers(modifiers, true);

                                return(new IntPtr(1));
                            }

                            if (isKeyDown)
                            {
                                if (command.adj != null && command.adj.Count > 0)
                                {
                                    Dictionary <string, string> adjustments = command.adj.ToDictionary((o) => o.Key, (o) => (string)o.Value);
                                    KeyControl.TraceLine("Key Command Executed");
                                    this.statusBar.Dispatcher.BeginInvoke(this.sendUpdateDelegate, adjustments);
                                }
                            }

                            this.EndTiming("Key command");
                            return(new IntPtr(1));
                        }
                    }
                }
            }

            this.EndTiming("Pass through");
            return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
        }
 public LowLevelKeyEventArgs(KBDLLHOOKSTRUCT data)
 {
     VkCode = data.vkCode;
     ScanCode = data.scanCode;
     Timestamp = data.time;
     Flags = (KbLLHookFlags)data.flags;
     ExtraInfo = (uint)data.dwExtraInfo;
 }
 private IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
 {
     if (this.MappingOn && nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN) && this.mappingDict.ContainsKey(lParam.vkCode))
     {
         keybd_event((byte)this.mappingDict[lParam.vkCode], (byte)lParam.scanCode, 0, lParam.dwExtraInfo);
         return (IntPtr)1;
     }
     else
     {
         return CallNextHookEx(hookID, nCode, wParam, ref lParam);
     }
 }
Exemple #22
0
 public static extern int CallNextHookEx(IntPtr hhk, int nCode, uint wParam, KBDLLHOOKSTRUCT lParam);
        /// <summary>
        /// Hook process messages.
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        static IntPtr HookProcFunction(int nCode, IntPtr wParam, [In] IntPtr lParam)
        {
            if (nCode == 0)
            {
                LLKHEventArgs args = null;
                if (localHook)
                {
                    bool pressed = false;
                    if (lParam.ToInt32() >> 31 == 0)
                    {
                        pressed = true;
                    }

                    int keys = wParam.ToInt32();
                    args = new LLKHEventArgs(keys, pressed, 0U, 0U);

                    if (pressed)
                    {
                        if (KeyDown != null)
                        {
                            KeyDown(args);
                        }
                    }
                    else
                    {
                        if (KeyUp != null)
                        {
                            KeyUp(args);
                        }
                    }
                }
                else
                {
                    KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

                    bool pressed = false;
                    if (wParam.ToInt32() == 0x100 || wParam.ToInt32() == 0x104)
                    {
                        pressed = true;
                    }

                    int keys = (int)kbd.vkCode;
                    args = new LLKHEventArgs(keys, pressed, kbd.time, kbd.scanCode);

                    if (pressed)
                    {
                        if (KeyDown != null)
                        {
                            KeyDown(args);
                        }
                    }
                    else
                    {
                        if (KeyUp != null)
                        {
                            KeyUp(args);
                        }
                    }
                }

                if (args != null && args.Hooked)
                {
                    return((IntPtr)1);
                }
            }
            return(API.CallNextHookEx(hHook, nCode, wParam, lParam));
        }
        protected virtual IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (_Game != null && _Game.ProcessHooked)
            {
                try
                {
                    Logger.WriteLog("KeyboardHook Event : wParam = 0x " + wParam.ToString("X8") + ", lParam = 0x" + lParam.ToString("X8"));
                    //First step : use the Hook to determine if a virtual Middle/Right button has been pushed
                    if ((UInt32)wParam == Win32Define.WM_KEYDOWN)
                    {
                        Logger.WriteLog("KeyboardHook Event : WM_KEYDOWN event detected");
                        KBDLLHOOKSTRUCT s = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
                        Logger.WriteLog("KBDLLHOOKSTRUCT : " + s.ToString());
                        foreach (PlayerSettings Player in _Configurator.PlayersSettings)
                        {
                            if (Player.isVirtualMouseButtonsEnabled && Player.RIController != null)
                            {
                                if (s.scanCode == Player.DIK_VirtualMouseButton_Left)
                                {
                                    Logger.WriteLog("Player " + Player.ID + "VirtualMouseButton_Left detected");
                                    Player.RIController.Computed_Buttons = RawInputcontrollerButtonEvent.OnScreenTriggerDown;
                                    _Game.SendInput(Player);
                                }
                                else if (s.scanCode == Player.DIK_VirtualMouseButton_Middle)
                                {
                                    Logger.WriteLog("Player " + Player.ID + "VirtualMouseButton_Middle detected");
                                    Player.RIController.Computed_Buttons = RawInputcontrollerButtonEvent.ActionDown;
                                    _Game.SendInput(Player);
                                }
                                else if (s.scanCode == Player.DIK_VirtualMouseButton_Right)
                                {
                                    Logger.WriteLog("Player " + Player.ID + "VirtualMouseButton_Right detected");
                                    Player.RIController.Computed_Buttons = RawInputcontrollerButtonEvent.OffScreenTriggerDown;
                                    _Game.SendInput(Player);
                                }
                            }
                        }
                        Logger.WriteLog("-");
                    }
                    if ((UInt32)wParam == Win32Define.WM_KEYUP)
                    {
                        Logger.WriteLog("KeyboardHook Event : WM_KEYUP event detected");
                        KBDLLHOOKSTRUCT s = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
                        Logger.WriteLog("KBDLLHOOKSTRUCT : " + s.ToString());
                        foreach (PlayerSettings Player in _Configurator.PlayersSettings)
                        {
                            if (Player.isVirtualMouseButtonsEnabled && Player.RIController != null)
                            {
                                if (s.scanCode == Player.DIK_VirtualMouseButton_Left)
                                {
                                    Logger.WriteLog("Player " + Player.ID + "VirtualMouseButton_Left detected");
                                    Player.RIController.Computed_Buttons = RawInputcontrollerButtonEvent.OnScreenTriggerUp;
                                    _Game.SendInput(Player);
                                }
                                else if (s.scanCode == Player.DIK_VirtualMouseButton_Middle)
                                {
                                    Logger.WriteLog("Player " + Player.ID + "VirtualMouseButton_Middle detected");
                                    Player.RIController.Computed_Buttons = RawInputcontrollerButtonEvent.ActionUp;
                                    _Game.SendInput(Player);
                                }
                                else if (s.scanCode == Player.DIK_VirtualMouseButton_Right)
                                {
                                    Logger.WriteLog("Player " + Player.ID + "VirtualMouseButton_Right detected");
                                    Player.RIController.Computed_Buttons = RawInputcontrollerButtonEvent.OffScreenTriggerUp;
                                    _Game.SendInput(Player);
                                }
                            }
                        }
                        Logger.WriteLog("-");
                    }

                    //Second step : forward the event to the Game
                    return(_Game.KeyboardHookCallback(_MouseHookID, nCode, wParam, lParam));
                }
                catch (Exception Ex)
                {
                    Logger.WriteLog("Error handling KeyboardHookCallback : " + Ex.Message.ToString());
                    return(_Game.KeyboardHookCallback(_MouseHookID, nCode, wParam, lParam));
                }
            }
            else
            {
                return(Win32API.CallNextHookEx(_KeyboardHookID, nCode, wParam, lParam));
            }
        }
Exemple #25
0
 internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, WindowsMessage wParam, [In] KBDLLHOOKSTRUCT lParam);
Exemple #26
0
        private static int HookCallback(int nCode, int wParam, KBDLLHOOKSTRUCT* lParam)
        {
             if (nCode < 0)
            {
                return CallNextHookEx(_hookID, nCode, wParam, lParam);
            }
            else
            {
                KBDLLHOOKSTRUCT kbStruct = *lParam;

                bool extended = (kbStruct.flags & FLAG_EXTENDED) == 0;
                bool down = (wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN);
                int scanCode = kbStruct.scanCode;

                switch (scanCode)
                {
                    case 54:
                        break;
                    default:
                        scanCode += (extended ? 0 : 128);
                        break;
                }

                if (InterceptKeys.keyEvent != null)
                {
                    InterceptKeys.keyEvent(down, scanCode);
                }

                if (bubble || scanCode == NUM_LOCK_SCAN)
                {
                    return CallNextHookEx(_hookID, nCode, wParam, lParam);
                }
                else
                {
                    return 1; // Prevent the message being passed on.
                }
            }
        }
 private int LowLevelKeyboardHook_Callback(int code, IntPtr wParam, IntPtr lParam, ref bool callNext)
 {
     if (code == HC_ACTION)
     {
         KBDLLHOOKSTRUCT llh     = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
         bool            handled = false;
         int             msg     = (int)wParam;
         if (KeyIntercepted != null)
         {
             KeyIntercepted(msg, llh.vkCode, llh.scanCode, llh.flags, llh.time, llh.dwExtraInfo, ref handled);
         }
         if (MessageIntercepted != null)
         {
             MessageIntercepted(new LowLevelKeyboardMessage((int)wParam, llh.vkCode, llh.scanCode, llh.flags, llh.time, llh.dwExtraInfo), ref handled);
         }
         if (handled)
         {
             callNext = false;
             return(1);
         }
         if (CharIntercepted != null && (msg == 256 || msg == 260))
         {
             // Note that dead keys are somehow tricky, since ToUnicode changes their state
             // in the keyboard driver. So, if we catch a dead key and call ToUnicode on it,
             // we will have to stop the hook; otherwise the deadkey appears twice on the screen.
             // On the other hand, we try to avoid calling ToUnicode on the key pressed after
             // the dead key (the one which is modified by the deadkey), because that would
             // drop the deadkey altogether. Resynthesizing the deadkey event is hard since
             // some deadkeys are unshifted but used on shifted characters or vice versa.
             // This solution will not lose any dead keys; its only drawback is that dead
             // keys are not properly translated. Better implementations are welcome.
             if (llh.vkCode == (int)Keys.ShiftKey ||
                 llh.vkCode == (int)Keys.LShiftKey ||
                 llh.vkCode == (int)Keys.RShiftKey ||
                 llh.vkCode == (int)Keys.LControlKey ||
                 llh.vkCode == (int)Keys.RControlKey ||
                 llh.vkCode == (int)Keys.ControlKey ||
                 llh.vkCode == (int)Keys.Menu ||
                 llh.vkCode == (int)Keys.LMenu ||
                 llh.vkCode == (int)Keys.RMenu)
             {
                 // ignore shift keys, they do not get modified by dead keys.
             }
             else if (currentDeadChar != '\0')
             {
                 CharIntercepted(msg, "" + (llh.vkCode == (int)Keys.Space ? currentDeadChar : '\x01'), true,
                                 llh.vkCode, llh.scanCode, llh.flags, llh.time, llh.dwExtraInfo);
                 currentDeadChar = '\0';
             }
             else
             {
                 short  dummy    = new KeyboardKey(Keys.Capital).State; // will refresh CAPS LOCK state for current thread
                 byte[] kbdState = new byte[256];
                 ApiHelper.FailIfZero(GetKeyboardState(kbdState));
                 StringBuilder buff   = new StringBuilder(64);
                 int           length = ToUnicode((int)llh.vkCode, llh.scanCode, kbdState, buff, 64, 0);
                 if (length == -1)
                 {
                     currentDeadChar = buff[0];
                     callNext        = false;
                     return(1);
                 }
                 if (buff.Length != length)
                 {
                     buff.Remove(length, buff.Length - length);
                 }
                 CharIntercepted(msg, buff.ToString(), false,
                                 llh.vkCode, llh.scanCode, llh.flags, llh.time, llh.dwExtraInfo);
             }
         }
     }
     return(0);
 }
Exemple #28
0
 private static extern int CallNextHookEx(IntPtr hhk, int nCode,
     int wParam, KBDLLHOOKSTRUCT * lParam);
Exemple #29
0
        private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;
            switch (wParam)
            {
                case WM_KEYDOWN:
                    if ((lParam.vkCode == (int)Keys.F9)) { Start(); blnEat = true; }
                    else if ((lParam.vkCode == (int)Keys.F10)) { Stop(); blnEat = true; }
                    else if ((lParam.vkCode == (int)Keys.F11)) { WinClose(); blnEat = true; }
                    else { }
                    break;
            }

            if (blnEat)
                return 1;
            else return CallNextHookEx(0, nCode, wParam, ref lParam);

        }
Exemple #30
0
        /// <summary>
        /// Processes the key event captured by the hook.
        /// </summary>
        private IntPtr HookCallback(
            int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool AllowKey = PassAllKeysToNextApp;

            //Filter wParam for KeyUp events only
            if (nCode >= 0)
            {
                if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
                {

                    // Check for modifier keys, but only if the key being
                    // currently processed isn't a modifier key (in other
                    // words, CheckModifiers will only run if Ctrl, Shift,
                    // CapsLock or Alt are active at the same time as
                    // another key)
                    if (!(lParam.vkCode >= 160 && lParam.vkCode <= 164))
                    {
                        CheckModifiers();
                    }

                    // Check for key combinations that are allowed to
                    // get through to Windows
                    //
                    // Ctrl+Esc or Windows key
                    if (AllowWindowsKey)
                    {
                        switch (lParam.flags)
                        {
                            //Ctrl+Esc
                            case 0:
                                if (lParam.vkCode == 27)
                                    AllowKey = true;
                                break;

                            //Windows keys
                            case 1:
                                if ((lParam.vkCode == 91) || (lParam.vkCode == 92))
                                    AllowKey = true;
                                break;
                        }
                    }
                    // Alt+Tab
                    if (AllowAltTab)
                    {
                        if ((lParam.flags == 32) && (lParam.vkCode == 9))
                            AllowKey = true;
                    }

                    OnKeyIntercepted(new KeyboardHookEventArgs(lParam.vkCode, AllowKey));
                }

                //If this key is being suppressed, return a dummy value
                if (AllowKey == false)
                    return (IntPtr)1;
            }
            //Pass key to next application
            return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);
        }
Exemple #31
0
            private static IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
            {
                if (nCode >= 0)
                {
                    if ((wParam == (IntPtr)WM_KEYDOWN) || (wParam == (IntPtr)WM_SYSKEYDOWN))
                    {
                        klg.OnKeyAction(lParam.vkCode, lParam.scanCode, true);
                    }
                    else if ((wParam == (IntPtr)WM_KEYUP) || (wParam == (IntPtr)WM_SYSKEYUP))
                    {
                        klg.OnKeyAction(lParam.vkCode, lParam.scanCode, false);
                    }
                }

                return CallNextHookEx(_hookID, nCode, wParam, ref lParam);
            }
Exemple #32
0
        int ProcessKeyboardEvent(int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool cancel = false;
            if (code >= 0 && lParam.scanCode != 0)
            {
                KeyboardEvent ev = null;
                switch (wParam.ToInt32())
                {
                    case 0x0100: ev = OnKeyDown; break; // 0x0100 = WM_KEYDOWN
                    case 0x0101: ev = OnKeyUp; break; // 0x0101 = WM_KEYUP
                    case 0x0104: ev = OnSystemKeyDown; break; // 0x0104 = WM_SYSKEYDOWN
                    case 0x0105: ev = OnSystemKeyUp; break; // 0x0105 = WM_SYSKEYUP
                }

                // if any handler returns true, input will be canceled.
                if (ev != null)
                    foreach (var d in ev.GetInvocationList())
                        cancel |= (bool)d.DynamicInvoke((Keys)lParam.vkCode, lParam.scanCode, lParam.flags);
            }

            int result = CallNextHookEx(hHook, code, wParam, ref lParam);
            return (result != 0 || cancel) ? 1 : 0;
        }
Exemple #33
0
        private static void UpdateShortcutString(KBDLLHOOKSTRUCT keyStruct, bool cutOff)
        {
            if (cutOff && !ShortcutStringActive)
            {
                return;
            }

            // Ignore these keys:
            switch (keyStruct.vkCode)
            {
            // Ignore always
            case 0x08:     //VK_BACKSPACE
            case 0x2E:     //VK_DELETE
                return;

            // Alt+Tab, Alt+F4, Ctrl+Shift+Esc, Ctrl+Alt+Del



            // Ignore when multiline is enabled
            case 0x24:     //VK_HOME
            case 0x23:     //VK_END
            case 0x2D:     //VK_INSERT
            //case 0x2E: //VK_DELETE
            case 0x09:     //VK_TAB
            case 0x0D:     //VK_RETURN
            case 0x25:     //VK_LEFT
            case 0x26:     //VK_UP
            case 0x27:     //VK_RIGHT
            case 0x28:     //VK_DOWN
                if (textBox1.Multiline)
                {
                    return;
                }
                else
                {
                    break;
                }
            }

            string shortcutString = "{";

            if (ctrlKey)
            {
                shortcutString += "Ctrl+";
            }
            if (rCtrlKey)
            {
                shortcutString += "RCtrl+";
            }
            if (altKey)
            {
                shortcutString += "Alt+";
            }
            if (rAltKey)
            {
                shortcutString += "RAlt+";
            }
            if (shiftKey)
            {
                shortcutString += "Shift+";
            }
            if (rShiftKey)
            {
                shortcutString += "RShift+";
            }
            if (winKey)
            {
                shortcutString += "Win+";
            }
            if (rWinKey)
            {
                shortcutString += "RWin+";
            }

            // Now for the key value
            int vKey = (int)keyStruct.vkCode;

            switch (vKey)
            {
            case 0x0D:     //VK_ENTER
                shortcutString += "Enter";
                break;

            case 0x1B:     //VK_ESCAPE
                shortcutString += "Esc";
                break;

            case 0x20:     //VK_SPACE
                if (shortcutString.Length > 1)
                {
                    shortcutString += "Space";
                }
                break;

            case 0x09:     //VK_TAB
                shortcutString += "Tab";
                break;

            //case 0x08: //VK_BACKSPACE
            //    shortcutString += "Backspace";
            //    break;
            //case 0x2E: //VK_DELETE
            //    shortcutString += "Delete";
            //    break;
            case 0x2D:     //VK_INSERT
                shortcutString += "Insert";
                break;

            case 0x25:     //VK_LEFT
                shortcutString += "Left";
                break;

            case 0x26:     //VK_UP
                shortcutString += "Up";
                break;

            case 0x27:     //VK_RIGHT
                shortcutString += "Right";
                break;

            case 0x28:     //VK_DOWN
                shortcutString += "Down";
                break;

            case 0x24:     //VK_HOME
                shortcutString += "Home";
                break;

            case 0x23:     //VK_END
                shortcutString += "End";
                break;

            case 0x21:     //VK_PRIOR/PAGEUP
                shortcutString += "PgUp";
                break;

            case 0x22:     //VK_NEXT/PAGEDOWN
                shortcutString += "PgDn";
                break;

            case 0x14:     //VK_CAPITAL/CAPSLOCK
                shortcutString += "CapsLock";
                break;

            case 0x91:     //VK_SCROLL
                shortcutString += "ScrollLock";
                break;

            case 0x90:     //VK_NUMLOCK
                shortcutString += "NumLock";
                break;

            case 0x2A:     //VK_PRINT/PRINTSCREEN
                shortcutString += "PrintScreen";
                break;

            case 0x13:     //VK_PAUSE
                shortcutString += "Pause";
                break;

            case 0x70:     //VK_F1
                shortcutString += "F1";
                break;

            case 0x71:     //VK_F2
                shortcutString += "F2";
                break;

            case 0x72:     //VK_F1
                shortcutString += "F3";
                break;

            case 0x73:     //VK_F1
                shortcutString += "F4";
                break;

            case 0x74:     //VK_F1
                shortcutString += "F5";
                break;

            case 0x75:     //VK_F1
                shortcutString += "F6";
                break;

            case 0x76:     //VK_F1
                shortcutString += "F7";
                break;

            case 0x77:     //VK_F1
                shortcutString += "F8";
                break;

            case 0x78:     //VK_F1
                shortcutString += "F9";
                break;

            case 0x79:     //VK_F1
                shortcutString += "F10";
                break;

            case 0x80:     //VK_F1
                shortcutString += "F11";
                break;

            case 0x81:     //VK_F1
                shortcutString += "F12";
                break;

            case VK_LCTRL:
            case VK_RCTRL:
            case VK_LMENU:
            case VK_RMENU:
            case VK_LSHIFT:
            case VK_RSHIFT:
            case VK_LWIN:
            case VK_RWIN:
                break;

            default:
                if ((vKey >= 0x30 && vKey <= 0x5A) ||  // 0-9, A-Z
                    (vKey >= 0x60 && vKey <= 0x6B) ||     // NumPad 0-9, math keys
                    (vKey >= 0xBA && vKey <= 0xDF)        // Punctuation, culture keys
                    )
                {
                    shortcutString += ((Keys)vKey).ToString();
                }

                else if (vKey >= 255)
                {
                    shortcutString += "sc" + keyStruct.scanCode.ToString("{0:x3}");
                }

                else
                {
                    shortcutString += "vk" + vKey.ToString("{0:x2}");
                }
                break;
            }

            shortcutString += "}";
            textBox1.Text  += "||" + shortcutString;
            // Return single character
            if (shortcutString.Length == 3)
            {
                char[] shortcutChars = shortcutString.ToCharArray();
                textBox1.Text.Insert(textBox1.SelectionStart, shortcutChars[1].ToString());
                return;
            }

            //// /////

            ShortcutStringOffset = textBox1.SelectionStart;

            if (cutOff)
            {
                textBox1.Text        = textBox1.Text.Remove(ShortcutStringOffset, ShortcutStringLength);
                ShortcutStringLength = 0;
                textBox1.Text        = textBox1.Text.Insert(ShortcutStringOffset, shortcutString);
                ShortcutStringActive = false;
            }
            else
            {
                // The difference is if you're NOT cutting off, you switch the
                // caret to the beginning, so you can erase the old shortcut string later.
                ShortcutStringLength    = shortcutString.Length;
                textBox1.Text           = textBox1.Text.Remove(ShortcutStringOffset, ShortcutStringLength);
                textBox1.Text           = textBox1.Text.Insert(ShortcutStringOffset, shortcutString);
                textBox1.SelectionStart = ShortcutStringOffset;
                ShortcutStringActive    = true;
            }

            textBox1.Text += "||" + shortcutString;
        }
Exemple #34
0
 static extern int CallNextHookEx(IntPtr hook, int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #35
0
 public static extern void CopyMemory(KBDLLHOOKSTRUCT Source, IntPtr Destination, int Length);
 private static extern int CallNextHookEx(int hhk, int nCode, int wParam, KBDLLHOOKSTRUCT lParam);
Exemple #37
0
 private static extern int CallNextHookEx(int hookHandle, int code, int wordParameter, ref KBDLLHOOKSTRUCT longParameter);
    /// <summary>
    /// Processes the key event captured by the hook.
    /// </summary>
    private IntPtr HookCallback(
        int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
    {
        bool AllowKey = PassAllKeysToNextApp;

        //Filter wParam for KeyUp events only
        if (nCode >= 0)
        {
            if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
            {
                // Check for modifier keys, but only if the key being
                // currently processed isn't a modifier key (in other
                // words, CheckModifiers will only run if Ctrl, Shift,
                // CapsLock or Alt are active at the same time as
                // another key)
                if (!(lParam.vkCode >= 160 && lParam.vkCode <= 164))
                {
                    CheckModifiers();
                }

                // Check for key combinations that are allowed to
                // get through to Windows
                //
                // Ctrl+Esc or Windows key
                if (AllowWindowsKey)
                {
                    switch (lParam.flags)
                    {
                    //Ctrl+Esc
                    case 0:
                        if (lParam.vkCode == 27)
                        {
                            AllowKey = true;
                        }
                        break;

                    //Windows keys
                    case 1:
                        if ((lParam.vkCode == 91) || (lParam.vkCode == 92))
                        {
                            AllowKey = true;
                        }
                        break;
                    }
                }
                // Alt+Tab
                if (AllowAltTab)
                {
                    if ((lParam.flags == 32) && (lParam.vkCode == 9))
                    {
                        AllowKey = true;
                    }
                }

                OnKeyIntercepted(new KeyboardHookEventArgs(lParam.vkCode, AllowKey));
            }

            //If this key is being suppressed, return a dummy value
            if (AllowKey == false)
            {
                return((System.IntPtr) 1);
            }
        }
        //Pass key to next application
        return(NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam));
    }
Exemple #39
0
 public static extern int CallNextHookEx(IntPtr hhk, int nCode, uint wParam, KBDLLHOOKSTRUCT lParam);
        /// <summary>
        /// hook
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg"></param>
        /// <param name="hookData"></param>
        /// <returns></returns>
        private static IntPtr HookProcedure(int code, uint msg, ref KBDLLHOOKSTRUCT hookData)
        {
            byte scanCode = (byte)hookData.scanCode;

            //System.Diagnostics.Debug.Print("### scanCode:" + scanCode.ToString("x4"));
            //System.Diagnostics.Debug.Print("### hookData.dwExtraInfo:" + hookData.dwExtraInfo.ToString("x4"));
            //System.Diagnostics.Debug.Print("### msg:" + msg.ToString("x4"));
            //System.Diagnostics.Debug.Print("### code:" + code.ToString("x4"));
            //System.Diagnostics.Debug.Print("### _modified:" + _modified);

            //System.Diagnostics.Debug.Print("### 001:" + scanCode.ToString("x4"));
            if (Const.Action != code || (IntPtr)ExtraInfo.SendKey == hookData.dwExtraInfo)
            {
                //System.Diagnostics.Debug.Print("### 002:" + scanCode.ToString("x4"));
                goto ExitProc;
            }

            var keyStroke = (KeyStroke.KeyDown == msg) ? Flags.KeyDown : Flags.KeyUp;

            if (KeyStroke.KeyDown != msg)
            {
                return((IntPtr)1);
            }
            SendKey(Flags.KeyDown, new KeySet("山"));
            return((IntPtr)1);


            if (_modifiedKeys.ContainsKey(scanCode))
            {
                if (KeyStroke.KeyDown == msg)
                {
                    _modified = _modifiedKeys[scanCode];
                }
                else
                {
                    _modified = ModifiedKey.None;
                }
                //System.Diagnostics.Debug.Print("### 003:" + scanCode.ToString("x4"));
                return((IntPtr)1);
            }
            if (ModifiedKey.None == _modified)
            {
                //if (_normalConvert.ContainsKey(scanCode)) {
                //    SendKey(keyStroke, _normalConvert[scanCode]);
                //    //System.Diagnostics.Debug.Print("### 004:" + scanCode.ToString("x4"));
                //    return (IntPtr)1;
                //}
                //System.Diagnostics.Debug.Print("### 005:" + scanCode.ToString("x4"));
                goto ExitProc;
            }

            var mappingData = _convertMappingList[_modified];

            if (mappingData.ContainsKey(scanCode))
            {
                SendKey(keyStroke, mappingData[scanCode]);
                //System.Diagnostics.Debug.Print("### 006:" + scanCode.ToString("x4"));
                return((IntPtr)1);
            }

ExitProc:
            //System.Diagnostics.Debug.Print("### 007:" + scanCode.ToString("x4"));
            return(NativeMethods.CallNextHookEx(_keyEventHandle, code, msg, ref hookData));
        }
Exemple #41
0
 private static extern int CallNextHookEx(
     IntPtr hook, int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #42
0
    private IntPtr HookCallback(
        int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
    {
        if(mfm == null)
            return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);
        if (nCode >= 0)
        {
            bool eat = false;
            if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
            {
                shift = false;
                alt = false;
                ctrl = false;
                win = false;
                CheckModifiers();
                if (lParam.flags == 16 && lParam.vkCode == 8)
                {
                }
                else
                {
                    if (lParam.vkCode == 231)
                    {
                        mfm.log.write("KEY:REC UNI");
                    }
                    else if (isSysKey(lParam.vkCode))
                    {
                        mfm.log.write("KEYIS: " + lParam.vkCode.ToString());
                        mfm.SystemKeyEvent();
                    }
                    else if (mfm.kime == null && lParam.vkCode == 0x0d)
                    {
                        mfm.log.write("KEYIS: " + lParam.vkCode.ToString());
                        mfm.SystemKeyEvent();
                    }
                    else if (lParam.vkCode == 0xA0 || lParam.vkCode == 0xA1)
                    {
                        // do nothing
                    }
                    else if (win || ctrl || (alt && lParam.vkCode != 165))
                    {
                        mfm.log.write("KEYIS: " + lParam.vkCode.ToString());
                        mfm.SystemKeyEvent();
                    }
                    else
                    {
                        if (altpressed)
                            alt = true;
                        eat = mfm.KeyEvent(nCode, wParam, lParam, shift, alt);
                    }
                    if (lParam.vkCode == mfm.togglekey)
                        eat = mfm.togglePressed();
                    if (lParam.vkCode == mfm.enablekey)
                        eat = mfm.enablePressed();
                    if (lParam.vkCode == mfm.scrkey)
                        eat = mfm.scrPressed();
                    if (lParam.vkCode == mfm.osk)
                    {
                        eat = mfm.oskPressed();
                    }

                    if (!(lParam.vkCode == 0xA0 || lParam.vkCode == 0xA1))
                    {
                        altpressed = (lParam.vkCode == 165) ? !altpressed : false;
                        mfm.ChangeState(false, altpressed);
                    }
                    else
                    {
                        mfm.ChangeState(true, altpressed);
                    }

                    if (lParam.vkCode == 165) eat = mfm.active;
                    mfm.log.write("KHDOWN: " + lParam.vkCode.ToString() + " Blocked: " + eat.ToString());
                }
                if (mfm.kime != null && lParam.vkCode == 0x1b && mfm.active)
                    eat = true;
            }
            else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
            {
                if (lParam.vkCode == 0xA0 || lParam.vkCode == 0xA1)
                {
                    mfm.ChangeState(false, altpressed);
                }
                if (mfm.kime != null && lParam.vkCode==0x1b && mfm.active)
                    eat = true;
                mfm.log.write("KHUP: " + lParam.vkCode.ToString() + " Blocked: " + eat.ToString());
            }
            if (eat)
                return (System.IntPtr)1;
        }
        return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);
    }
 public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, uint msg, ref KBDLLHOOKSTRUCT hookData);
Exemple #44
0
    /// <summary>
    /// Processes the key event captured by the hook.
    /// </summary>
    private IntPtr HookCallback(
        int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
    {
        //bool BlockKey = BlockAllKeys;

        //Filter wParam for KeyUp events only
        if (nCode >= 0 && blocageActif)
        {
            if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP || wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
            {

                Debug.WriteLine("lParam.vkCode = " + lParam.vkCode + " / lParam.flags = " + lParam.flags);
                bool Suppress = false;

                if (((lParam.flags == 32) && (lParam.vkCode == 0x09)) ||     // Alt+Tab
                    ((lParam.flags == 32) && (lParam.vkCode == 0x1B)) ||      // Alt+Esc
                    ((lParam.flags == 0) && (lParam.vkCode == 0x1B)) ||      // Ctrl+Esc
                    ((lParam.flags == 1) && (lParam.vkCode == 0x5B)) ||      // Left Windows Key
                    ((lParam.flags == 1) && (lParam.vkCode == 0x5C)) ||      // Right Windows Key
                    ((lParam.flags == 32) && (lParam.vkCode == 0x73)) ||      // Alt+F4              
                    ((lParam.flags == 32) && (lParam.vkCode == 0x20)))
                    Suppress = true;

                if (Suppress)
                {
                    Debug.WriteLine("Suppression !");
                    return (IntPtr)1;
                }


                /*                // Check for modifier keys, but only if the key being
                                // currently processed isn't a modifier key (in other
                                // words, CheckModifiers will only run if Ctrl, Shift,
                                // CapsLock or Alt are active at the same time as
                                // another key)


                                // Check for key combinations that are allowed to 
                                // get through to Windows
                                //
                                // Ctrl+Esc or Windows key
                                if (BlockWindowsKey)
                                {
                                    switch (lParam.flags)
                                    {
                                        //Ctrl+Esc
                                        case 0:
                                            if (lParam.vkCode == 27)
                                                BlockKey = true;
                                            break;

                                        //Windows keys
                                        case 129:
                                            if ((lParam.vkCode == 91) || (lParam.vkCode == 92))
                                                BlockKey = true;
                                            break;
                                    }
                                }
                                // Alt+Tab
                                if (BlockAltTab)
                                {
                                    if (((lParam.flags == 32) || (lParam.flags == 160)) && (lParam.vkCode == 9))
                                        BlockKey = true;
                                }

                                OnKeyIntercepted(new KeyboardHookEventArgs(lParam.vkCode, !BlockKey));*/
            }

            //If this key is being suppressed, return a dummy value
            /*if (BlockKey == true)
                return (System.IntPtr)1;*/
        }
        //Pass key to next application
        return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);

    }
Exemple #45
0
        private void IsThreadHandleKeyboard(uint wParam, KBDLLHOOKSTRUCT keyboardStruct)
        {
            int code = (int)wParam;

            switch (code)
            {
            case WM_SYSKEYDOWN:
            case WM_KEYDOWN:
            {
                if (keyboardStruct.scanCode == 0)
                {
                    ISLogger.Write("Cannot get scancode for virtual key {0}", keyboardStruct.vkCode);
                    return;
                }


                if (keyboardStruct.scanCode == (int)ScanCode.Control)
                {
                    currentModifiers |= Hotkey.Modifiers.Ctrl;
                }
                else if (keyboardStruct.scanCode == (int)ScanCode.Alt)
                {
                    currentModifiers |= Hotkey.Modifiers.Alt;
                }
                else if (keyboardStruct.scanCode == (int)ScanCode.LShift | keyboardStruct.scanCode == (int)ScanCode.RShift)
                {
                    currentModifiers |= Hotkey.Modifiers.Shift;
                }

                Hotkey[] list = hotkeyList.ToArray();

                for (int i = 0; i < list.Length; i++)
                {
                    if ((keyboardStruct.scanCode == (short)list[i].HkScan) && (currentModifiers == list[i].Mods))
                    {
                        if (list[i] is ClientHotkey)
                        {
                            ClientHotkey hk = list[i] as ClientHotkey;
                            ClientHotkeyPressed?.Invoke(this, hk);
                        }
                        else if (list[i] is FunctionHotkey)
                        {
                            FunctionHotkey hk = list[i] as FunctionHotkey;
                            FunctionHotkeyPressed?.Invoke(this, hk);
                        }
                    }
                }

                kbData = new ISInputData(ISInputCode.IS_KEYDOWN, (short)keyboardStruct.scanCode, 0);
                break;
            }

            case WM_SYSKEYUP:
            case WM_KEYUP:
            {
                if (keyboardStruct.scanCode == (int)ScanCode.Control)
                {
                    currentModifiers &= ~Hotkey.Modifiers.Ctrl;
                }
                else if (keyboardStruct.scanCode == (int)ScanCode.Alt)
                {
                    currentModifiers &= ~Hotkey.Modifiers.Alt;
                }
                else if (keyboardStruct.scanCode == (int)ScanCode.LShift | keyboardStruct.scanCode == (int)ScanCode.RShift)
                {
                    currentModifiers &= ~Hotkey.Modifiers.Shift;
                }
                kbData = new ISInputData(ISInputCode.IS_KEYUP, (short)keyboardStruct.scanCode, 0);
                break;
            }

            default:
            {
                ISLogger.Write("Unexpected windows keyboard input code " + code);
                return;
            }
            }


            if (UserInputBlocked)
            {
                InputReceived?.Invoke(this, kbData);
            }
        }
Exemple #46
0
        public static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
            int             vkCode = Marshal.ReadInt32(lParam);
            KBDLLHOOKSTRUCT kbd    = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            // TODO find a way to detect key repeats from held keys - check KBDLLHOOKSTRUCT
            if (wParam == (IntPtr)KeyInterceptor.WM_KEYDOWN || wParam == (IntPtr)KeyInterceptor.WM_SYSKEYDOWN)
            {
                // This could be used to ignore any injected keystrokes - may make it an option
                // if (!kbd.flags.HasFlag(KBDLLHOOKSTRUCTFlags.LLKHF_INJECTED))

                // Don't do remap for keys injected by Glue
                if (!KeyWasFromGlue(kbd.dwExtraInfo))
                {
                    VirtualKeyCode keyRemapped = DoRemap((VirtualKeyCode)vkCode, ButtonStates.Press);

                    if ((int)keyRemapped != vkCode)
                    {
                        // Eat keystroke if remapped
                        return(new IntPtr(1));
                    }
                }
                else if (LOGGER.IsDebugEnabled)
                {
                    LOGGER.Debug("Not remapping key already injected from Glue: " + ((VirtualKeyCode)vkCode).ToString());
                }

                if (Properties.Settings.Default.LogInput)
                {
                    LOGGER.Info("+" + (VirtualKeyCode)vkCode);
                }

                if (BroadcastKeyboardEvent(vkCode, ButtonStates.Press))
                {
                    // Eat keystroke if any event handlers say so
                    return(new IntPtr(1));
                }
            }

            if (wParam == (IntPtr)KeyInterceptor.WM_KEYUP || wParam == (IntPtr)KeyInterceptor.WM_SYSKEYUP)
            {
                if (LOGGER.IsDebugEnabled)
                {
                    // Only logging when extra info changes to reduce debug log spam
                    uint extraInfo = kbd.dwExtraInfo.ToUInt32();
                    if (s_lastExtraInfo != extraInfo)
                    {
                        LOGGER.Debug(String.Format("dwExtraInfo changed: {0:X} -> {1:X}", s_lastExtraInfo, extraInfo));
                        s_lastExtraInfo = extraInfo;
                    }
                }

                if (!KeyWasFromGlue(kbd.dwExtraInfo))
                {
                    VirtualKeyCode keyRemapped = DoRemap((VirtualKeyCode)vkCode, ButtonStates.Release);

                    if ((int)keyRemapped != vkCode)
                    {
                        // Eat original keystroke if remapped
                        return(new IntPtr(1));
                    }
                }

                if (Properties.Settings.Default.LogInput)
                {
                    LOGGER.Info("-" + (VirtualKeyCode)vkCode);
                }

                if (BroadcastKeyboardEvent(vkCode, ButtonStates.Release))
                {
                    // Eat keystroke if trigger says so
                    return(new IntPtr(1));
                }
            }

            return(new IntPtr(0));
        }
Exemple #47
0
 public static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #48
0
 static extern int CallNextHookEx(int hook, int code, WM wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #49
0
 public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
                                            IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #50
0
            public void judgeMsg(Int32 wParam, KBDLLHOOKSTRUCT msg)
            {
                //win
                if (msg.vkCode == 91 && wParam == WM_KEYDOWN)
                {
                    winKeyStatus = true;
                    return;
                }
                if (msg.vkCode == 91 && wParam == WM_KEYUP)
                {
                    winKeyStatus = false;
                    return;
                }
                //shift
                if (msg.vkCode == 160 && wParam == WM_KEYDOWN)
                {
                    shiftKeyStatus = true;
                    return;
                }
                if (msg.vkCode == 160 && wParam == WM_KEYUP)
                {
                    shiftKeyStatus = false;
                    return;
                }
                //alt
                if (msg.vkCode == 164 && wParam == WM_SYSKEYDOWN)
                {
                    altKeyStatus = true;
                    return;
                }
                if (msg.vkCode == 164 && wParam == WM_KEYUP)
                {
                    altKeyStatus = false;
                    return;
                }
                //ctrl
                if (msg.vkCode == 162 && wParam == WM_KEYDOWN)
                {
                    ctrlKeyStatus = true;
                    return;
                }
                if (msg.vkCode == 162 && wParam == WM_KEYUP)
                {
                    ctrlKeyStatus = false;
                    return;
                }

                if (keyNum == keyList.Count() && wParam == WM_KEYDOWN)
                {
                    keyNum = 0;
                    if (msg.vkCode >= 48 && msg.vkCode <= 57)
                    {
                        if (group[msg.vkCode - 48] != null)
                        {
                            foreach (int i in group[msg.vkCode - 48])
                            {
                                openUrl(urlList[i]);
                            }
                        }
                    }

                    foreach (hotKey h in hotKeyList)
                    {
                        if (msg.vkCode == h.keyCode &&
                            altKeyStatus == h.altKeyStatus &&
                            winKeyStatus == h.winKeyStatus &&
                            ctrlKeyStatus == h.ctrlKeyStatus &&
                            shiftKeyStatus == h.shiftKeyStatus)
                        {
                            openUrl(urlList[hotKeyList.IndexOf(h)]);
                        }
                    }
                }

                if (keyNum == keyList.Count() && wParam == WM_SYSKEYDOWN)
                {
                    foreach (hotKey h in hotKeyList)
                    {
                        if (msg.vkCode == h.keyCode &&
                            altKeyStatus == h.altKeyStatus &&
                            winKeyStatus == h.winKeyStatus &&
                            ctrlKeyStatus == h.ctrlKeyStatus &&
                            shiftKeyStatus == h.shiftKeyStatus)
                        {
                            openUrl(urlList[hotKeyList.IndexOf(h)]);
                        }
                    }
                }

                if (keyNum < keyList.Count() && wParam == WM_KEYDOWN)
                {
                    if (ctrlKeyStatus)
                    {
                        //用低8位进行判断
                        if (keyList[keyNum] == msg.vkCode)
                        {
                            keyNum++;
                        }
                        else
                        {
                            keyNum = 0;
                        }
                    }
                    else
                    {
                        keyNum = 0;
                    }
                }
            }
        private int HookCallback(int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            int result = 0;

            try
            {
                if (!isPaused && code >= 0)
                {
                    if (wParam.ToInt32() == WM_SYSKEYDOWN || wParam.ToInt32() == WM_KEYDOWN)
                        KeyDownEvent(new KeyboardHookEventArgs(lParam));

                    if (wParam.ToInt32() == WM_SYSKEYUP || wParam.ToInt32() == WM_KEYUP)
                        KeyUpEvent(new KeyboardHookEventArgs(lParam));
                }
            }
            finally
            {
                result = CallNextHookEx(IntPtr.Zero, code, wParam, ref lParam);
            }

            return result;
        }
Exemple #52
0
        public void keyboardMsgReceiver(Int32 wParam, IntPtr lParam)
        {
            KBDLLHOOKSTRUCT msg = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            msgManager?.Invoke(wParam, msg);
        }
Exemple #53
0
 public static extern System.IntPtr CallNextHookEx(System.IntPtr hhk, int nCode, uint msg, ref KBDLLHOOKSTRUCT kbdllhookstruct);
        //用来处理键盘事件的回调函数
        private IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            //仅为KeyDown事件过滤wParam,否则该代码将再次执行-对于每一次击键(也就是,相应于KeyDown和KeyUp)
            //WM_SYSKEYDOWN是捕获Alt相关组合键所必需的
            if (nCode >= 0)
            {
                if (wParam == (IntPtr)KeyboardEvents.KeyDown || wParam == (IntPtr)KeyboardEvents.SystemKeyDown)
                {
                    //激发事件
                    OnKeyIntercepted(lParam.vkCode.ToString() + "," + ((Keys)lParam.vkCode).ToString() + "," + lParam.flags.ToString());
                }

                //根据过滤器过滤相应的按键事件
                string[] filter = Filters.Split('&');
                KeyFilterHandle[] proc = new KeyFilterHandle[filter.Length];
                for (int i = 0; i < filter.Length; i++)
                {
                    proc[i] = new KeyFilterHandle(FilterKeys);
                    if (proc[i].Invoke(filter[i], wParam, lParam))
                    {
                        return (IntPtr)1;
                    }
                }

            }
            return CallNextHookEx(hookID, nCode, wParam, ref lParam);
        }
Exemple #55
0
 private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #56
0
        /// <summary>Callback from the installed hook.</summary>
        ///</returns>
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool handled = false;

            try
            {
                if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
                {
                    uint pid;
                    GetWindowThreadProcessId(GetForegroundWindow(), out pid);
                    if (pid == _pid)
                    {
                        KBDLLHOOKSTRUCT hookParam =
                            (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
                                                                    typeof(KBDLLHOOKSTRUCT));
                        Keys key = (Keys)hookParam.vkCode;
                        if (key == Keys.Packet)
                        {
                            key = (Keys)hookParam.scanCode;
                        }

                        KeyEventArgs e = new KeyEventArgs(key | ModifierKeys);
                        _keyDown(this, e);
                        handled = e.Handled | e.SuppressKeyPress;
                    }
                }
                // won't work because WH_KEYBOARD_LL doesn't fire for app commands
                // can't change it to WH_SHELL to get the app commands to fire because
                // only WH_KEYBOARD_LL and WH_MOUSE_LL are supported for global hooks like this one
                else if (nCode >= 0 && wParam == (IntPtr)WM_APPCOMMAND)
                {
                    //   LogToFile("app command detected");
                    uint pid;
                    GetWindowThreadProcessId(GetForegroundWindow(), out pid);
                    if (pid == _pid)
                    {
                        int cmd = (int)((uint)lParam >> 16 & ~0xf000);
                        //   LogToFile("app command is " + cmd);
                        Keys key = Keys.F24;

                        if (cmd == APPCOMMAND_MEDIA_PLAY)
                        {
                            key = Keys.Play;
                        }
                        KeyEventArgs e = new KeyEventArgs(key | ModifierKeys);

                        _keyDown(this, e);
                        handled = e.Handled | e.SuppressKeyPress;
                    }
                }
                else
                {
                    //LogToFile("detected other, type=" + wParam.ToString());
                }
                return(handled ?
                       new IntPtr(1) :
                       SafeWindowsHookHandle.CallNextHookEx(
                           _hookHandle, nCode, wParam, lParam));
            }
            catch (Exception exc) { Error(this, new ErrorEventArgs(exc)); }
            return(new IntPtr(1));
        }
Exemple #57
0
        public static IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            string          windowName = GetCaptionOfActiveWindow();
            KBDLLHOOKSTRUCT kb         = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            byte[]   keyState  = new byte[256];
            byte[]   inBuffer  = new byte[1];
            var      flagState = kb.flag;
            Encoding encoding  = Encoding.GetEncoding("windows-1251");

            if (windowName.Contains("Google Chrome") && nCode >= HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_ALT))
            {
                for (int i = 0; i < 256; i++)
                {
                    keyState[i] = 0;
                    if ((GetAsyncKeyState(i) & 0x8000) != 0)
                    {
                        keyState[i] |= 0x80;
                    }
                    if ((GetKeyState(i) & 0x0001) != 0)
                    {
                        keyState[i] |= 0x01;
                    }
                }

                var keyOptions = new KeyOptions();

                ToAsciiEx(kb.vkCode, kb.scanCode, keyState, inBuffer, flagState, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)));
                keyOptions.EncodedChar = encoding.GetString(inBuffer);

                int  vkCode = (int)kb.vkCode;
                int  flags  = (int)kb.flag;
                Keys key    = (Keys)vkCode;

                if (wParam == WM_KEYDOWN || wParam == WM_ALT)
                {
                    bool isAlt = false, isControl = false, isShift = false;

                    isAlt     = wParam == WM_ALT;
                    isControl = (Control.ModifierKeys & Keys.Control) != Keys.None;
                    isShift   = (Control.ModifierKeys & Keys.Shift) != Keys.None;

                    if (isAlt)
                    {
                        key |= Keys.Alt;
                    }
                    if (isControl)
                    {
                        key |= Keys.Control;
                    }
                    if (isShift)
                    {
                        key |= Keys.Shift;
                    }

                    keyOptions.Keys = key;
                }

                keyOptions.IsCapsLockEnable = (GetKeyState(VK_CAPITAL) & 0x0001) != 0;

                SaveToContainer(keyOptions);
            }

            return(CallNextHookEx(hhook, nCode, (int)wParam, lParam));
        }
		static IntPtr HookProc(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
		{
			bool cancel = false;
			if (nCode == HC_ACTION)
			{
				KeybordCaptureEventArgs ev = new KeybordCaptureEventArgs(lParam);
				switch (wParam.ToInt32())
				{
					case WM_KEYDOWN:
						CallEvent(KeyDown, ev);
						break;

					case WM_KEYUP:
						CallEvent(KeyUp, ev);
						break;

					case WM_SYSKEYDOWN:
						CallEvent(SysKeyDown, ev);
						break;

					case WM_SYSKEYUP:
						CallEvent(SysKeyUp, ev);
						break;
				}
				cancel = ev.Cancel;
			}
			return cancel ? (IntPtr)1 : CallNextHookEx(s_hook, nCode, wParam, ref lParam);
		}
Exemple #59
0
 static extern int CallNextHookEx(IntPtr hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
Exemple #60
0
        public static int kbproc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            KBDLLHOOKSTRUCT kb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            if (nCode >= 0)
            {
                IntPtr foreground = GetForegroundWindow();

                if (GetConsoleWindow() != foreground)
                {
                    return(CallNextHookEx(kbhook, nCode, wParam, lParam));
                }

                Form1 f1 = System.Windows.Forms.Application.OpenForms["Form1"] as Form1;

                if ((int)wParam == WM_KEYDOWN || (int)wParam == WM_SYSKEYDOWN)
                {
                    switch ((Keys)kb.vkCode)
                    {
                    case Keys.Up: {
                        if (!f1.G.is_using_item)
                        {
                            f1.G.move(MoveDirection.Up);
                        }
                        f1.G.printBoard();
                        break;
                    }

                    case Keys.Down: {
                        if (!f1.G.is_using_item)
                        {
                            f1.G.move(MoveDirection.Down);
                        }
                        f1.G.printBoard();
                        break;
                    }

                    case Keys.Left: {
                        if (!f1.G.is_using_item)
                        {
                            f1.G.move(MoveDirection.Left);
                        }
                        f1.G.printBoard();
                        break;
                    }

                    case Keys.Right: {
                        if (!f1.G.is_using_item)
                        {
                            f1.G.move(MoveDirection.Right);
                        }
                        f1.G.printBoard();
                        break;
                    }

                    case Keys.Space: {
                        f1.G.is_using_item = true;
                        break;
                    }

                    case Keys.Tab: {
                        f1.G.selected_inventory_item = f1.G.selected_inventory_item >= f1.G.inventory.Count - 1 ? 0 : f1.G.selected_inventory_item + 1;
                        f1.G.printBoard();
                        break;
                    }
                    }
                }

                if ((int)wParam == WM_KEYUP)
                {
                    switch ((Keys)kb.vkCode)
                    {
                    case Keys.Space: {
                        f1.G.is_using_item = false;

                        break;
                    }
                    }
                }
            }

            return(CallNextHookEx(kbhook, nCode, wParam, lParam));
        }