Exemple #1
0
        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            IntPtr hFocus = NativeMethods.GetForegroundWindow();

            //Tracer.Dump("GFW: {0} ; hCWnd: {1}", hFocus, _hCWnd);
            bool passThru = true;
            // are we focused? (e.g. was this our input?)
            bool global = (_hCWnd != hFocus);

            if (nCode >= 0 && (wParam == (IntPtr)NativeMethods.WM_KEYDOWN))
            {
                try {
                    var keyInfo =
                        (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

                    bool shift   = (NativeMethods.GetKeyState(NativeMethods.VK_SHIFT) & ~1) != 0;
                    bool control = (NativeMethods.GetKeyState(NativeMethods.VK_CONTROL) & ~1) != 0;
                    bool alt     = (NativeMethods.GetKeyState(NativeMethods.VK_MENU) & ~1) != 0; // ALT

                    char keyChar = (char)NativeMethods.MapVirtualKey(
                        (uint)keyInfo.vkCode, NativeMethods.MAPVK_VK_TO_CHAR);

                    var consoleKeyInfo = new ConsoleKeyInfo(keyChar,
                                                            (ConsoleKey)keyInfo.vkCode, shift, alt, control);

                    var args = new ConsoleKeyEventArgs(consoleKeyInfo, global);

                    // notify any listeners
                    PSHotKeyManager.Instance.OnConsoleKeyDown(args);

                    // eat keystroke or not?
                    passThru = !args.Cancel;
                }
                catch (Exception ex) {
                    string message =
                        String.Format("HookCallback threw an exception: {0}. Disabling hook.", ex.Message);
                    Console.WriteLine(message);
                    Trace.WriteLine(message, "PSEventingKeyHandler");

                    // dispose/unhook/suppressfinalize
                    using (this) {}
                }
            }

            if (passThru)
            {
                return(NativeMethods.CallNextHookEx(_hook, nCode, wParam, lParam));
            }

            // eat it
            return(new IntPtr(1)); // non zero to eat it
            //return IntPtr.Zero;
        }
Exemple #2
0
        internal void OnConsoleKeyDown(ConsoleKeyEventArgs keyEventArgs)
        {
            Tracer.Dump("PSHotKeyManager.OnConsoleKeyDown (Global: {0}) {1} {2} {3}",
                        keyEventArgs.Global,
                        keyEventArgs.KeyInfo.Key,
                        keyEventArgs.KeyInfo.KeyChar,
                        keyEventArgs.KeyInfo.Modifiers);

            var temp = ConsoleKeyDown;

            if (temp != null)
            {
                temp(this, keyEventArgs);
            }
        }
Exemple #3
0
            private void OnConsoleKeyDown(Object sender, ConsoleKeyEventArgs args)
            {
                Tracer.Dump("HotKeyListener_{0} OnConsoleKeyDown.", _listenerId);

                // if watching global or key is local, and key matches
                if ((_watchGlobal || !args.Global) && (args.KeyInfo == _myHotKey))
                {
                    if (!_passThru)
                    {
                        // eat the keystroke
                        args.Cancel = true;
                    }
                    Tracer.Dump("HotKeyListener_{0} HotKey triggered.", _listenerId);

                    // notify powershell 2.0 eventing
                    HotKeyInternal(this, args);
                }
            }