Internet Disconnect Alert

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Huh-Say What
Posts: 25
Joined: 24 Apr 2023, 20:44

Internet Disconnect Alert

27 Apr 2023, 19:05

Hello! Can someone please fix the ending of my script? It works great letting me know if I have lost internet connection and also informs when it is reconnected.
When it disconnects, I currently have a Gui popup flashing until the connection is restored. My issue is I don't know how to make this script give the connection restored popup only once, and it keeps flashing like the disconnected message does.
Please help!!

Code: Select all

global Connected := TRUE
SetTimer CheckInternet, 1000
Return
CheckInternet()
{
    If (!IsConnected())
    {
Soundbeep, 800, 100 	 ; Cuckoo Alert
Soundbeep, 600, 100
Gui, +AlwaysOnTop -Caption +hwndHGUI +LastFound
Gui, Color, 123456
Gui, Font, s14 w800 cRED q5, Calibri
Gui, Margin, 15, 15
Gui, Add, Text, w333 h50 Center +0x0200 0x00800000, ATTENTION: Internet Has Disconnected!! 
Gui, Font, s12 w400 cYELLOW q5, Calibri
Gui, Add, Text, x67 y+5, Continuing Connection Monitoring... 
Gui, Font, s12 w400 cABCDEF q5, Calibri
Gui, Add, Text, x33 y+5, Will Notify Once Connection Is Reestablished.
Gui, Show, Hide
WinGetPos, X, Y, W, H
R := Min(W, H) // 5 
WinSet, Region, 0-0 W%W% H%H% R%R%-%R% 
Gui, Show
OnMessage(0x0201, "WM_LBUTTONDOWN")
Sleep, 500
Gui, Destroy
Return
        Connected := FALSE
    }
    Else
    {
Soundbeep, 1800, 250
Gui, +AlwaysOnTop -Caption +hwndHGUI +LastFound
Gui, Color, ABCDEF
Gui, Font, s14 w800 cGREEN q5, Calibri
Gui, Margin, 15, 15
Gui, Add, Text, w333 h50 Center +0x0200 0x00800000, ATTENTION: Internet Has Reconnected!!
Gui, Font, s12 w400 c123456 q5, Calibri
Gui, Add, Text, x67 y+5, Continuing Connection Monitoring...
Gui, Show, Hide
WinGetPos, X, Y, W, H
R := Min(W, H) // 5
WinSet, Region, 0-0 W%W% H%H% R%R%-%R%
Gui, Show
OnMessage(0x0201, "WM_LBUTTONDOWN")
Sleep, 2000
Gui, Destroy
Return
        If (!Connected)
        {
            Connected := TRUE
        }
    }
}
IsConnected()
{
    INetworkListManager := ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}", "{DCB00000-570F-4A9B-8D69-199FDBA5723B}")
    DllCall(NumGet(NumGet(INetworkListManager+0)+11*A_PtrSize), "UPtr", INetworkListManager, "UShortP", t, "UInt")
    ObjRelease(INetworkListManager)
    Return t
} 
WM_LBUTTONDOWN() {
   PostMessage, 0x00A1, 2, 0
}
Return
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Internet Disconnect Alert

28 Apr 2023, 05:20

You might be able to achieve the detection with Windows Task Scheduler alone. You can then use it to run your alerting script. I've been running such tasks for a while.

The screenshot below shows how to detect a network connection. For a disconnection, change 10000 to 10001. This may or may not work depending on your situation and network.

image001-20230428.png
Windows Task Scheduler
image001-20230428.png (137.23 KiB) Viewed 802 times
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Internet Disconnect Alert

28 Apr 2023, 06:06

@mikeyww, from the documentation I can find on NetworkProfile events 10000 and 10001, they detect when a network connection has been made or disconnected - not an Internet connection. As I commented on viewtopic.php?f=76&t=116458 , there is a difference. You can still be connected to a LAN (local area network) either via ethernet cable or wi-fi, but NOT be connected to the internet (your ISP is having problems, your cable/fiber is disconnected from your router, etc.)

Have I misinterpreted the event codes? If your laptop is connected to your wi-fi, but you unplug the ISP's cable to your router, does the event still trigger?

Russ
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Internet Disconnect Alert

28 Apr 2023, 06:16

That's right-- I have not tested all situations-- but this approach may or may not be useful, depending on the specific need. Malcev's link may be more useful.

Thank you, Russ.
User avatar
Huh-Say What
Posts: 25
Joined: 24 Apr 2023, 20:44

Re: Internet Disconnect Alert

01 May 2023, 09:14

It took some tinkering and I had to scrap my original attempt, but I now have my internet monitor working. Imma just gonna drop this here in case it can help others...
It does not auto-reconnect for you, but clearly informs that your connected has dropped.

