Esempio n. 1
0
        /**
         * Assigns Modifier Key values keeping in mind their histories.
         * Helpful it Ctrl+X was pressed but Ctrl key was released before X key.
         *
         */
        private KeyHookEventArgs CreateArgument(KeyHookEventArgs e)
        {
            bool alt   = e.Alt;
            bool ctrl  = e.Control;
            bool shift = e.Shift;

            foreach (int key in this.modifierKeys)
            {
                switch ((Keys)key)
                {
                case Keys.LMenu:
                case Keys.RMenu:
                    alt = true;
                    break;

                case Keys.LShiftKey:
                case Keys.RShiftKey:
                    shift = true;
                    break;

                case Keys.LControlKey:
                case Keys.RControlKey:
                    ctrl = true;
                    break;
                }
            }
            return(new KeyHookEventArgs((uint)e.KeyValue, e.KeyChar, e.Injected, alt, ctrl, shift, e.CapsLock, e.NumLock, e.ScrollLock));
        }
Esempio n. 2
0
        /// <summary>
        /// The callback for the keyboard hook
        /// </summary>
        /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
        /// <param name="wParam">The event type</param>
        /// <param name="lParam">The keyhook event information</param>
        /// <returns></returns>
        private IntPtr hookProc(int code, IntPtr wParam, ref NativeMethods.keyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                Key key = KeyInterop.KeyFromVirtualKey(lParam.vkCode);
                if (HookedKeys.Contains(key))
                {
                    var wparam = wParam.ToInt64();

                    var args = new KeyHookEventArgs(key);

                    if ((wparam == NativeMethods.WM_KEYDOWN || wparam == NativeMethods.WM_SYSKEYDOWN) && (KeyDown != null))
                    {
                        KeyDown.Invoke(this, args);
                    }
                    else if ((wparam == NativeMethods.WM_KEYUP || wparam == NativeMethods.WM_SYSKEYUP) && (KeyUp != null))
                    {
                        KeyUp.Invoke(this, args);
                    }

                    if (args.Handled)
                    {
                        return(new IntPtr(1));
                    }
                }
            }
            return(NativeMethods.CallNextHookEx(hhook, code, wParam, ref lParam));
        }
Esempio n. 3
0
        public static Int32 Hook_Callback(Int32 code, Int32 wParam, ref keyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                Key     key       = (Key)System.Windows.Input.KeyInterop.KeyFromVirtualKey(lParam.vkCode);
                Boolean IsPressed = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);

                SetModifiers(key, IsPressed, lParam.vkCode);
                if (HookedKeys.Contains(key))
                {
                    KeyHookEventArgs kea = CreateEventArgs(key);

                    Boolean Handled = false;
                    if (IsPressed)
                    {
                        Handled = TriggerKeyDown(kea);
                    }
                    else
                    {
                        Handled = TriggerKeyUp(kea);
                    }

                    if (Handled)
                    {
                        return(1);
                    }
                }
            }
            return(CallNextHookEx(Hook, code, wParam, ref lParam));
        }
        public static int Hook_Callback(int code, int wParam, ref keyboardHookStruct lParam)
        {
            if (code < 0)
            {
                return(CallNextHookEx(Hook, code, wParam, ref lParam));
            }

            var  key       = (Key)System.Windows.Input.KeyInterop.KeyFromVirtualKey(lParam.vkCode);
            bool IsPressed = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);

            SetModifiers(key, IsPressed, lParam.vkCode);

            if (!HookedKeys.Contains(key))
            {
                return(CallNextHookEx(Hook, code, wParam, ref lParam));
            }

            KeyHookEventArgs kea = CreateEventArgs(key);

            var Handled = false;

            Handled = IsPressed ? TriggerKeyDown(kea) : TriggerKeyUp(kea);

            return(Handled ? 1 : CallNextHookEx(Hook, code, wParam, ref lParam));
        }
Esempio n. 5
0
 bool onKey(KeyHookEventArgs e)
 {
     if (e.Key == Keys.PageUp)
     {
         active = !active;
         this.Invalidate(false);
     }
     return(true);
 }
