locked
Limit the mouse position if key Shift is pressed RRS feed

  • Question

  • Hi all!

    I'd like to develop a function which limit the mouse position to the active form if the Shift key is pressed but I don't know how to start...

    I've thought about using mouse and keyboard hooking, I know, more or less, how to use them separately but I'd have to use them combinated because I think I have to capture mouse movement only if key shift is pressed. Am I on the right way? Is this possible? Someone could explain me how to start?

    Thanks in advance! Smile
    Thursday, April 17, 2008 3:51 PM

Answers

  • Hi all!

    At the end I've solved the question and I've decided to post it here because it could help someone.
    The problem I had with the code above came with System.Windows.Rect because ClipCursor didn't wait that, it waited a RECT (other struct) object. So, the solution is:

    private void MyKeyDown(object sender, KeyEventArgs e)
            {
                if(Magnificador.Instance.Raton.RestricVentActiva)
                {
                   bool isDownShift = ((NativeMethods.GetKeyState(NativeMethods.VK_SHIFT) & 0x80) == 0x80 ? true : false);
                   if(isDownShift)
                        LimitarRatonFormularioActivo();          
                }
            }

     private static void LimitarRatonFormularioActivo()
            {
                NativeMethods.WINDOWINFO info = new NativeMethods.WINDOWINFO();           
                NativeMethods.GetWindowInfo(NativeMethods.GetForegroundWindow(), ref info);
               
                int posX = info.rcWindow.Left;
                int posY = info.rcWindow.Top;
                int ancho = info.rcWindow.Right - info.rcWindow.Left;
                int alto = info.rcWindow.Bottom - info.rcWindow.Top;
               
                Cursor.Clip = new Rectangle(posX, posY, ancho, alto);
            }

    Thanks for all! Smile


    Saturday, April 19, 2008 9:09 AM

All replies

  • Thanks for your help Smile but my real problem is how to detect the events mouse movement and shift pressed at the same time Sad to raise the set cursor position.


    Thursday, April 17, 2008 4:21 PM
  •  

    I assume you'd need to detect this even if the mouse cursor was outside of the control / form, and the control / form didn't have focus?

     

    If not, take a look at MouseMove, KeyDown and KeyUp events.

     

    otherwise, hooks may be the way to go.

    Thursday, April 17, 2008 4:25 PM
  • I've been looking for information and I've found Cursor.Clip; with that I could set the cursor inside a rect when a key is pressed (for that i use a Keyboard Hook) but I need get the handle of the active form in the system; I've developed my app without focus catching so, I need the window with it, is this possible?? I've found the function GetForegroundWindow but ti doesn't act as I wait.

    Thanks for all again (and sorry for my english)
    Thursday, April 17, 2008 4:41 PM
  • Ya know, if you use form.ShowDialog() to display a form instead of form.Show(), the user can move the mouse anywhere.  But, only the active form will respond to the mouse until that form is closed.

     

    Rudedog

     

    Friday, April 18, 2008 3:09 PM
  • I'm trying to develop a screen magnifier to help people with any kind of visual disabilities, then one of the charecteristics was let the user check a control which active a mouse control, whose function was limit the mouse movement to the active window.

    Now I've gotten hook the shift key (with a global keyboard hook) and catch the handle of the active window with GetForegroundWindow(), with that I use the function GetWindowRect(System.IntPtr hWnd, out RECT lpRect) which returns me a rect to use with ClipCursor(ref RECT lpRect);

    I thought that was what i had to do, more or less, but it doesn't work; when I inspect the lpRect object it's said that its size is zero!! Tongue Tied But I think this is a good way... what could be wrong?¿

    I hope i had explain me better than in the other threads, sorry for all, and many many thanks Smile

    Friday, April 18, 2008 3:36 PM
  • Code Snippet

    private void Form_Enter(object sender, EventArgs e)

    {

    this.Focus();

    }

    private void Form_Leave(object sender, EventArgs e)

    {

    this.Focus();

    }

     

     

    Try these events.

    There's also MouseEnter and MouseLeave, but these should take into account someone pressing CTRL-Tab.

     

    Rudedog

    Friday, April 18, 2008 3:51 PM
  • Sorry.  Events fire but mouse still moves off the form.

     

    Friday, April 18, 2008 4:01 PM
  • The code that I've added to the key event pressed is:

    private void KeyDowned(object sender, KeyEventArgs e)
              {
                SystemWindow sw = new SystemWindow(NativeMethods.GetForegroundWindow());
                System.Windows.Rect rect = new System.Windows.Rect();

                NativeMethods.GetWindowRect(sw.HWnd, out rect);
                NativeMethods.ClipCursor(rect);
    }


    But it doesn't work; though the handle to the Window is correct, the size of the rect is 0.

    But those events affect only to my app?? The hped behaviour is that mouse movement could be limited to the active window while the user press the Shift key, my app doesn't get the focus, it's transparent, so this behaviour has to affect to the other desktop aplications.
    Friday, April 18, 2008 4:08 PM
  • Hi all!

    At the end I've solved the question and I've decided to post it here because it could help someone.
    The problem I had with the code above came with System.Windows.Rect because ClipCursor didn't wait that, it waited a RECT (other struct) object. So, the solution is:

    private void MyKeyDown(object sender, KeyEventArgs e)
            {
                if(Magnificador.Instance.Raton.RestricVentActiva)
                {
                   bool isDownShift = ((NativeMethods.GetKeyState(NativeMethods.VK_SHIFT) & 0x80) == 0x80 ? true : false);
                   if(isDownShift)
                        LimitarRatonFormularioActivo();          
                }
            }

     private static void LimitarRatonFormularioActivo()
            {
                NativeMethods.WINDOWINFO info = new NativeMethods.WINDOWINFO();           
                NativeMethods.GetWindowInfo(NativeMethods.GetForegroundWindow(), ref info);
               
                int posX = info.rcWindow.Left;
                int posY = info.rcWindow.Top;
                int ancho = info.rcWindow.Right - info.rcWindow.Left;
                int alto = info.rcWindow.Bottom - info.rcWindow.Top;
               
                Cursor.Clip = new Rectangle(posX, posY, ancho, alto);
            }

    Thanks for all! Smile


    Saturday, April 19, 2008 9:09 AM