我试图弄清楚如何跟踪用户何时从计算机闲置,这不仅意味着我的应用程序.原因是我希望我的应用程序能够在一定时间后将用户设置为"离开".想像Skype那样在X分钟后将你带走.
任何想法如何实现这一目标?
编辑
我到目前为止跟踪鼠标的方法:
//Init
mouseTimer = new QTimer();
mouseLastPos = QCursor::pos();
mouseIdleSeconds = 0;
//Connect and Start
connect(mouseTimer, SIGNAL(timeout()), this, SLOT(mouseTimerTick()));
mouseTimer->start(1000);
void MainWindow::mouseTimerTick()
{
QPoint point = QCursor::pos();
if(point != mouseLastPos)
mouseIdleSeconds = 0;
else
mouseIdleSeconds++;
mouseLastPos = point;
//Here you could determine whatever to do
//with the total number of idle seconds.
qDebug() << mouseIdleSeconds;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法添加键盘呢?
有特定于平台的方法来获取空闲用户通知.你应该几乎总是使用它们,而不是自己动手.
假设你坚持要自己编写代码.在X11,OS X和Windows上,应用程序根本不会收到任何针对其他应用程序的事件.Qt在监控此类全球事件方面没有提供太多帮助.您需要挂钩相关的全局事件,并过滤它们.这是特定于平台的.
因此,无论您做什么,您都必须编写一些前端API,公开您所使用的功能,并编写一个或多个特定于平台的后端.
首选的特定于平台的空闲时间API包括:
在Windows上,GetLastInputInfo请参阅此答案.
在OS X,NSWorkspaceWillSleepNotification和NSWorkspaceDidWakeNotification,看到这个答案.
在X11上,它是屏幕保护程序API:
/* gcc -o getIdleTime getIdleTime.c -lXss */
#include <X11/extensions/scrnsaver.h>
#include <stdio.h>
int main(void) {
Display *dpy = XOpenDisplay(NULL);
if (!dpy) {
return(1);
}
XScreenSaverInfo *info = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
printf("%u", info->idle);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
最好的办法是检查鼠标和键盘事件。
如果您重写该eventFilter函数并在其中检查:
QEvent::MouseButtonPress
QEvent::MouseButtonRelease
QEvent::Wheel
QEvent::KeyPress
QEvent::KeyRelease
Run Code Online (Sandbox Code Playgroud)
创建一个QTimer,它将在任何 上重置events,如果没有,只需让计时器滴答作响并以您希望的任何时间间隔触发回调。
编辑:
请参阅评论和 Kuba Ober 的答案以获取更多信息。
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |