public MouseListener(GlobalHooker hooker, bool blockLocalInput = false, bool blockRemoteInput = true)
        {
            if (hooker == null)
            {
                throw new ArgumentNullException("invalid mouse hooker");
            }
            h = hooker;
            MouseListener.blockLocalInput  = blockLocalInput;
            MouseListener.blockRemoteInput = blockRemoteInput;

            HookCallbackReference = new HookCallback(HookCallback);
            try
            {
                HookHandle = h.Subscribe(GetHookId(), HookCallbackReference);
            }
            catch (Exception)
            {
                HookCallbackReference = null;
                HookHandle            = IntPtr.Zero;
                throw;
            }
            mouseTypeMovement = Form1.getForm().mouseTypeMovement;
            System.Drawing.Rectangle rect = Form1.getForm().getScreenSize();
            _screenSize.x = rect.Width;
            _screenSize.y = rect.Height;
        }
Exemplo n.º 2
0
        private void updateMouseTypeMovement()
        {
            MouseTypeMovement mouseTypeMovementOld = _mouseTypeMovement;

            if (rdbMouseAbs.Checked)
            {
                _mouseTypeMovement = CommonClasses.Win32.MouseTypeMovement.ABSOLUTE;
            }
            else if (rdbMouseAbsResc.Checked)
            {
                _mouseTypeMovement = CommonClasses.Win32.MouseTypeMovement.ABSOLUTE_RESCALED;
            }
            else if (rdbMouseRel.Checked)
            {
                _mouseTypeMovement = CommonClasses.Win32.MouseTypeMovement.RELATIVE;
            }
            if (_mouseTypeMovement != mouseTypeMovementOld)
            {
                Hooks.MouseListener.setMouseTypeMovement(_mouseTypeMovement);
            }
        }
        /* The specific function that is being used to convert back from normalized coordinates to pixels is equivalent to:
         * x = trunc(dx*PrimaryScreen.Width/65536)
         * y = trunc(dy*PrimaryScreen.Height/65536)
         *
         * So to make sure that the click arrives at the exact coordinates you desire, you have to convert from pixels to normalized like this:
         * dx = ceiling(x*65536/PrimaryScreen.Width)
         * dy = ceiling(y*65536/PrimaryScreen.Height)
         */
        /*
         * http://stackoverflow.com/questions/8021954/sendinput-doesnt-perform-click-mouse-button-unless-i-move-cursor
         * */
        private bool sendMInputToQueue(MSLLHOOKSTRUCT hookStruct, MouseMessage mouseMessage)
        {
            MouseTypeMovement localMouseTypeMovement = mouseTypeMovement;

            if (!previousXYInitialized)
            {
                _previousXY.x         = hookStruct.pt.x;
                _previousXY.y         = hookStruct.pt.y;
                previousXYInitialized = true;
            }
            INPUT input = new INPUT();

            input.type            = (int)InputType.MOUSE;
            input.mkhi.mi.dwFlags = (uint)hookStruct.flags;
            if (localMouseTypeMovement == MouseTypeMovement.ABSOLUTE || localMouseTypeMovement == MouseTypeMovement.ABSOLUTE_RESCALED)
            {
                input.mkhi.mi.dx = hookStruct.pt.x;
                input.mkhi.mi.dy = hookStruct.pt.y;
            }
            else if (localMouseTypeMovement == MouseTypeMovement.RELATIVE)
            {
                input.mkhi.mi.dx = hookStruct.pt.x - _previousXY.x;
                input.mkhi.mi.dy = hookStruct.pt.y - _previousXY.y;
            }
            input.mkhi.mi.mouseData   = hookStruct.mouseData;
            input.mkhi.mi.dwExtraInfo = hookStruct.dwExtraInfo;

            INPUT_EXTENDED input_extended = new INPUT_EXTENDED();

            input_extended.input             = input;
            input_extended.mouseMessage      = mouseMessage;
            input_extended.mouseTypeMovement = localMouseTypeMovement;
            if (localMouseTypeMovement == MouseTypeMovement.ABSOLUTE_RESCALED)
            {
                input_extended.screen_size = _screenSize;
            }

            /*if (mouseMessage != MouseMessage.WM_MOUSEMOVE && ((int)mouseMessage) != 0)
             * {
             *  mouseMessage = mouseMessage;
             * }*/
            //MasterGlobalVars.HookingToSocketQueue.Enqueue(input_extended);

            //Socket.MasterSocket.sendQueuedInput();//TODO use another thread

            if (!blockLocalInput)
            {
                _previousXY.x = hookStruct.pt.x;
                _previousXY.y = hookStruct.pt.y;
            }
            else
            {
                //workaround in order to get deltaXY: the SO inject the new position,but at the end is not accepted..so position remain the same
            }
            if (blockRemoteInput)
            {
                return(true);
            }
            MasterGlobalVars.HookingToSocketQueue.Add(input_extended);

            return(true);
        }
 public static void setMouseTypeMovement(MouseTypeMovement mouseTypeMovement)
 {
     MouseListener.mouseTypeMovement = mouseTypeMovement;
 }