how to hook to events / messages in windows using python

后端 未结 1 1510
名媛妹妹
名媛妹妹 2021-02-03 12:43

in short:

i want to intercept suspend/standby messages on my laptop, but my program doesn\'t receives all relevant messages.

background:

相关标签:
1条回答
  • 2021-02-03 13:21

    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.

    0 讨论(0)
提交回复
热议问题