Advertisement
Guest User

LidClose.au3

a guest
Jul 27th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <Timers.au3>
  2. #include <MsgBoxConstants.au3>
  3.  
  4. Const $idletime = 900000
  5.  
  6. Global Const $WM_POWERBROADCAST = 0x0218
  7. ; GUID_LIDSWITCH_STATE_CHANGE GUID
  8. ; 0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3
  9. Global Const $GUID_LIDSWITCH_STATE_CHANGE = DllStructCreate("byte[16]")
  10. DllStructSetData($GUID_LIDSWITCH_STATE_CHANGE, 1, Binary("0x4D0F3EBA17B89440A2D1D56379E6A0F3"))
  11.  
  12. ; create gui and register notification
  13. Global $GUI = GUICreate("")
  14. GUIRegisterMsg($WM_POWERBROADCAST, "__WM_POWER")
  15. Global $hPower = DllCall("user32.dll", "handle", "RegisterPowerSettingNotification", "handle", $GUI, "ptr", DllStructGetPtr($GUID_LIDSWITCH_STATE_CHANGE), "dword", 0)
  16. GUISetState()
  17. _GuiMinimizeToTray($GUI)
  18.  
  19. HotKeySet ( "^!L", "_Exit" )
  20.  
  21. Do
  22. ; Mouse/Keyboard action during this delay will change reported idle time
  23. Sleep(300000)
  24.  
  25. If _Timer_GetIdleTime() > $idletime Then
  26. Beep();
  27. MsgBox($MB_TOPMOST, "Going sleep in 3 seconds ...", "Any activity to cancel", 3)
  28. If _Timer_GetIdleTime() > $idletime Then
  29. Run ( "schtasks.exe /run /tn Sleep")
  30. EndIf
  31. EndIf
  32.  
  33. Until GUIGetMsg() = -3
  34.  
  35. ; unregister notification
  36. DllCall("user32.dll", "bool", "UnregisterPowerSettingNotification", "handle", $hPower[0])
  37.  
  38. Func __WM_POWER($hwnd, $msg, $wparam, $lparam)
  39. #forceref $hwnd
  40. Local Const $PBT_POWERSETTINGCHANGE = 0x8013
  41. ; Data member is variable depending on notification
  42. ; the size of the Data member is returned in DataLength
  43. Local Const $POWERBROADCAST_SETTING = "byte[16];dword DataLength;byte Data"
  44.  
  45. Switch $msg
  46. Case $WM_POWERBROADCAST
  47. Switch $wparam
  48. Case $PBT_POWERSETTINGCHANGE
  49. Local $PBT_PSC = DllStructCreate($POWERBROADCAST_SETTING, $lparam)
  50. ; GUID of registered notification is the first member of the structure
  51. Switch DllStructGetData($PBT_PSC, 1)
  52. Case DllStructGetData($GUID_LIDSWITCH_STATE_CHANGE, 1)
  53. ; for this notification, Data is an int representing the lid state
  54. Local $data = DllStructCreate("int", DllStructGetPtr($PBT_PSC, "Data"))
  55. _LidStateChange(DllStructGetData($data, 1))
  56. ;
  57. Return 1
  58. EndSwitch
  59. EndSwitch
  60. EndSwitch
  61.  
  62. Return "GUI_RUNDEFMSG"
  63. EndFunc
  64.  
  65. Func _LidStateChange($iState)
  66. Static $iPrevious = -1
  67. If $iPrevious = -1 Then
  68. ; first fire, current state
  69. $iPrevious = $iState
  70. Return
  71. ElseIf $iPrevious = $iState Then
  72. Return
  73. Else
  74. $iPrevious = $iState
  75. EndIf
  76. ; lid state:
  77. ; 0 -> closed
  78. ; 1 -> open
  79. Switch $iState
  80. Case 0
  81. ;ConsoleWrite("lid closed" & @CRLF)
  82. ;DllCall("user32.dll", "bool", "LockWorkStation")
  83. run ( "schtasks.exe /run /tn Sleep")
  84. Case 1
  85. ;ConsoleWrite("lid opened" & @CRLF)
  86. EndSwitch
  87. EndFunc
  88.  
  89. Func _GuiMinimizeToTray($h_wnd)
  90.  
  91. ; check if 1 = Window exists, then check to see if its 16 = Window is minimized
  92. If BitAND(WinGetState($h_wnd), 1) Or Not BitAND(WinGetState($h_wnd), 16) Then
  93. ; change the window state to hide
  94. WinSetState($h_wnd, "", @SW_HIDE)
  95. EndIf
  96.  
  97. EndFunc ;==>_GuiMinimizeToTray
  98.  
  99. Func _Exit()
  100. Exit 0
  101. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement