我的cocoa应用程序运行后台任务,我想在用户空闲时停止(没有键盘/鼠标输入),然后在用户再次激活时恢复.有没有办法注册空闲状态通知?
我试图弄清楚如何跟踪用户何时从计算机闲置,这不仅意味着我的应用程序.原因是我希望我的应用程序能够在一定时间后将用户设置为"离开".想像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)
有没有办法添加键盘呢?