Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Capturing mouse click outside the window
Forum Updated to NodeBB v4.3 + New Features

Capturing mouse click outside the window

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.2k 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.
  • S Offline
    S Offline
    SamuelYKC
    wrote on last edited by
    #1

    I am trying to capture mouse click event even when the window lost focus. I want to record the coordinates of the points which the user clicked and knowing when the mouse is clicked is essential. I have been googling for the answer yet I couldn't find a working solution. I even tried looking at the MSDN.

    The mousePressEvent() function in Qt only captures mouse clicks that are sent to the window only and so is the eventFilter() function. (Even worse, some how the eventFilter() processes a click for several times.)

    If anyone know how to perform this task, please share the solution with me.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      This is not a trivial thing and Qt will not help you with this.

      You mentioned MSDN so I assume you're only interested in Windows.

      To do this you will need a library(dll) that gets injected into every process.
      In that library you'll need to export a function with "this signature":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644988.aspx
      Once you have your dll you need to load it in your app and pass its handle to the "SetWindowsHookEx":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx method with the WH_MOUSE parameter. Refer to the docs in the first link to see how to parse the arguments of the hook function to get mouse position and buttons.

      There's also an "easy" path, but it will be far less accurate. Create a timer and poll periodically for mouse position and status of the buttons.
      You can get current cursor position with QCursor::pos(). getting mouse buttons requires Windows API call to GetAsyncKeyState with whichever button you want to check eg. VK_LBUTTON. Qt has also QApplication::mouseButtons() method, but it will work only when you click inside your app.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        archqt
        wrote on last edited by
        #3

        Hi,
        Maybe grabmouse() can do the job except on MacOS.
        Sincerely

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SamuelYKC
          wrote on last edited by
          #4

          [quote author="Chris Kawa" date="1416850156"]
          To do this you will need a library(dll) that gets injected into every process.
          In that library you'll need to export a function with "this signature":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644988.aspx
          Once you have your dll you need to load it in your app and pass its handle to the "SetWindowsHookEx":http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx method with the WH_MOUSE parameter. Refer to the docs in the first link to see how to parse the arguments of the hook function to get mouse position and buttons.
          [/quote]

          Thanks for the reply. Well, actually I am kind of a beginner and the documentation could be mind-twisting to me. Based on what I've read and understood, should I start by adding the below codes in the comment?
          @
          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);

          //HOOKPROC hkprcSysMsg;
          //static HINSTANCE hinstDLL; 
          //static HHOOK hhookSysMsg; 
          
          //hinstDLL = LoadLibrary(TEXT("c:\\myapp\\sysmsg.dll")); 
          //hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "SysMessageProc"); 
          
          //hhookSysMsg = SetWindowsHookEx(WH_MOUSE, hkprcSysMsg, hinstDLL, 0); 
          

          }
          @

          The codes are grabbed from their example and I haven't changed the DLL 'cause I don't know what DLL should it be in my case.

          Also, how do I use MouseProc() afterwards?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SamuelYKC
            wrote on last edited by
            #5

            [quote author="archqt" date="1416857650"]Hi,
            Maybe grabmouse() can do the job except on MacOS.
            Sincerely[/quote]

            Thanks for the suggestion. After the grabmouse() is called, the buttons inside the program became unable to receive the click. And for the recording, it made the program capable of recording the click that caused the window to lose focus, but still it wouldn't record clicks after the focus is lost.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved