Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

LidWatcher - Monitor the state of your laptop lid


  • Please log in to reply
11 replies to this topic
Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

A few users have encouraged me to post my first script in this section..

 

Purpose: Detects when your laptop lid is opened or closed, and calls a function for you to handle it.

Requirements: Vista+

 

Get it from my repository:

Download: LidWatcher.ahk

Example: LidWatcher_Test.ahk

 

Or from here:

LidWatcher.ahk

Spoiler

 

LidWatcher_Test.ahk

Spoiler

 

Other stuff:

-Report bugs in this thread.

-Suggestions are welcome, even if not for this script.

-You can change the function that is called by modifying this line (in LidWatcher.ahk):

static fx := "LidStateChange"  ;Replace LidStateChange with the quoted name of the function to call

Join us at the new forum - http://www.ahkscript.org/

 


Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012

Thank you! happy.png


Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.


dufferZafar
  • Members
  • 19 posts
  • Last active: Jan 04 2014 05:40 PM
  • Joined: 30 Oct 2012

Nice wink.png



ggleb
  • Members
  • 1 posts
  • Last active: Jun 14 2014 07:49 PM
  • Joined: 14 Jun 2014

Hi, can anybody translate this code to Visual Basic ?

Thank you !

 



Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

Sorry for the late reply. I don't know VB well enough to translate this on my own.

I found this post that looks to be the same algorithm in VB: http://www.experts-e...Q_27459058.html

 

Edit: Link seems broken, I'm not sure where to find it now


Join us at the new forum - http://www.ahkscript.org/

 


aiiro
  • Members
  • 8 posts
  • Last active: Nov 16 2014 09:38 AM
  • Joined: 21 Apr 2014

Thanks for this, it works! I haven't found anything useful besides this. I need this for my laptop, which doesn't turn off monitor on lid close, I need to modify it so that on close it turns off monitor and on open lid it turns monitor back on and that means I need different actions on close and open. However... My AHK scripting level is basically zero as it took me a week to write a script for detecting a window and entering some credentals after it had loaded the page. So my initial idea was to detect the correct action from the log file, but I know this is not the correct way to do this. Could anyone please point me to the right direction regarding the needs?

 

Thanks in advance!



Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

I'm very surprised your monitor didn't shut off automatically when you close the lid. That's sort of a standard power-saving procedure. You shouldn't need an AHK script if this is all you want to do though. Somewhere in your control panel power settings you can set what action closing the lid takes, and turning off the monitor is one. 

 

Failing that, this script might do what you want. My laptop failed a few weeks back, so I can't actually test if this works, sorry. 

#Include LidWatcher.ahk
r := Start_LidWatcher()  ;Start waiting for the lid to be opened or closed

OnExit, cleanup
return

cleanup:
  Stop_LidWatcher(r)  ;Stop watching the lid state changes
  ExitApp
return


; This function will be called whenever the lid changes states.
; newstate will be either "opened" or "closed", hopefully self-explanatory.
LidStateChange(newstate)
{
  if (newstate = "opened")
    SendMessage, 0x112, 0xF170, -1,, Program Manager  ;-1 = turn monitor on
  else if (newstate = "closed")
    SendMessage, 0x112, 0xF170, 2,, Program Manager  ;2 = turn monitor off
}


Join us at the new forum - http://www.ahkscript.org/

 


aiiro
  • Members
  • 8 posts
  • Last active: Nov 16 2014 09:38 AM
  • Joined: 21 Apr 2014

I am sorry I haven't had time to test this before, but I did now and thank You very much for this!!! It works perfectly.

 

Of course I shouldn't have to deal with stuff like this, but I have Dell E6540 which is supposed to be high-end business laptop and it doesn't even have volume indicator by default! I had to solve this with AHK also, now I can at least see the volume level when I press the dedicated volume buttons. It would be better if it would open the default Windows volume slider but at least it's better than nothing.

 

Anyway, regarding this, since at first of course I tried to find settings for it but I only get choices like - Do nothing / Sleep / Hibernate / Shut down - when I access the appropriate Control Panel applet "Define power buttons and turn on password protection" (which in itself is contradictory to the content it displays but with MS UI I don't wonder).

 

So that was the reason I started researching this and now I have the solution (thanks again!). I loaded the scripts now and tested and since I am using a dock at work with an external monitor, at first it seemed that it doesn't work in that configuration but it also seems that Windows likes to duplicate displays by default. So I found the right place in Control Panel again, "Change the appearance of Your displays" - which by the way opens under "Adjust resolution" link - and chose "Show desktop only on 2". Well, as You can probably tell I don't like W7 Control Panel since it's messy and confusing and sometimes rediculous etc.

 

Voila, now with the scripts everything works as it's supposed to in the first place! Meaning when I close the lid off dock, the display goes off. When I close the lid in dock, both displays go off but external comes back after a second or two (if the display is duplicated like by default it is, the laptop display comes back too). I can't comprehend why the manufacturers of hardware / software fail to address these elementary issues...

 

So thank You again and I hope others who have problems with this will find this thread too!

 

 

 



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
great script
is there something possible with LockWorkStation (Win+L) and Login back?
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

aiiro
  • Members
  • 8 posts
  • Last active: Nov 16 2014 09:38 AM
  • Joined: 21 Apr 2014

I wanted to take that one, but I can't make it work :)

LidStateChange(newstate)
{
  if (newstate = "closed")
  Send {LWin Down}l{LWin Up}
}

For some reason the script simply ignores the LWin commands. I think my syntax is legal. Beats me...

 

I am also not sure I understand what You mean by login back - after locking there is only one option and that's it :)



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
more like this:
 
; GLOBAL SETTINGS ===============================================================================================================

#Warn
#NoEnv
#SingleInstance Force

; SCRIPT ========================================================================================================================

tmp := Start_SESSION_CHANGE()
OnExit, EOF
return

; FUNCTIONS =====================================================================================================================

Start_SESSION_CHANGE()
{
    ret := DllCall("wtsapi32.dll\WTSRegisterSessionNotification", "Ptr", A_ScriptHwnd, "UInt", 1)

    if (!ret || ErrorLevel)
    {
        MsgBox % "WTSRegisterSessionNotification failed with error: " (ErrorLevel ? ErrorLevel : A_LastError)
        return 0
    }
    OnMessage(0x02B1, "WM_WTSSESSION_CHANGE")
    return ret
}

Stop_SESSION_CHANGE(hWnd)
{
    DllCall("wtsapi32.dll\WTSUnRegisterSessionNotification", "Ptr", hWnd)
    OnMessage(0x02B1, "")
}
 
WM_WTSSESSION_CHANGE(wparam, lparam)
{
    if (wparam = 0x7)
        on_lock()
    else if (wparam = 0x8)
        on_unlock()
}

on_lock()
{
    FileAppend, %A_Now% - Win Lock`n, test.log
}
 
on_unlock()
{
    FileAppend, %A_Now% - Win UnLock`n, test.log
}

; EXIT ==========================================================================================================================

EOF:
    Stop_SESSION_CHANGE(tmp)
    ExitApp
return
Win+L (Lock Windows) => call func on_lock()
Login (Unlock Windows) => call func on_unlock()
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

aiiro
  • Members
  • 8 posts
  • Last active: Nov 16 2014 09:38 AM
  • Joined: 21 Apr 2014

Okay... You meant auto unlocking. I didn't know that was even possible :)

 

But what is it good for? I thought there would be point if one could lock the machine after closing lid and the whole point of locking is that You have to enter password to unlock...

 

Anyway... it seems You have far more experience for scripting :)