Esempio n. 6
0
 public static Boolean TriggerKeyUp(KeyHookEventArgs args)
 {
     if (KeyUp != null)
     {
         return(KeyUp(args));
     }
     else
     {
         return(false);
     }
 }
 public static bool TriggerKeyDown(KeyHookEventArgs args)
 {
     if (KeyDown != null)
     {
         return(KeyDown(args));
     }
     else
     {
         return(false);
     }
 }
Esempio n. 8
0
        public static byte[] GetEncodedData(EventClass eventClass, EventType type, HookArgs args, int[] pressedKeys = null, char pressedChar = '\0')
        {
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                ms.WriteByte(GetHeaderByte(eventClass, type, args));
                if (eventClass == EventClass.MouseEvent)
                {
                    MouseHookEventArgs me = args as MouseHookEventArgs;
                    ms.Write(BitConverter.GetBytes((ushort)me.Location.X), 0, 2);
                    ms.Write(BitConverter.GetBytes((ushort)me.Location.Y), 0, 2);
                    //ms.WriteByte((byte)me.Location.X);
                    //ms.WriteByte((byte)me.Location.Y);
                    //System.Windows.Forms.MouseButtons.Right;

                    ms.WriteByte((byte)((int)me.Button >> 20));
                    byte extra = 0;
                    switch (type)
                    {
                    case EventType.MouseClick:
                    case EventType.MouseDblClick:
                        extra = (byte)me.ClickCount;
                        break;

                    case EventType.MouseWheel:
                        extra = (byte)Math.Abs(me.ScrollAmount);
                        if (me.ScrollAmount < 0)
                        {
                            extra |= 0x80;
                        }
                        break;
                    }
                    ms.WriteByte(extra);
                }
                else
                {
                    KeyHookEventArgs ke    = args as KeyHookEventArgs;
                    byte[]           pKeys = { 0, 0, 0 };
                    int len = Math.Min(3, pressedKeys.Length);
                    for (int i = 0; i < len; i++)
                    {
                        pKeys[i] = (byte)pressedKeys[i];
                    }

                    ms.Write(pKeys, 0, 3);
                    ms.WriteByte((byte)pressedChar);
                }

                data = ms.ToArray();
            }
            return(data);
        }
Esempio n. 9
0
 void kh_KeyPressed(KeyHookEventArgs e)
 {
     // Will not receive CTRL+F6
     // Only CTRL + Alphabet
     if (!(e.KeyChar == '\0' || Char.IsControl(e.KeyChar)))
     {
         if (this.pressedChar != e.KeyChar)                // If pressed CharKey is different then process. Avoids clutter.
         {
             this.pressedChar = e.KeyChar;
             LogKeyCombo(e);
             this.pressedKeys.Remove(e.KeyValue);
         }
     }
 }
Esempio n. 10
0
 void kh_KeyDown(KeyHookEventArgs e)
 {
     if (this.IsModifierKey(e.KeyCode))
     {
         if (this.modifierKeys.IndexOf(e.KeyValue) < 0)
         {
             this.modifierKeys.Add(e.KeyValue);
         }
     }
     else if (this.pressedKeys.IndexOf(e.KeyValue) < 0)
     {
         this.pressedKeys.Add(e.KeyValue);
     }
 }
Esempio n. 11
0
 // Called when a key is pressed or released regardless of form focus. etc.
 public void OnKeyHook(object sender, KeyHookEventArgs e)
 {
     if (e.Key == Settings.Default.Audio_PTTKey)
     {
         if (e.KeyDown)
         {
             StartRecording();
         }
         else
         {
             recorder.StopRecording();
         }
     }
 }
Esempio n. 12
0
 private void LogKeyCombo(KeyHookEventArgs e)
 {
     if (this.pressedKeys.Count != 0)
     {
         /*string _t = String.Format("[{0}]", String.Join(" + ", this.pressedKeys.ToArray()));
          * if (this.pressedChar != '\0')
          * {
          *  _t += this.pressedChar;
          *  this.pressedChar = '\0';
          * }
          * this.keyLog.Enqueue(_t);*/
         this.keyLog.Enqueue(HookEventCodec.GetEncodedData(HookEventCodec.EventClass.KeyEvent, HookEventCodec.EventType.KeyPress, this.CreateArgument(e), this.pressedKeys.ToArray(), this.pressedChar));
     }
 }
