Results 1 to 7 of 7

Thread: How to know if somebody is using the PC?

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to know if somebody is using the PC?

    Hello!

    I want to create a function in my software that, before doing something, verifies if there is somebody using the PC. I imagine that the best way to do that is implement an algorithm that verifies if the keyboard or the mouse has been used in the past X seconds (I guess 10 seconds is already enough). If not, than it moves on, but if yes, than it waits. [as an analogy, the idea is to implement the same thing Windows has to verifiy if it should start the screen protection or diactivate graphics).

    I guess:
    What I would have to do is to connect the MouseMove Event and some sort of Keyboard Press Event to a variable that stores the current time when the event happens. When the time comes, this variable will be read and if there is X or more time elapsed since the last mouse or keyboard was touched, the software goes on.

    The problem is that this MouseMove catcher and keyboard press catcher must be on even when the software is not focused (i.e. when it's not been selected, viewed, used, etc., i.e. runnning in background). Now how can I do that?


    So, summarizing:
    * How to make mouse move events and keyboard press events be captured even when the software runs in background?
    * Which are the methods that should be used to capture the Keyboard inputs?


    Thanks,


    Momergil

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to know if somebody is using the PC?

    Did you search the forum before asking this question? It has been asked and answered a number of times already.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to know if somebody is using the PC?

    This has nothing to do with Qt.
    Here is a sample project which is doing hooking.
    http://www.codeproject.com/Articles/...ard-Hooks-in-C

    And of course this is highly NOT portable!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know if somebody is using the PC?

    Did you search the forum before asking this question? It has been asked and answered a number of times already.
    Well wysota, I came to figure out that direct research on the forum is normally helpless, so normally I don't do the research anymore, but when I type the title to the new thread, the forum automatically selects 3 probable related posts, and than I look them. But this time, nothing appeared, so I move on to write and publish the thread.

    This has nothing to do with Qt.
    Well, I guess that since I was developing the software in Qt and wanted to use Qt functions (such as QWidget::mouseMoveEvent), my doubt would have something to do with Qt... (even if, after all, in order to capture the events while the software is running in background, I would have to use some Windows functions).

    Here is a sample project which is doing hooking.
    Hmm, I'm not sure it will work, since it's in C#. And I gave a look to the "Browse Code", and it looks that this is not the sort of solution that is aplicable.

    Thanks for it, anyway!


    Added after 1 9 minutes:


    Well, as I sad; I did a research in the forum and found nothing practically useful.

    Anyway, I was able to do what I wanted, though not as I expected. The only thing usefull I found in the research was a thread where wysota, I think, explained that by using Qt functions to catch a mouse or keyboard event I would be able to figure out changins in other softwares, because of the specific relathionship between the OS and the application in how on passes a mouse or keyboard event to the other. So that convinced me that I would find what I wanted using only windows functions.

    So here is the resultant code:

    Qt Code:
    1. //.h
    2. POINT pwx;
    3. unsigned int usage_count;
    4. bool flag_reset;
    5.  
    6. //constructor
    7. usage_count = 0;
    8. flag_reset = false;
    9. GetCursorPos(&pwx);
    10.  
    11. mouseMovement = new QTimer(this);
    12. connect(mouseMovement,SIGNAL(timeout()),this,SLOT(onMouseMovement()));
    13. mouseMovement->start(1000); //Um seg
    14.  
    15. //function conected to QTimer
    16. void MainWindow::onMouseMovement()
    17. {
    18. POINT pwx2;
    19. GetCursorPos(&pwx2);
    20.  
    21. if (pwx.x != pwx2.x || pwx.y != pwx2.y)
    22. {
    23. pwx.x = pwx2.x;
    24. pwx.y = pwx2.y;
    25. usage_count = 0;
    26. flag_reset = false;
    27. }
    28. else
    29. {
    30. usage_count++;
    31.  
    32. if (usage_count >= 10)
    33. flag_reset = true; //10 seconds passed without any mouse event happening. Can move on.
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    Now there is the keyboard lacking.
    Last edited by Momergil; 4th July 2012 at 18:24.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to know if somebody is using the PC?

    Quote Originally Posted by Momergil View Post
    Well wysota, I came to figure out that direct research on the forum is normally helpless, so normally I don't do the research anymore
    Then maybe you should do it now.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to know if somebody is using the PC?

    Hmm, I'm not sure it will work, since it's in C#. And I gave a look to the "Browse Code", and it looks that this is not the sort of solution that is aplicable.
    What does the language it was written in has to do with it?
    The windows API used is the same, the windows calls are the same, and what you have to do is the same.
    Besides, there are examples in C/C++ too, now that you know exactly what you are looking for (hooking) you could have googled for such an example.
    Polling the mouse for its global position, is , how shell I say, not very elegant - but wysota knows it I am sure, and he probably offered it because someone didn't want/could not use system specific code - in that regard I think its a nice idea.
    Well, I guess that since I was developing the software in Qt and wanted to use Qt functions (such as QWidget::mouseMoveEvent), my doubt would have something to do with Qt... (even if, after all, in order to capture the events while the software is running in background, I would have to use some Windows functions).
    You can use system specific calls in any Qt application - since what you want to do is system specific you are breaking the portability Qt offers you anyway.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to know if somebody is using the PC?

    I don't think I ever offered polling global mouse position to anyone. That's a stupid approach, so even if I did, one should ignore that advice.

    The proper way on Windows is to use hooks and handle them through QCoreApplication::winEventFilter().

    By the way:

    when I type the title to the new thread, the forum automatically selects 3 probable related posts, and than I look them. But this time, nothing appeared, so I move on to write and publish the thread.
    The hint you get is only as good as the thread title you write. When I first saw the title "How to know if somebody is using the PC", what came to my mind was "Check the amount of dust on the computer case and monitor". With a title like that no wonder that you didn't get anything useful.
    Last edited by wysota; 5th July 2012 at 09:21.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.