Code: Select all

;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  
#Persistent
#SingleInstance, Force
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
SetBatchlines, -1
SetWinDelay, 0
Menu, Tray, Icon, netshell.dll, 103 ; Green Antenna

; Initialize Variables

internetConnection := true

;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  
; Define GUI Styles

Gui, LostConnection: 
    +AlwaysOnTop 
    -Caption 
    +E0x20 
    +LastFound 
    +OwnDialogs 
    +ToolWindow
Gui, LostConnection: Color, B4F0FF 	 ; B4F0FF=Light Blue (Ice)
Gui, LostConnection: Font, s16 Bold, Segoe UI
Gui, LostConnection: Add, Text, cCA0000, Internet Connection Lost 	 ; CA0000=RED

;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  
Gui, ResumedConnection: 
    +AlwaysOnTop 
    -Caption 
    +E0x20 
    +LastFound 
    +OwnDialogs 
    +ToolWindow
Gui, ResumedConnection: Color, 008000 	 ; 008000=GREEN
Gui, ResumedConnection: Font, s16 Bold, Segoe UI
Gui, ResumedConnection: Add, Text, cFFFFFF Center, Internet Connection Resumed 	 ; FFFFFF=WHITE

;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  
; Main Loop.

Loop {
    ; Check internet connection.
    if (IsConnected()) {
        ; Connection established
        if (!internetConnection) {
; Internet connection re-established.
            Gui, LostConnection: Destroy
            Gui, ResumedConnection: Show, , Internet Connection Resumed
            Soundbeep, 1700, 300
            Sleep, 2000
            Reload
        }
        internetConnection := true
    } else {

; Wait for 6 seconds and try to reconnect before declaring lost connection. 
; Attempt to aid in minimizing site bombardment to a small degree and ignore mini connection hiccups.
            Sleep, 6000
; After 6 seconds...Connection lost.
        
            Gui, LostConnection: Show, , Internet Connection Lost
        internetConnection := false
    }
    Sleep, 1000
}
;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  
; Function to Check Internet Connection.

IsConnected() {
    try {
        WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
        WinHTTP.Open("GET", "http://www.google.com", false)
        WinHTTP.Send()
        return (WinHTTP.StatusText = "OK")
    } catch {
        return false
    }
}
Return

Edited: To add Gui, LostConnection: Destroy, as when connection was resumed the Lost Connection Gui was bleeding through depending on color choices.
Pt.2 Edit: Added 6 second sleep and the tray Icon I use. Not sure it's available on everyones pc however.
image.png
image.png (1.5 KiB) Viewed 711 times
Last edited by Huh-Say What on 01 May 2023, 17:16, edited 3 times in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Internet Disconnect Alert

01 May 2023, 09:46

It's good to have examples like this. Glad that you have it working.

Some sites will ban "callers" who bombard their sites too frequently. I have no idea what Google does or whether it matters to them. Just something to watch and understand. I would use the lowest frequency that provides the functionality that you need.
User avatar
Huh-Say What
Posts: 25
Joined: 24 Apr 2023, 20:44

Re: Internet Disconnect Alert

01 May 2023, 14:53

Thank you. By lowest frequency I'm assuming you mean something like checking every 10 seconds vs 1 second? (still a boatload of checks per hour)
I'm planning on adding a filter so it only runs while the pc is being actively used, checking against mouse/keyboard activity, but that is learning for another day.
And I've seen reference to 8.8.8.8 being better to use then Google.com, but not sure how to test that or actually how to implement that in place of Google.com.
Other than to head the ban warning, is there anything you see that can be improved on? As is this seems to work great, but I'm always open to more robust improvements.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Internet Disconnect Alert

01 May 2023, 15:26

Yes, a lower frequency could be one minute, etc.
I have not tested your script, but if it works, great!
As far as I know, 8.8.8.8 is a DNS server, so I don't know whether it works here, but you can always experiment.
Enjoy! :wave:
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Internet Disconnect Alert

02 May 2023, 05:39

You can also get a list of dedicated ping responders here located worldwide. I have a status board set up that pings every device on our network as well as a few of these ping responders every 20 seconds. I can immediately tell if a device is offline because the respective icon changes from green to red. If all of the ping responders turn red, I know there is a problem with our internet connection. Before anyone asks, no, the status board is not programmed in AHK.

Russ
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Internet Disconnect Alert

02 May 2023, 06:54

I cheat and use a program that someone else wrote!

https://www.nirsoft.net/utils/multiple_ping_tool.html

:)
User avatar
Huh-Say What
Posts: 25
Joined: 24 Apr 2023, 20:44