Esempio n. 13
0
        /// <summary>
        /// Processes the key event captured by the hook.
        /// </summary>
        private IntPtr HookCallback(
            int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            //Filter wParam for KeyUp events only
            if (nCode >= 0)
            {
                var khargs = new KeyHookEventArgs(wParam, lParam);
                var allow  = AllowKey?.Invoke(khargs) != false;

                KeyIntercepted?.Invoke(khargs, allow);

                //If this key is being suppressed, return a dummy value
                if (!allow)
                {
                    return((IntPtr)1);
                }
            }
            //Pass key to next application
            return(NativeMethods.CallNextHookEx(hookID_, nCode, wParam, ref lParam));
        }
Esempio n. 14
0
        void kh_KeyUp(KeyHookEventArgs e)
        {
            // Reset CharKey
            if (this.pressedChar != '\0')
            {
                this.pressedKeys.Remove(((int)this.pressedChar - 32));  // Remove CharKey if present due to persistant press
                this.pressedChar = '\0';
            }

            // Log Key
            LogKeyCombo(e);

            if (this.IsModifierKey(e.KeyCode))
            {
                this.modifierKeys.Remove(e.KeyValue);
            }
            else
            {
                this.pressedKeys.Remove(e.KeyValue);
            }
        }
Esempio n. 15
0
        private HookEventArgs[] DecodeHookEvents(Stream ls)
        {
            List <HookEventArgs> hookEvents = new List <HookEventArgs>();

            MemoryStream ms = ls as MemoryStream;

            HookEventCodec.EventClass ec;
            HookEventCodec.EventType  et;
            HookEventArgs             ha;

            byte[] pressedKeys;
            char   pressedChar;
            int    offset = 0;

            while (HookEventCodec.GetDecodedData(offset, ms.ToArray(), out ec, out et, out ha, out pressedKeys, out pressedChar))
            {
                if (ec == HookEventCodec.EventClass.MouseEvent)
                {
                    ha      = new MouseHookEventArgsEx(ha as MouseHookEventArgs, et);
                    offset += (int)HookEventCodec.Offsets.MouseLogOffset;
                }
                else
                {
                    KeyHookEventArgs e = ha as KeyHookEventArgs;
                    List <System.Windows.Forms.Keys> pKeys = new List <System.Windows.Forms.Keys>();
                    foreach (byte b in pressedKeys)
                    {
                        if (b != 0)
                        {
                            pKeys.Add((System.Windows.Forms.Keys)b);
                        }
                    }

                    offset += (int)HookEventCodec.Offsets.KeyLogOffset;
                    ha      = new KeyHookEventArgsEx(e, et, pKeys, pressedChar);
                }
                hookEvents.Add(ha);
            }
            return(hookEvents.ToArray());
        }
