我使用的是Ubuntu 12.04。有没有办法在Python中捕捉暂停事件,即如果笔记本电脑要暂停,请执行此操作...?捕捉关机事件的同一个问题。如何在Python中捕获系统暂停事件?
回答
我认为最简单的方法是使用DBUS python接口 和听“AboutToSleep”和/或“org.freedesktop.UPower”接口“睡觉”事件
您可以扩展此代码,它监听来自acpid的事件,试着打印它接收到的字符串并生成你想要的事件,看看这个字符串是什么样的。
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/var/run/acpid.socket")
print "Connected to acpid"
while 1:
for event in s.recv(4096).split('\n'):
event=event.split(' ')
if len(event)<2: continue
print event
if event[0]=='ac_adapter':
if event[3]=='00000001': #plugged
plugged() #Power plugged event
else: #unplugged
unplugged() #Power unplugged event
elif event[0]=='button/power':
power_button() #Power button pressed
elif event[0]=='button/lid':
if event[2]=='open':
lid_open() #Laptop lid opened
elif event[2]=='close':
lid_close() #Laptop lid closed
谢谢。它正在寻找什么。顺便说一句,你在哪里可以得到这些代码是什么意思?例如000000081? – koogee
哼不知道说实话,我通过生成的事件,看到了字符串是什么样子,你可以尝试的acpid的文件,这是守护程序发送这些字符串中看到了这些。 – LtWorf
这很奇怪。看起来我在暂停/恢复后只能获得盖子打开/关闭信号中的一个。 [ '处理器', 'CPU0', '00000081', '00000000'] ** [ '按钮/盖', 'LID0', '00000080', '00000003'] ** [ '处理器' ['battery','BAT0','00000080','0000000'] ['ac_adapter','ADP0','00000080','00000000'] ['battery' ] [“处理器”,“CPU0”,“00000081”,“00000000”] – koogee
如果有一个人对同样的问题绊倒,下面的代码:
#!/usr/bin/env python
import dbus # for dbus communication (obviously)
import gobject # main loop
from dbus.mainloop.glib import DBusGMainLoop # integration into the main loop
def handle_resume_callback():
print "System just resumed from hibernate or suspend"
def handle_suspend_callback():
print "System about to hibernate or suspend"
DBusGMainLoop(set_as_default=True) # integrate into main loob
bus = dbus.SystemBus() # connect to dbus system wide
bus.add_signal_receiver( # defince the signal to listen to
handle_resume_callback, # name of callback function
'Resuming', # singal name
'org.freedesktop.UPower', # interface
'org.freedesktop.UPower' # bus name
)
bus.add_signal_receiver( # defince the signal to listen to
handle_suspend_callback, # name of callback function
'Sleeping', # singal name
'org.freedesktop.UPower', # interface
'org.freedesktop.UPower' # bus name
)
loop = gobject.MainLoop() # define mainloop
loop.run() # run main loop
- 1. 使用python捕获系统事件
- 2. Joomla - 如何捕获系统事件
- 3. 捕获系统事件
- 4. 在活动中捕获系统事件
- 5. 如何在minko中捕获和处理系统事件?
- 6. 系统在事件循环中暂停以进行大循环
- 7. AIR中是否存在系统暂停事件?
- 8. unittest中的系统(“暂停”)
- 9. 如何在jQuery中暂停事件?
- 10. 如何在JavaScript中暂停事件?
- 11. 如何撤消系统(“暂停”)
- 12. 系统(“暂停”)澄清
- 13. C#电梯系统暂停
- 14. 如何捕获Windows操作系统事件? (C#)
- 15. 在Android系统中可靠地暂停媒体播放系统
- 16. jquery键盘暂停捕获
- 17. 未捕获抛出:暂停
- 18. 捕获系统默认编辑器关闭事件在Java中
- 19. 如何在java中进行系统暂停进行调试?
- 20. FileSystemWatcher停止捕获事件
- 21. 如何暂停在python
- 22. 在cron脚本暂停由于系统
- 23. 暂停播放记录在android系统
- 24. 如何在jQuery Mobile carousel中捕获暂停时间?
- 25. 如何在win7上捕获文件打开事件操作系统
- 26. 暂停python并等待Matplotlib事件
- 27. 在Clojure捕获系统/出
- 28. 在Python中捕获关机事件
- 29. 如何捕捉系统中出现的事件(android)?
- 30. 如何暂停故事板?
这本书以醒目注销事件。但可以帮助你:http://stackoverflow.com/questions/2490166/detect-user-logout-shutdown-in-python-gtk-under-linux-sigterm-hup-not-rece – theAlse
谢谢...我会看看gnome.ui – koogee