Lidwatcher v1 By Linear Spoon

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
MrDodel
Posts: 96
Joined: 28 Apr 2021, 09:03
Location: Event Horizon

Lidwatcher v1 By Linear Spoon

Post by MrDodel » 15 Mar 2023, 15:08

Hiya,
I have a script for Lenovo laptops that I have written which enables backlight and selective brightness on the keyboards using PowerShell. I also incorporated Linear Spoons' very nice Lidwatcher script from this thread https://www.autohotkey.com/board/topic/92888-lidwatcher-monitor-the-state-of-your-laptop-lid/

May I ask if someone would be kind enough to convert this over to v2 for me at all, I did some work on converting, but when it comes to .dll calls I'm lost.

If someone is kind enough to convert to v2, would you also add comments to the .dll so I can understand the flow?

**I did try the https://github.com/mmikeww/AHK-v2-script-converter from here, and this is where I hit the wall.

Thank you

v1 Code Below

Code: Select all

;Author: Linear Spoon
;Date: 9/3/2013

/*
 This script should work for Vista+

 Details:
 Use Start_LidWatcher to begin monitoring the lid state.
 r := Start_LidWatcher()

 When the state changes, the function "LidStateChange" will be called.
 LidStateChange should have the following format:
 LidStateChange(newstate)
 {
   ...
 }

 newstate will be one of two values:
 "opened" = The lid was opened
 "closed" = The lid was closed

 Note: It's possible that this function will be immediately called after 
       using Start_LidWatcher with the lid's current state (usually open).

 When the application no longer needs to monitor the lid state,
 call Stop_LidWatcher with the return value from Start_LidWatcher.
 Pass it the return value from Start_LidWatcher.
 Stop_LidWatcher(r)

*/

Start_LidWatcher()
{
  VarSetCapacity(guid, 16)
  ;GUID_LIDSWITCH_STATE_CHANGE
  Numput(0xBA3E0F4D, guid, 0, "uint"), Numput(0x4094B817, guid, 4, "uint")
  Numput(0x63D5D1A2, guid, 8, "uint"), Numput(0xF3A0E679, guid, 12, "uint")

  r := DllCall("RegisterPowerSettingNotification", "ptr", A_ScriptHwnd, "ptr", &guid, "uint", 0)
  if (!r || ErrorLevel)
  {
    ;MSDN says check GetLastError if the return value was NULL.
    Msgbox % "RegisterPowerSettingNotification failed with error: " (ErrorLevel ? ErrorLevel : A_LastError)
    return 0
  }
  OnMessage(0x218, "WM_POWERBROADCAST")
  return r
}

Stop_LidWatcher(r)
{
  DllCall("UnregisterPowerSettingNotification", "ptr", r)
  OnMessage(0x218, "")
}

WM_POWERBROADCAST(wparam, lparam)
{
  static fx := "LidStateChange"
  if (wparam = 0x8013 && isFunc(fx))
  {
    ;data = 0 = closed
    ;data = 1 = opened
    %fx%(Numget(lparam+20, 0, "uchar") ? "opened" : "closed")
  }
  return 1
}
So much universe, and so little time. GNU Sir Terry.

Return to “Ask for Help (v2)”