Esempio n. 16
0
        public void DecodeEvent(int owner, byte[] data)
        {
            this._neuroLog.WriteFormat("Decode Event", "Owner: {0}\n\nData:\n{1}", owner, data);
            HookEventCodec.EventClass ec;
            HookEventCodec.EventType  et;
            HookEventArgs             ea;

            byte[] _b;
            char   _c;

            HookEventCodec.GetDecodedData(0, data, out ec, out et, out ea, out _b, out _c);
            if (ec == HookEventCodec.EventClass.KeyEvent)
            {
                KeyHookEventArgs ke = ea as KeyHookEventArgs;
                switch (et)
                {
                case HookEventCodec.EventType.KeyDown:
                    //Log.WriteLine("[KeyDown]->" + ke.KeyCode);
                    this._neuroLog.WriteFormat("Key Down", "Key Code: {0}", ke.KeyCode);
                    this._ki.KeyDown(ke.KeyCode);
                    break;

                case HookEventCodec.EventType.KeyPress:
                    //Log.WriteLine("[KeyPress]->" + ke.KeyCode);
                    this._neuroLog.WriteFormat("Key Press", "Key Code: {0}", ke.KeyCode);
                    this._ki.KeyPress(ke.KeyCode, ke.Alt, ke.Control, ke.Shift);
                    break;

                case HookEventCodec.EventType.KeyUp:
                    //Log.WriteLine("[KeyUp]->" + ke.KeyCode);
                    this._neuroLog.WriteFormat("Key Up", "Key Code: {0}", ke.KeyCode);
                    this._ki.KeyUp(ke.KeyCode);
                    break;
                }
            }
            else if (ec == HookEventCodec.EventClass.MouseEvent)
            {
                MouseHookEventArgs me = ea as MouseHookEventArgs;
                KeyHookEventArgs   ke = ea as KeyHookEventArgs;
                switch (et)
                {
                case HookEventCodec.EventType.MouseClick:
                    //Log.WriteLine("[Click]");
                    this._neuroLog.WriteFormat("Mouse Click", "Button: {0}\nCount: {1}", me.Button, me.ClickCount);
                    this._mi.MouseClick(me.Button, me.ClickCount);
                    break;

                case HookEventCodec.EventType.MouseDoubleClick:
                    //Log.WriteLine("[DoubleClick]");
                    this._neuroLog.WriteFormat("Mouse Double Click", "Button: {0}", me.Button);
                    this._mi.MouseDoubleClick(me.Button);
                    break;

                case HookEventCodec.EventType.MouseDown:
                    //Log.WriteLine("[MouseDown]");
                    this._neuroLog.WriteFormat("Mouse Down", "Button: {0}", me.Button);
                    this._mi.MouseDown(me.Button);
                    break;

                case HookEventCodec.EventType.MouseMove:
                    //int x = (int)(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width * (me.Location.X / Math.Pow(10, this.NormalizedPrecisionFactor)));
                    //int y = (int)(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height * (me.Location.Y / Math.Pow(10, this.NormalizedPrecisionFactor)));
                    int x = (int)(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width * ((ushort)me.Location.X / 65535.0f));
                    int y = (int)(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height * ((ushort)me.Location.Y / 65535.0f));
                    //this._neuroLog.WriteFormat("Mouse Move", "Normalized Location: {0}, {1}\nClient Location: {2}, {3}", me.Location.X, me.Location.Y, x, y);
                    //Log.WriteLine(String.Format("{0} -> {1}, {2} -> {3}", (ushort)me.Location.X, x, (ushort)me.Location.Y, y));

                    /*using (StreamWriter sw = new StreamWriter("log.txt",true))
                     * {
                     *  sw.WriteLine(String.Format("[{0},{1}] | {2}, {3}", me.Location.X.ToString(), me.Location.Y.ToString(), x.ToString(), y.ToString()));
                     * }*/
                    this._mi.MouseMove(x, y);
                    break;

                case HookEventCodec.EventType.MouseUp:
                    //Log.WriteLine("[MouseUp]");
                    this._neuroLog.WriteFormat("Mouse Up", "Button: {0}", me.Button);
                    this._mi.MouseUp(me.Button);
                    break;

                case HookEventCodec.EventType.MouseWheel:
                    //Log.WriteLine("[Scroll]");
                    this._neuroLog.WriteFormat("Mouse Scroll", "Amount: {0}", me.ScrollAmount);
                    this._mi.MouseScroll(me.ScrollAmount);
                    break;
                }
            }
        }
Esempio n. 17
0
 public static Boolean TriggerKeyUp(KeyHookEventArgs args)
 {
     if (KeyUp != null)
     return KeyUp(args);
      else return false;
 }
Esempio n. 18
0
 public static bool TriggerKeyUp(KeyHookEventArgs args)
 {
     return KeyUp != null && KeyUp(args);
 }
Esempio n. 19
0
 public static bool TriggerKeyDown(KeyHookEventArgs args)
 {
     return KeyDown != null && KeyDown(args);
 }
Esempio n. 20
0
        /// <summary>
        /// The callback for the keyboard hook
        /// </summary>
        /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
        /// <param name="wParam">The event type</param>
        /// <param name="lParam">The keyhook event information</param>
        /// <returns></returns>
        private IntPtr hookProc(int code, IntPtr wParam, ref NativeMethods.keyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                Key key = KeyInterop.KeyFromVirtualKey(lParam.vkCode);
                if (HookedKeys.Contains(key))
                {
                    var wparam = wParam.ToInt64();

                    var args = new KeyHookEventArgs(key);

                    if ((wparam == NativeMethods.WM_KEYDOWN || wparam == NativeMethods.WM_SYSKEYDOWN) && (KeyDown != null))
                    {
                        KeyDown.Invoke(this, args);
                    }
                    else if ((wparam == NativeMethods.WM_KEYUP || wparam == NativeMethods.WM_SYSKEYUP) && (KeyUp != null))
                    {
                        KeyUp.Invoke(this, args);
                    }

                    if (args.Handled)
                        return new IntPtr(1);
                }
            }
            return NativeMethods.CallNextHookEx(hhook, code, wParam, ref lParam);
        }
 public static bool TriggerKeyDown(KeyHookEventArgs args)
 {
     if (KeyDown != null)
         return KeyDown(args);
     else return false;
 }
 public static bool TriggerKeyUp(KeyHookEventArgs args)
 {
     return(KeyUp != null && KeyUp(args));
 }
