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. Check if mouse button is pressed?

Check if mouse button is pressed?

Scheduled Pinned Locked Moved C / C++ / MFC
htmlcomquestion
9 Posts 4 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.
  • M Offline
    M Offline
    Maarten Kools
    wrote on last edited by
    #1

    Hi. I'm porting an application from Macintosh to Windows, and at some point the function StillDown() is used. I can't seem to find a way to figure out whether the mouse button is pressed on Windows... Is there a function for that?

    D 1 Reply Last reply
    0
    • M Maarten Kools

      Hi. I'm porting an application from Macintosh to Windows, and at some point the function StillDown() is used. I can't seem to find a way to figure out whether the mouse button is pressed on Windows... Is there a function for that?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Check out the WM_LBUTTONDOWN and WM_LBUTTONUP messages.


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      M 1 Reply Last reply
      0
      • D David Crow

        Check out the WM_LBUTTONDOWN and WM_LBUTTONUP messages.


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        M Offline
        M Offline
        Maarten Kools
        wrote on last edited by
        #3

        Yea, that's what I do now. I set up a boolean at mouse down, and mouse up the boolean is set to false. The problem is, however, when the mouse is down some sort of a drag operation is started. The StillDown function on the mac is used in a while loop, I tried to do the same for windows, but of course that ends up being an endless loop...

        R 1 Reply Last reply
        0
        • M Maarten Kools

          Yea, that's what I do now. I set up a boolean at mouse down, and mouse up the boolean is set to false. The problem is, however, when the mouse is down some sort of a drag operation is started. The StillDown function on the mac is used in a while loop, I tried to do the same for windows, but of course that ends up being an endless loop...

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          Capture the mouse on the first WM_LBUTTONDOWN, then handle WM_MOUSEMOVE, checking the wParam. Be sure to release capture on WM_LBUTTONUP or when your app loses focus! WM_MOUSEMOVE fwKeys = wParam; // key flags xPos = LOWORD(lParam); // horizontal position of cursor yPos = HIWORD(lParam); // vertical position of cursor Parameters fwKeys Value of wParam. Indicates whether various virtual keys are down. This parameter can be any combination of the following values: MK_CONTROL Set if the ctrl key is down. MK_LBUTTON Set if the left mouse button is down. MK_MBUTTON Set if the middle mouse button is down. MK_RBUTTON Set if the right mouse button is down. MK_SHIFT Set if the shift key is down. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

          M 1 Reply Last reply
          0
          • R Ravi Bhavnani

            Capture the mouse on the first WM_LBUTTONDOWN, then handle WM_MOUSEMOVE, checking the wParam. Be sure to release capture on WM_LBUTTONUP or when your app loses focus! WM_MOUSEMOVE fwKeys = wParam; // key flags xPos = LOWORD(lParam); // horizontal position of cursor yPos = HIWORD(lParam); // vertical position of cursor Parameters fwKeys Value of wParam. Indicates whether various virtual keys are down. This parameter can be any combination of the following values: MK_CONTROL Set if the ctrl key is down. MK_LBUTTON Set if the left mouse button is down. MK_MBUTTON Set if the middle mouse button is down. MK_RBUTTON Set if the right mouse button is down. MK_SHIFT Set if the shift key is down. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

            M Offline
            M Offline
            Maarten Kools
            wrote on last edited by
            #5

            I found what I was looking for. Apparently, GetAsyncKeyState (found in the keyboard section of MSDN) can also query whether the mouse button is down, which was exactly what I was looking for. Thanks anyway :)

            R J 2 Replies Last reply
            0
            • M Maarten Kools

              I found what I was looking for. Apparently, GetAsyncKeyState (found in the keyboard section of MSDN) can also query whether the mouse button is down, which was exactly what I was looking for. Thanks anyway :)

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #6

              Ah, much easier! :) /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

              1 Reply Last reply
              0
              • M Maarten Kools

                I found what I was looking for. Apparently, GetAsyncKeyState (found in the keyboard section of MSDN) can also query whether the mouse button is down, which was exactly what I was looking for. Thanks anyway :)

                J Offline
                J Offline
                Joe Woodbury
                wrote on last edited by
                #7

                Under several circumstances that will fail. The only reliable way of tracking when the mouse button is released is by capturing in on the down message. One failure point is if you have an overlapping window or the user very quickly drags the mouse to another application. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                M 1 Reply Last reply
                0
                • J Joe Woodbury

                  Under several circumstances that will fail. The only reliable way of tracking when the mouse button is released is by capturing in on the down message. One failure point is if you have an overlapping window or the user very quickly drags the mouse to another application. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                  M Offline
                  M Offline
                  Maarten Kools
                  wrote on last edited by
                  #8

                  Not quite sure what ya mean. It seems to work fine here when I drag the mouse out of the window? And I don't see how overlapping windows could occur?

                  J 1 Reply Last reply
                  0
                  • M Maarten Kools

                    Not quite sure what ya mean. It seems to work fine here when I drag the mouse out of the window? And I don't see how overlapping windows could occur?

                    J Offline
                    J Offline
                    Joe Woodbury
                    wrote on last edited by
                    #9

                    Windows is message driven so using a loop to monitor where the mouse is is self-defeating. You incur a performance penalty for no reason. If you simply need to know when a mouse leaves a window, use TrackMouseEvent with the TME_LEAVE option. If you are monitoring the mouse with the button down, the prescribed method is to use mouse capture so that the mouse message will be sent to that window. As for overlapping windows; this is windows. Unless you have taken over the screen, there are all sorts of overlapping windows present. Your app is guaranteed to receive mouse messages only if you are capturing the mouse with the button down. (What if an other app pops up over the window which you are monitoring?) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                    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