how to hook to events / messages in windows using python

吃可爱长大的小学妹 提交于 2019-12-02 22:31:58

I've found an ugly workaround: I wrote an AutoIt script which detects the Excel's error MessageBox, closes it, and runs a sysinternals' utility which forces the computer to standby.

Opt("WinWaitDelay",400)
; -- exact text match, to save LOTS of cup cycles!
Opt("WinTitleMatchMode",3)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)
; Opt("WinSearchChildren",1)
dim $title = "Microsoft Excel"
dim $text = "Windows cannot go on standby because Microsoft Office documents or application components are being accessed from the network. You must close the open documents or exit the applications before you can put the computer on standby."
While True
     ; wait for excel's error msg
     WinWait($title, $text)
     Run("psshutdown.exe -c -d -accepteula -m mooshmoosh -t 5")
     ; the annoying msgbox doesn't close without the 'sleep'
     Sleep(1000)
     ; close the annoying modal msgbox!
     WinClose($title)
     ;1 minute delay, save cpu (?)
     Sleep(1*60*1000)
WEnd

(this is an optimized version - the first trials were CPU intensive).
now it sits in the system-tray and just works.

the lost messages question is still open. though i realized it has nothing to do with python in the first place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!