PDA

View Full Version : How to know if somebody is using the PC?



Momergil
4th July 2012, 16:05
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

wysota
4th July 2012, 16:08
Did you search the forum before asking this question? It has been asked and answered a number of times already.

high_flyer
4th July 2012, 16:16
This has nothing to do with Qt.
Here is a sample project which is doing hooking.
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

And of course this is highly NOT portable!

Momergil
4th July 2012, 18:24
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:


//.h
POINT pwx;
unsigned int usage_count;
bool flag_reset;

//constructor
usage_count = 0;
flag_reset = false;
GetCursorPos(&pwx);

mouseMovement = new QTimer(this);
connect(mouseMovement,SIGNAL(timeout()),this,SLOT( onMouseMovement()));
mouseMovement->start(1000); //Um seg

//function conected to QTimer
void MainWindow::onMouseMovement()
{
POINT pwx2;
GetCursorPos(&pwx2);

if (pwx.x != pwx2.x || pwx.y != pwx2.y)
{
pwx.x = pwx2.x;
pwx.y = pwx2.y;
usage_count = 0;
flag_reset = false;
}
else
{
usage_count++;

if (usage_count >= 10)
flag_reset = true; //10 seconds passed without any mouse event happening. Can move on.
}
}


Now there is the keyboard lacking.

wysota
4th July 2012, 18:32
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.

high_flyer
5th July 2012, 09:06
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.

wysota
5th July 2012, 09:15
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.