Re: Internet Disconnect Alert

02 May 2023, 10:42

So while I was fairly happy with the above script, I realized pretty quickly that the layout was somewhat limiting in what I could do. So I edited and rewrote, mostly for my idea of aethetics. New version provide rounded Gui cornering and allows to drag the disconnected notification Gui out of the way if connection is slow to return. Connection resumed is also draggable, but doesn't stay long enough to be used. I added double buffering (+0x0200 0x00800000) to smooth out the disconnected flickering caused by the looping, but that breaks the ability to drag, so it has been removed until I can figure out a way around that if possibe. I also attempted to make the Gui semi-transparent and click throughable, but that seems to run afoul of the Gui shaping (rounding). Anyhow, I hope this helps others at least a little.


Gui popup examples:
image.png
image.png (3.02 KiB) Viewed 616 times
image.png
image.png (3.02 KiB) Viewed 616 times

Code: Select all

; Initialize variables
internetConnection := true

Loop {
    ; Check internet connection.
    if (IsConnected()) {
        ; Connection established.
        if (!internetConnection) {
            Gui, LostConnection: Destroy

Gui, ResumedConnection: 
    +AlwaysOnTop 
    -Caption 
    +E0x20 
    +hwndHGUI 
    +LastFound 
    +OwnDialogs 
    +ToolWindow

Gui, ResumedConnection: Margin, 9, 9
Gui, ResumedConnection: Color, 051505 	 ; 051505=Midnight Green
Gui, ResumedConnection: Font, s14 Bold, Segoe UI
Gui, ResumedConnection: Add, Text, c008000 w280 h50 Center +0x0200 0x00800000, Internet Connection Resumed 	 ; 008000=GREEN

; 	  ̶  ̶  ̶  ̶  ̶  Gui shaping and positioning learning  ̶  ̶  ̶  ̶  ̶  
Gui, ResumedConnection: Show, Hide
WinGetPos, X, Y, W, H
R := Min(W, H) // 5 			 ; <<<<-----  Set value to amount of cornering. (0.5=Oval, 0=square, 1= capsule, 5=rounded corners).
WinSet, Region, 0-0 W%W% H%H% R%R%-%R% 	  ; <<<<-----  Cornering math.

            Gui, ResumedConnection: Show, , Internet Connection Resumed

OnMessage(0x0201, "WM_LBUTTONDOWN") ;<<<<-----  Drag Gui with mouse.

;  ̶  ̶  ̶  ̶  ̶  
            Soundbeep, 1900, 50
            Sleep, 1250
            Gui, ResumedConnection: Destroy
            Reload
        }
        internetConnection := true
    } else {

;  ̶  ̶  ̶  ̶  ̶  
Gui, LostConnection: 
    +AlwaysOnTop 
    -Caption 
    +E0x20 
    +hwndHGUI 
    +LastFound 
    +OwnDialogs 
    +ToolWindow

Gui, LostConnection: Margin, 9, 9
Gui, LostConnection: Color, 670104 	 ; 240404=Dark Plumb
Gui, LostConnection: Font, s16 Bold, Segoe UI
Gui, LostConnection: Add, Text, cFF0000 w270 h50 Center +0x0200 0x00800000, Internet Connection Lost 	 ; 670104=Red

; 	  ̶  ̶  ̶  ̶  ̶  Gui shaping and positioning learning  ̶  ̶  ̶  ̶  ̶  
Gui, LostConnection: Show, Hide
WinGetPos, X, Y, W, H
R := Min(W, H) // 5 			 ; <<<<-----  Set value to amount of cornering. (0.5=Oval, 0=square, 1= capsule, 5=rounded corners).
WinSet, Region, 0-0 W%W% H%H% R%R%-%R% 	  ; <<<<-----  Cornering math.

            Gui, LostConnection: Show, , Internet Connection Lost

OnMessage(0x0201, "WM_LBUTTONDOWN") ;<<<<-----  Drag Gui with mouse.
;  ̶  ̶  ̶  ̶  ̶  
        internetConnection := false
    }
    Sleep, 2000 
}

; 	  ̶  ̶  ̶  ̶  ̶  Gui mouse drag magic  ̶  ̶  ̶  ̶  ̶  
WM_LBUTTONDOWN() {
   PostMessage, 0x00A1, 2, 0
}

; 	  ̶  ̶  ̶  ̶  ̶  Function to check internet connection  ̶  ̶  ̶  ̶  ̶  
IsConnected() {
    try {
        WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
        WinHTTP.Open("GET", "http://www.google.com", false)
        WinHTTP.Send()
        return (WinHTTP.StatusText = "OK")
    } catch {
        return false
    }
}
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, peter_ahk and 338 guests