Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically toggle Airplane mode on Windows

On my laptop I can toggle Airplane mode manually by pressing FN+F12, I want to do the same thing automatically from VB6 project or VBA.

I did a lot of search and only found answers about Enable/Disable wireless adapter or using Sendkeys for Windows 8:

Dim WSh As Object
Set WSh = CreateObject("Wscript.Shell")
WSh.Run "C:\WINDOWS\system32\rundll32.exe %SystemRoot%\system32\van.dll,RunVAN", , True
Sleep 200
WSh.SendKeys " "
Sleep 1000
WSh.SendKeys "{ESC}"

But this code is not reliable and I don't think it will work on Windows 7 or Windows 10.

So my question is: Is there any reliable way to automatically toggle Airplane mode on Windows.

like image 633
Fadi Avatar asked Feb 07 '17 13:02

Fadi


People also ask

Is there a hotkey for airplane mode Windows 10?

Function key Some computers, such as laptops, include a dedicated hardware key to enable or disable Airplane mode. If this is the case, you can simply press the Function key + Airplane key to turn on the feature.

Is there a hotkey for airplane mode?

Function key method Laptops usually include a dedicated physical key to enable or disable Airplane mode. If this is the case, press the Function key + Airplane keyboard shortcut (or dedicated key) to turn on the feature.

Why does my computer automatically go to airplane mode?

Reasons Why Windows 10 Gets Stuck in Airplane Mode Usually, the problem is due to software bugs or glitches, faulty network drivers, or a simple physical switch. In most cases, your first approach should be to restart the computer.


2 Answers

I do not have a function key on my keyboard, and this is not tested, but just an idea - why don't you try like this:

Sub SetMode()

    CreateObject("Shell.Application").MinimizeAll        
    Application.SendKeys "{F12}"  'Try a way to refer the function key

End Sub

Another possible option is something like this, depending on your Windows:

Option Explicit

Public Sub TestMe()

    Application.SendKeys ("^{ESC}")
    Application.Wait Now + TimeValue("00:00:01")
    SendKeys ("{s}")
    SendKeys ("{e}")
    SendKeys ("{t}")
    SendKeys ("{t}")
    SendKeys ("{i}")
    SendKeys ("{n}")
    SendKeys ("{g}")
    SendKeys ("{s}")
    SendKeys "~", False
    Application.Wait Now + TimeValue("00:00:01")
    SendKeys ("{a}")
    SendKeys ("{i}")
    SendKeys ("{r}")
    Application.Wait Now + TimeValue("00:00:01")
    SendKeys "~", False
    Application.Wait Now + TimeValue("00:00:01")
    SendKeys "~", False

End Sub

The ~ sign is for ENTER, and the ctrl+escape simulates the windows key on your keyboard. After you reach what you want you can navigate with tabs and arrows.

like image 148
Vityata Avatar answered Oct 14 '22 16:10

Vityata


Solution 1 : SendKeys

AFAIK the Fn key on a keyboard is not intercepted by Windows, it is a hardware mapping to a function key ie. "Volume Up". Now the problem with that is that the "shutdown/enable wifi" key sends a signal to the hardware to power off the card. So that's that for the SendKey, there is no virtual key for "Wireless off/on" (although there is one for "volume up").

Solution 2 : The Windows 8 API

Now the other approach would be to use the Windows 8 API here https://msdn.microsoft.com/en-us/library/windows/hardware/hh406627(v=vs.85).aspx and more specifically the following interfaces :

  • IMediaRadioManager
  • IRadioInstance
  • IRadioInstanceCollection
  • IMediaRadioManagerNotifySink

This should allow you to get the radio for bluetooth, wifi, ... as well as the "airplane mode" then shut them down, but I've never tried to use that with VBS.

Solution 3 : Using WMI queries

Using WMI queries you can basically access anything in your machine, including network cards. The class you are looking for is "Win32_NetworkAdapter" and all docs can be found here : https://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx

Here is a little sample code that will list the current network adapters, you can customize this to save which ones were enabled before you run the script to be able to re enable them after.

' connects to the WMI server of the local machine
Set objConnection = GetObject("winmgmts:" _
    & "{impersonationLevel=Delegate," _
    & "authenticationLevel=PktPrivacy}!" _
    & "\\localhost\root\cimv2")

' gets a list of all the network adapters in the system
Set objNetworkAdapters = objConnection.ExecQuery("SELECT * FROM  Win32_NetworkAdapter")

' loops through all network adapters
For Each objCurrentNetworkAdapter in objNetworkAdapters
    ' objCurrentNetworkAdapter.Disable
    ' objCurrentNetworkAdapter.Enable
    WScript.Echo objCurrentNetworkAdapter.Name
Next

Remark :

You are basically not supposed to access the "Airplane mode" from code as it is a user privilege to do so, imagine if someone builds an app that turns on roaming and data connection then starts updating while you are abroad...

like image 31
Foxtrot Avatar answered Oct 14 '22 17:10

Foxtrot