Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. completely restrict mouse movement (including raw data)

completely restrict mouse movement (including raw data)

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialgraphicshelpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 Offline
    3 Offline
    3Dizard
    wrote on last edited by
    #1

    Hi, I want to confine mouse movement. Let's say I want to confine mouse movement to x-direction (so only horizontal movement is possible) when pressing a certain button. This should work globally. What I mean with that is, that I want to use this in any windows program. Let's take MS Paint as an example: When my program runs and I press the certain button, the movement of the mouse should be restricted to horizontal movement. So when drawing a straight horizontal line should be the result. My research so far led me to ClipCursor, but ClipCursor seems to only affect the mousecursor, not the raw data. So returning to the example of MS Paint the following happens: The cursor moves on the horizontal line, but the mouse data received by paint is different: So if you move the mouse slightly in y-direction this still affects the line drawn, also the mouse cursor itself does not indicate that (stays on the straight line). So the result is not a straight line as expected. Does anybody know how to solve that problem ? (as ClipCursor does not confine the raw data sent to an application, it seems to be the wrong routine for doing that) Thanks in advance.

    E C 2 Replies Last reply
    0
    • 3 3Dizard

      Hi, I want to confine mouse movement. Let's say I want to confine mouse movement to x-direction (so only horizontal movement is possible) when pressing a certain button. This should work globally. What I mean with that is, that I want to use this in any windows program. Let's take MS Paint as an example: When my program runs and I press the certain button, the movement of the mouse should be restricted to horizontal movement. So when drawing a straight horizontal line should be the result. My research so far led me to ClipCursor, but ClipCursor seems to only affect the mousecursor, not the raw data. So returning to the example of MS Paint the following happens: The cursor moves on the horizontal line, but the mouse data received by paint is different: So if you move the mouse slightly in y-direction this still affects the line drawn, also the mouse cursor itself does not indicate that (stays on the straight line). So the result is not a straight line as expected. Does anybody know how to solve that problem ? (as ClipCursor does not confine the raw data sent to an application, it seems to be the wrong routine for doing that) Thanks in advance.

      E Offline
      E Offline
      elchupathingy
      wrote on last edited by
      #2

      HHOOK WINAPI SetWindowsHookEx(
      __in int idHook,
      __in HOOKPROC lpfn,
      __in HINSTANCE hMod,
      __in DWORD dwThreadId
      );

      http://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx[^] and then look at: WH_MOUSE or WH_MOUSE_LL to get what you need. WH_MOUSE_LL gives a little bit more information, but may not be needed in your application.

      3 1 Reply Last reply
      0
      • 3 3Dizard

        Hi, I want to confine mouse movement. Let's say I want to confine mouse movement to x-direction (so only horizontal movement is possible) when pressing a certain button. This should work globally. What I mean with that is, that I want to use this in any windows program. Let's take MS Paint as an example: When my program runs and I press the certain button, the movement of the mouse should be restricted to horizontal movement. So when drawing a straight horizontal line should be the result. My research so far led me to ClipCursor, but ClipCursor seems to only affect the mousecursor, not the raw data. So returning to the example of MS Paint the following happens: The cursor moves on the horizontal line, but the mouse data received by paint is different: So if you move the mouse slightly in y-direction this still affects the line drawn, also the mouse cursor itself does not indicate that (stays on the straight line). So the result is not a straight line as expected. Does anybody know how to solve that problem ? (as ClipCursor does not confine the raw data sent to an application, it seems to be the wrong routine for doing that) Thanks in advance.

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        Additionally to what elchupathingy said, you might also try to globally hook[^] GetCursorPos[^].

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

        E 1 Reply Last reply
        0
        • C Code o mat

          Additionally to what elchupathingy said, you might also try to globally hook[^] GetCursorPos[^].

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

          E Offline
          E Offline
          elchupathingy
          wrote on last edited by
          #4

          Selectively injecting a DLL and hooking the functions you need is IMO the better solution. More complicated than using SetWindowHookEx, but will not have the overhead and annoying problems that are associated with global hooks...IE getting the mouse stuck on your horizontal plane, which I think is not what is to happen.

          C 1 Reply Last reply
          0
          • E elchupathingy

            Selectively injecting a DLL and hooking the functions you need is IMO the better solution. More complicated than using SetWindowHookEx, but will not have the overhead and annoying problems that are associated with global hooks...IE getting the mouse stuck on your horizontal plane, which I think is not what is to happen.

            C Offline
            C Offline
            Code o mat
            wrote on last edited by
            #5

            Wouldn't he still need to use a hook to modify the mouse-input messages? I mean, the coords "encoded" in the message params probably come from somewhere else than GetCursorPos, do they not? Wouldn't btw selectively injecting the DLL instead of trying this globally with every process make it less "global"?

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

            E 1 Reply Last reply
            0
            • C Code o mat

              Wouldn't he still need to use a hook to modify the mouse-input messages? I mean, the coords "encoded" in the message params probably come from somewhere else than GetCursorPos, do they not? Wouldn't btw selectively injecting the DLL instead of trying this globally with every process make it less "global"?

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

              E Offline
              E Offline
              elchupathingy
              wrote on last edited by
              #6

              He could hook the actual function it self by using a DLL, messy but could work well and to make this global you can keep an array of processes and check for new ones and inject into the new ones. Or, looking back at what you have said another way to do it would be to use SetWindowHookEx and pass the thread ID of the process that is to be injected into. [^] I just feel that this particular program could cause problems in other programs if it were a global hook, and that it only needs to be applied to specific processes rather than all running processes. From the example of the op with mspaint. Also the call chain for GetCursorPos is: GetCursorPos -> NtUserCallOneParam( DWORD dwParam, DWORD routine ) So, a hook on NtUserCallOneParam could be used to modify the mouse position that is returned.

              1 Reply Last reply
              0
              • E elchupathingy

                HHOOK WINAPI SetWindowsHookEx(
                __in int idHook,
                __in HOOKPROC lpfn,
                __in HINSTANCE hMod,
                __in DWORD dwThreadId
                );

                http://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx[^] and then look at: WH_MOUSE or WH_MOUSE_LL to get what you need. WH_MOUSE_LL gives a little bit more information, but may not be needed in your application.

                3 Offline
                3 Offline
                3Dizard
                wrote on last edited by
                #7

                Thanks for the answer. I could use one more hint. To modify the mouse data with let's say WH_MOUSE_LL I have to use my own custom CallBack function and modify the POINT pt of the MSLLHOOKSTRUCT pointed to by lParam, which has been passed to the CallBack function, right? Or did I get something wrong about how to restrict the movement correctly?

                E 1 Reply Last reply
                0
                • 3 3Dizard

                  Thanks for the answer. I could use one more hint. To modify the mouse data with let's say WH_MOUSE_LL I have to use my own custom CallBack function and modify the POINT pt of the MSLLHOOKSTRUCT pointed to by lParam, which has been passed to the CallBack function, right? Or did I get something wrong about how to restrict the movement correctly?

                  E Offline
                  E Offline
                  elchupathingy
                  wrote on last edited by
                  #8

                  If i remember correctly yes that would be how you would restrict the movement.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups