by garry » 28 Feb 2024, 08:32
F7 > toggle ON/OFF
Code: Select all
;- Activar y desactivar bluetooth
;- https://www.autohotkey.com/boards/viewtopic.php?f=40&t=126581&p=560889#p560889
#Requires AutoHotkey v2
;Run("ms-settings:bluetooth")
;- toggle
$F7::
{
static toggled := false
if toggled := !toggled
RadioModule('Bluetooth').SetState('On')
else
RadioModule('Bluetooth').SetState('Off')
return
}
;---
esc::exitapp()
;----
class RadioModule
{
; Docs: https://tinyurl.com/ynr9mkz4
; Header: windows.devices.radios.h
RadioKind := ['Other', 'WiFi', 'MobileBroadband', 'Bluetooth', 'FM']
RadioState := ['Unknown', 'On', 'Off', 'Disabled']
RadioAccessStatus := ['Unspecified', 'Allowed', 'DeniedByUser', 'DeniedBySystem']
; kind can be 'WiFi', 'MobileBroadband', 'Bluetooth', 'FM'
__New(kind) {
radios := IReadOnlyList( RadioModule.Await(IRadioStatics.GetRadiosAsync()) )
Loop radios.Count {
radio := IRadio(radios.Item[A_Index - 1])
} until this.RadioKind[radio.Kind + 1] = kind && this.module := radio
if !this.HasOwnProp('module') {
MsgBox kind . ' not found', 'Not found', 0x30
ExitApp
}
this.kind := kind
}
; state can be 'On' or 'Off'
SetState(state) {
accessStatus := ''
if !(state ~= 'i)^(On|Off)$') {
MsgBox 'Only "On" and "Off" states are supported', 'State not supported', 0x30
} else if this.State = state {
MsgBox this.kind . ' is already turned ' . StrLower(state), ' ', 0x40
} else {
pIAsyncOperation := this.module.SetStateAsync(state = 'On' ? 1 : 2)
accessStatus := this.RadioAccessStatus[RadioModule.Await(pIAsyncOperation) + 1]
if accessStatus != 'Allowed' {
MsgBox 'Failed to turn ' . StrLower(state) . ' ' . this.kind . '.`nAccess status: ' . accessStatus, 'Error', 0x10
}
else
{
MsgBox 'BLUETOOTH now TURNED to > ' . StrLower(state)
}
}
return accessStatus = 'Allowed'
}
State {
get => this.RadioState[this.module.State + 1]
set => this.SetState(value)
}
Name => WrtString(this.module.Name).GetText()
static Await(pIAsyncOperation) {
static AsyncStatus := ['Started', 'Completed', 'Canceled', 'Error']
AsyncOperation := IAsyncOperation(pIAsyncOperation)
AsyncInfo := IAsyncInfo(AsyncOperation.QueryIAsyncInfo())
Loop {
Sleep 10
status := AsyncStatus[AsyncInfo.Status + 1]
} until status != 'Started'
if (status != 'Completed') {
throw OSError('AsyncInfo error, status: "' . status . '"', A_ThisFunc)
}
return AsyncOperation.GetResults()
}
}
class IRadioStatics {
static __New() {
static IID_IRadioStatics := '{5FB6A12E-67CB-46AE-AAE9-65919F86EFF4}'
, VT_UNKNOWN := 13, runtimeClass := 'Windows.Devices.Radios.Radio'
DllCall('Ole32\CLSIDFromString', 'Str', IID_IRadioStatics, 'Ptr', CLSID := Buffer(16))
this.comObj := ComValue(VT_UNKNOWN, WrtString(runtimeClass).GetFactory(CLSID))
this.ptr := this.comObj.ptr
}
static GetRadiosAsync() => (ComCall(6, this, 'PtrP', &pIAsyncOperation := 0), pIAsyncOperation)
}
class IRadio extends InterfaceBase
{
SetStateAsync(state) => (ComCall(6, this, 'Int', state, 'PtrP', &pIAsyncOperation := 0), pIAsyncOperation)
State => (ComCall( 9, this, 'IntP', &state := 0), state)
Name => (ComCall(10, this, 'PtrP', &hString := 0), hString)
Kind => (ComCall(11, this, 'IntP', &kind := 0), kind)
}
class IReadOnlyList extends InterfaceBase
{
Item[index] => (ComCall(6, this, 'Int', index, 'PtrP', &pItem := 0), pItem)
Count => (ComCall(7, this, 'UIntP', &count := 0), count)
}
class IAsyncOperation extends InterfaceBase
{
QueryIAsyncInfo() => ComObjQuery(this, IID_IAsyncInfo := '{00000036-0000-0000-C000-000000000046}')
GetResults() => (ComCall(8, this, 'PtrP', &pResult := 0), pResult)
}
class IAsyncInfo extends InterfaceBase
{
Status => (ComCall(7, this, 'PtrP', &status := 0), status)
}
class InterfaceBase
{
__New(ptr) {
this.comObj := ComValue(VT_UNKNOWN := 0xD, ptr)
this.ptr := this.comObj.ptr
}
}
class WrtString
{
__New(stringOrHandle) {
if Type(stringOrHandle) = 'Integer'
this.ptr := stringOrHandle
else {
DllCall('Combase\WindowsCreateString', 'Str', stringOrHandle, 'UInt', StrLen(stringOrHandle), 'PtrP', &HSTRING := 0)
this.ptr := HSTRING
}
}
__Delete() => DllCall('Combase\WindowsDeleteString', 'Ptr', this)
GetText() => DllCall('Combase\WindowsGetStringRawBuffer', 'Ptr', this, 'Ptr', 0, 'Str')
GetFactory(riid) {
hr := DllCall('Combase\RoGetActivationFactory', 'Ptr', this, 'Ptr', riid, 'PtrP', &pInterface := 0)
if (hr != 0)
throw OSError(WrtString.SysError(hr), A_ThisFunc)
return pInterface
}
static SysError(nError := '') {
static flags := (FORMAT_MESSAGE_ALLOCATE_BUFFER := 0x100) | (FORMAT_MESSAGE_FROM_SYSTEM := 0x1000)
(nError = '' && nError := A_LastError)
DllCall('FormatMessage', 'UInt', flags, 'UInt', 0, 'UInt', nError, 'UInt', 0, 'PtrP', &pBuf := 0, 'UInt', 128)
err := (str := StrGet(pBuf)) = '' ? nError : str
DllCall('LocalFree', 'Ptr', pBuf)
return err
}
}
;=====================================================================
F7 > toggle ON/OFF
[code]
;- Activar y desactivar bluetooth
;- https://www.autohotkey.com/boards/viewtopic.php?f=40&t=126581&p=560889#p560889
#Requires AutoHotkey v2
;Run("ms-settings:bluetooth")
;- toggle
$F7::
{
static toggled := false
if toggled := !toggled
RadioModule('Bluetooth').SetState('On')
else
RadioModule('Bluetooth').SetState('Off')
return
}
;---
esc::exitapp()
;----
class RadioModule
{
; Docs: https://tinyurl.com/ynr9mkz4
; Header: windows.devices.radios.h
RadioKind := ['Other', 'WiFi', 'MobileBroadband', 'Bluetooth', 'FM']
RadioState := ['Unknown', 'On', 'Off', 'Disabled']
RadioAccessStatus := ['Unspecified', 'Allowed', 'DeniedByUser', 'DeniedBySystem']
; kind can be 'WiFi', 'MobileBroadband', 'Bluetooth', 'FM'
__New(kind) {
radios := IReadOnlyList( RadioModule.Await(IRadioStatics.GetRadiosAsync()) )
Loop radios.Count {
radio := IRadio(radios.Item[A_Index - 1])
} until this.RadioKind[radio.Kind + 1] = kind && this.module := radio
if !this.HasOwnProp('module') {
MsgBox kind . ' not found', 'Not found', 0x30
ExitApp
}
this.kind := kind
}
; state can be 'On' or 'Off'
SetState(state) {
accessStatus := ''
if !(state ~= 'i)^(On|Off)$') {
MsgBox 'Only "On" and "Off" states are supported', 'State not supported', 0x30
} else if this.State = state {
MsgBox this.kind . ' is already turned ' . StrLower(state), ' ', 0x40
} else {
pIAsyncOperation := this.module.SetStateAsync(state = 'On' ? 1 : 2)
accessStatus := this.RadioAccessStatus[RadioModule.Await(pIAsyncOperation) + 1]
if accessStatus != 'Allowed' {
MsgBox 'Failed to turn ' . StrLower(state) . ' ' . this.kind . '.`nAccess status: ' . accessStatus, 'Error', 0x10
}
else
{
MsgBox 'BLUETOOTH now TURNED to > ' . StrLower(state)
}
}
return accessStatus = 'Allowed'
}
State {
get => this.RadioState[this.module.State + 1]
set => this.SetState(value)
}
Name => WrtString(this.module.Name).GetText()
static Await(pIAsyncOperation) {
static AsyncStatus := ['Started', 'Completed', 'Canceled', 'Error']
AsyncOperation := IAsyncOperation(pIAsyncOperation)
AsyncInfo := IAsyncInfo(AsyncOperation.QueryIAsyncInfo())
Loop {
Sleep 10
status := AsyncStatus[AsyncInfo.Status + 1]
} until status != 'Started'
if (status != 'Completed') {
throw OSError('AsyncInfo error, status: "' . status . '"', A_ThisFunc)
}
return AsyncOperation.GetResults()
}
}
class IRadioStatics {
static __New() {
static IID_IRadioStatics := '{5FB6A12E-67CB-46AE-AAE9-65919F86EFF4}'
, VT_UNKNOWN := 13, runtimeClass := 'Windows.Devices.Radios.Radio'
DllCall('Ole32\CLSIDFromString', 'Str', IID_IRadioStatics, 'Ptr', CLSID := Buffer(16))
this.comObj := ComValue(VT_UNKNOWN, WrtString(runtimeClass).GetFactory(CLSID))
this.ptr := this.comObj.ptr
}
static GetRadiosAsync() => (ComCall(6, this, 'PtrP', &pIAsyncOperation := 0), pIAsyncOperation)
}
class IRadio extends InterfaceBase
{
SetStateAsync(state) => (ComCall(6, this, 'Int', state, 'PtrP', &pIAsyncOperation := 0), pIAsyncOperation)
State => (ComCall( 9, this, 'IntP', &state := 0), state)
Name => (ComCall(10, this, 'PtrP', &hString := 0), hString)
Kind => (ComCall(11, this, 'IntP', &kind := 0), kind)
}
class IReadOnlyList extends InterfaceBase
{
Item[index] => (ComCall(6, this, 'Int', index, 'PtrP', &pItem := 0), pItem)
Count => (ComCall(7, this, 'UIntP', &count := 0), count)
}
class IAsyncOperation extends InterfaceBase
{
QueryIAsyncInfo() => ComObjQuery(this, IID_IAsyncInfo := '{00000036-0000-0000-C000-000000000046}')
GetResults() => (ComCall(8, this, 'PtrP', &pResult := 0), pResult)
}
class IAsyncInfo extends InterfaceBase
{
Status => (ComCall(7, this, 'PtrP', &status := 0), status)
}
class InterfaceBase
{
__New(ptr) {
this.comObj := ComValue(VT_UNKNOWN := 0xD, ptr)
this.ptr := this.comObj.ptr
}
}
class WrtString
{
__New(stringOrHandle) {
if Type(stringOrHandle) = 'Integer'
this.ptr := stringOrHandle
else {
DllCall('Combase\WindowsCreateString', 'Str', stringOrHandle, 'UInt', StrLen(stringOrHandle), 'PtrP', &HSTRING := 0)
this.ptr := HSTRING
}
}
__Delete() => DllCall('Combase\WindowsDeleteString', 'Ptr', this)
GetText() => DllCall('Combase\WindowsGetStringRawBuffer', 'Ptr', this, 'Ptr', 0, 'Str')
GetFactory(riid) {
hr := DllCall('Combase\RoGetActivationFactory', 'Ptr', this, 'Ptr', riid, 'PtrP', &pInterface := 0)
if (hr != 0)
throw OSError(WrtString.SysError(hr), A_ThisFunc)
return pInterface
}
static SysError(nError := '') {
static flags := (FORMAT_MESSAGE_ALLOCATE_BUFFER := 0x100) | (FORMAT_MESSAGE_FROM_SYSTEM := 0x1000)
(nError = '' && nError := A_LastError)
DllCall('FormatMessage', 'UInt', flags, 'UInt', 0, 'UInt', nError, 'UInt', 0, 'PtrP', &pBuf := 0, 'UInt', 128)
err := (str := StrGet(pBuf)) = '' ? nError : str
DllCall('LocalFree', 'Ptr', pBuf)
return err
}
}
;=====================================================================
[/code]