Esempio n. 23
0
        public static bool GetDecodedData(int offset, byte[] data, out EventClass eventClass, out EventType type, out HookArgs args, out byte[] pressedKeys, out char pressedChar)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                // Set Offset
                ms.Position = offset;

                // Assign
                pressedKeys = null;
                pressedChar = '\0';
                eventClass  = EventClass.None;
                type        = EventType.None;
                args        = null;

                // Check
                if (ms.Position < ms.Length)
                {
                    FromHeaderByte((byte)ms.ReadByte(), out eventClass, out type, out args);
                    pressedKeys = new byte[3] {
                        0, 0, 0
                    };

                    if (eventClass == EventClass.MouseEvent)
                    {
                        //int x = ms.ReadByte();
                        //int y = ms.ReadByte();
                        byte[] loc = new byte[2];

                        ms.Read(loc, 0, 2);
                        int x = BitConverter.ToInt16(loc, 0);
                        ms.Read(loc, 0, 2);
                        int y = BitConverter.ToInt16(loc, 0);
                        System.Windows.Forms.MouseButtons button = System.Windows.Forms.MouseButtons.None;
                        switch (ms.ReadByte())
                        {
                        case 1:
                            button = System.Windows.Forms.MouseButtons.Left;
                            break;

                        case 2:
                            button = System.Windows.Forms.MouseButtons.Right;
                            break;

                        case 4:
                            button = System.Windows.Forms.MouseButtons.Middle;
                            break;

                        case 8:
                            button = System.Windows.Forms.MouseButtons.XButton1;
                            break;

                        case 16:
                            button = System.Windows.Forms.MouseButtons.XButton2;
                            break;
                        }
                        int extra  = ms.ReadByte();
                        int click  = 0;
                        int scroll = 0;
                        switch (type)
                        {
                        case EventType.MouseClick:
                        case EventType.MouseDblClick:
                            click = extra;
                            break;

                        case EventType.MouseWheel:
                            if ((extra & 0x80) == 0x80)
                            {
                                scroll = -1;
                            }
                            else
                            {
                                scroll = 1;
                            }

                            extra &= 0x7F;
                            scroll = scroll * extra * 120;     //Delta
                            break;
                        }

                        args = new MouseHookEventArgs(false, button, click, x, y, scroll, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock);
                    }
                    else
                    {
                        ms.Read(pressedKeys, 0, 3);
                        pressedChar = (char)ms.ReadByte();
                        args        = new KeyHookEventArgs(pressedKeys[0], pressedChar, false, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock);
                    }
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 24
0
        private void OnKeyUp(object sender, KeyHookEventArgs keyEventArgs)
        {
            Debug.WriteLine(string.Format("OnKeyUp(sender: {0}, keyEventArgs: {1})", sender, keyEventArgs));
            if (LogKeyPresses) KeyPresses.Insert(0, keyEventArgs.Key.ToString());

            if (keyEventArgs.Key == VirtualKey.MEDIA_PLAY_PAUSE && _callStatus == TCallStatus.clsRinging)
            {
                foreach (Call activeCall in _skype.ActiveCalls)
                {
                    if (activeCall.Status == TCallStatus.clsRinging)
                    {
                        activeCall.Answer();
                    }
                }
            }

            if (keyEventArgs.Key == VirtualKey.MEDIA_PLAY_PAUSE && _callStatus == TCallStatus.clsInProgress)
            {
                var muted = ((ISkype) _skype).Mute;
                muted = !muted; // Toggle muted state
                ((ISkype)_skype).Mute = muted;
                SetTrayIcon(muted);
                return;
            }

            if (keyEventArgs.Key == VirtualKey.MEDIA_STOP)
            {
                foreach (Call call in _skype.ActiveCalls)
                {
                    call.Finish();
                }
            }
        }