Qt检测计算机何时进入睡眠? [英] Qt detect when computer goes into sleep?
问题描述
如何检测用户计算机何时进入睡眠状态(笔记本电脑的盖子关闭,休眠模式由于不活动等)?
How can i detect when a users computer goes into sleep (laptop lid closes, sleep mode due to inactivity, etc)?
我需要这样做才能断开连接用户TCP连接。基本上,我们有一个简单的聊天应用程序,我们想让用户离线。
I need to do this to disconnect the users TCP connection. Basically we got a simple chat application where we want to take the user off-line.
推荐答案
没有Qt方式检测当计算机进入睡眠或休眠时。
There is no Qt way to detect when computer goes to sleep or hibernation. But there are some platform dependent ways to do it.
在Windows上,您可以在WindowProc处理程序中侦听WM_POWERBROADCAST消息:
On Windows you can listen for the WM_POWERBROADCAST message in your WindowProc handler:
LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (WM_POWERBROADCAST == message && PBT_APMSUSPEND == wParam) {
// Going to sleep
}
}
在linux您可以将以下shell脚本放在/etc/pm/sleep.d中,该脚本使用参数执行程序。您可以通过某种方式启动程序并通知主应用程序:
On linux you can put the following shell script in /etc/pm/sleep.d which executes a program with arguments. You can start a program and notify your main application in some way:
#!/bin/bash
case $1 in
suspend)
#suspending to RAM
/Path/to/Program/executable Sleeping
;;
resume)
#resume from suspend
sleep 3
/Path/to/Program/executable Woken
;;
esac
对于OS X,您可以看到此。
For OS X you can see this.
这篇关于Qt检测计算机何时进入睡眠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!