How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? EDITED Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? EDITED

22 Dec 2017, 13:03

Hello friends..

I want that when I press f1 hotkey then it should disable (disconnect) Wireless Network Connection in network and sharing center which exists in control panel. If I again press f1 key then it should enable (connect) Wireless Network Connection in network and sharing center existing in control panel. I am using windows 7, 32 bit.

please look at these screen shot-

Image


Image


I searched the forum for getting the help for the same. I found following links-

https://autohotkey.com/board/topic/1289 ... rk-switch/
https://autohotkey.com/board/topic/1722 ... ntry372132

in the above links i found these codes-

Code: Select all

f1::
c:=!c  ; toggle c between 1 or 0
if !c
soundbeep  ;warn when off 
netconnect()  ; set c to 1 or 0  to switch on off
return

NetConnect(bEnable := False, sConnection := "Wireless Network Connection") { ; for AHK v1.1+
    For oConn In ComObjCreate("Shell.Application").Namespace(0x0031).Items
    If  oConn.Name = sConnection {
        If InStr(oConn.Verbs.Item(0).Name, bEnable ? "&a" : "&b")
            oConn.Verbs.Item(0).DoIt
        Break
    }
}
return
but these codes are not working at all. Please help me to enable/disable wireless network connection by pressing a hotkey. Thanks..
Last edited by Sabestian Caine on 23 Dec 2017, 00:52, edited 1 time in total.
I don't normally code as I don't code normally.
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

23 Dec 2017, 11:22

teadrinker wrote:Hi, Sabestian Caine,

Try this.
thanks dear teadrinder for your kind reply...

But the codes provided by you in https://autohotkey.com/boards/viewtopic ... 11#p181811 are not working for me....
i used the codes like this-

Code: Select all

F1:: ToggleWiFi()

ToggleWiFi()  {
   wmi := ComObjGet("winmgmts:")
   for adapter in wmi.ExecQuery("Select * from Win32_NetworkAdapter")
      if InStr(adapter.name, "Wireless Network Connection") && (interfaceName := adapter.NetConnectionID) && status := adapter.NetConnectionStatus
         break
      ; i have wireless network connection so i changed wireless to wireless network connection
   if (interfaceName = "" || status = "")  {
      MsgBox, Failed to get the interfaceName!
      return
   }
   if status not in 0,2   ; Disconnected = 0, Connected = 2
   {
      Loop  {
         Sleep, 500
         for adapter in wmi.ExecQuery("Select * from Win32_NetworkAdapter Where Index=" . adapter.Index)
            status := adapter.NetConnectionStatus
      } until status = 0 || status = 2 || (A_Index = 20 && failed := true)
      if failed  {
         MsgBox, Failed to get the status!
         return
      }
   }
   Run, % (A_IsAdmin ? "" : "*RunAs ") . "netsh.exe interface set interface name="""
                                       . interfaceName . """ admin="
                                       . (status = 0 ? "en" : "dis") . "abled",, Hide
}
when i run these codes by pressing f1 key then it shows msgbox failed to get the interfaceName!
i am using win7 32 bit...
please solve my issue...
thanks a lot...
I don't normally code as I don't code normally.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED  Topic is solved

23 Dec 2017, 11:28

Sabestian Caine wrote:

Code: Select all

if InStr(adapter.name, "Wireless Network Connection") && (interfaceName := adapter.NetConnectionID) && status := adapter.NetConnectionStatus
Did you try

Code: Select all

if InStr(adapter.name, "wireless") && (interfaceName := adapter.NetConnectionID) && status := adapter.NetConnectionStatus
?
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

23 Dec 2017, 12:39

teadrinker wrote:
Sabestian Caine wrote:

Code: Select all

if InStr(adapter.name, "Wireless Network Connection") && (interfaceName := adapter.NetConnectionID) && status := adapter.NetConnectionStatus
Did you try

Code: Select all

if InStr(adapter.name, "wireless") && (interfaceName := adapter.NetConnectionID) && status := adapter.NetConnectionStatus
?
thanks dear teadrinker... codes are working fine with if InStr(adapter.name, "wireless") && (interfaceName := adapter.NetConnectionID) && status := adapter.NetConnectionStatus

please tell me one more thing.. if i use the internet by LAN cable then it can also be connected or disconnected by the above codes or they are only for wireless connection? please guide....
I don't normally code as I don't code normally.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

23 Dec 2017, 13:04

Sabestian Caine wrote:if i use the internet by LAN cable then it can also be connected or disconnected by the above codes or they are only for wireless connection?
This code is only for wireless connection. For another connections you need to figure out exact name of your current adapter and replace

Code: Select all

if InStr(adapter.name, "wireless")
with

Code: Select all

if (adapter.name = "your adapter name")
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

23 Dec 2017, 23:34

teadrinker wrote:
Sabestian Caine wrote:if i use the internet by LAN cable then it can also be connected or disconnected by the above codes or they are only for wireless connection?
This code is only for wireless connection. For another connections you need to figure out exact name of your current adapter and replace

Code: Select all

if InStr(adapter.name, "wireless")
with

Code: Select all

if (adapter.name = "your adapter name")

Ok... Thank u so much...

Plz tell me how can i know the name of my adaptor..

In network and sharing center it shows local area connection 3... Is my adopter name is that??

Plz guide...

Thanks a lot...
I don't normally code as I don't code normally.
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

24 Dec 2017, 05:35

Sabestian Caine wrote:Plz tell me how can i know the name of my adaptor..
Launch the next code:

Code: Select all

run ncpa.cpl
Network Connections folder will open:

Image

In this case adapter name is "Realtek PCIe GBE Family Controller".
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

25 Dec 2017, 03:35

teadrinker wrote:
Sabestian Caine wrote:Plz tell me how can i know the name of my adaptor..
Launch the next code:

Code: Select all

run ncpa.cpl
Network Connections folder will open:

Image

In this case adapter name is "Realtek PCIe GBE Family Controller".
Thank u so much dear teadrinker.... Now i have understood how to get adapter name....
you are really great...
i appreciate your skills....
thanks a lot once again.... :clap: :salute:
I don't normally code as I don't code normally.
yannicks

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

23 Apr 2018, 04:57

I tried this code to disable/enable my virtual TAP device (used by OpenVPN) but it gives me this error:
Failed to get the status!

Will this work with a virtual network adapter?
william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

Re: How to Enable or Disable Wireless Network Connection in network and sharing center, when I press specific hotkey? ED

29 Oct 2022, 11:32

Another solution:

Code: Select all

toggle_wifi() {
	static wifi_name := "Wi-Fi"
	wifi_status := RunCMD("netsh interface show interface name=""" wifi_name """")
	Run % "netsh interface set interface name=""" wifi_name """ admin=" (InStr(wifi_status, "Disabled") ? "EN" : "DIS" "ABLED"),, Hide
}
(Get RunCMD)
I find this to be quicker than wmi.ExecQuery.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Frogrammer and 270 guests