- Completed Windows autoset keyboard feature

This commit is contained in:
Ben Reaves
2020-06-15 01:15:01 -05:00
parent 7f9ccabf77
commit 2b9598c84f
4 changed files with 147 additions and 10 deletions

View File

@@ -1,19 +1,39 @@
#SingleInstance, force
#NoTrayIcon
OnMessage(0x219, "notify_change")
Return
lastnotify := 0
lastkb = ""
DllCall("AllocConsole")
WinHide % "ahk_id " DllCall("GetConsoleWindow", "ptr")
notify_change(wParam, lParam, msg, hwnd)
{
global lastnotify
T = %A_Now%
T -= 19700101000000,seconds
Tdiff := T
Tdiff -= lastnotify
if Tdiff > 5
global lastkb
; kbtype = % ComObjCreate("WScript.Shell").Exec("cscript /nologo usb.vbs").StdOut.ReadAll()
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
WshShell := ComObjCreate("Wscript.Shell")
exec := WshShell.Exec("cscript /nologo usb.vbs")
kbtype := exec.StdOut.ReadAll()
DllCall("FreeConsole")
Process Close, %pid%
if lastkb != %kbtype%
{
MsgBox % Tdiff
if InStr(kbtype, "Apple")
{
; MsgBox, Apple
Run, %A_ScriptDir%\NoShell.vbs %A_ScriptDir%\toggle_kb.bat mac, %A_ScriptDir%
}
else{
; MsgBox, Windows
Run, %A_ScriptDir%\NoShell.vbs %A_ScriptDir%\toggle_kb.bat win, %A_ScriptDir%
}
; MsgBox % kbtype
}
lastnotify := T
}
lastkb = %kbtype%
}

View File

@@ -2,6 +2,9 @@
#NoEnv
#Persistent
DetectHiddenWindows, On
Run, %A_ScriptDir%\detectUSB.ahk
I_Icon = %A_ScriptDir%\assets\kinto-white.ico
IfExist, %I_Icon%
Menu, Tray, Icon, %I_Icon%
@@ -44,9 +47,50 @@ tray_suspend(){
}
Exit() {
IfWinExist, detectUSB.ahk
WinClose
ExitApp
}
OnMessage(0x219, "notify_change")
return
lastkb = ""
DllCall("AllocConsole")
WinHide % "ahk_id " DllCall("GetConsoleWindow", "ptr")
notify_change(wParam, lParam, msg, hwnd)
{
global lastkb
; kbtype = % ComObjCreate("WScript.Shell").Exec("cscript /nologo usb.vbs").StdOut.ReadAll()
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
WshShell := ComObjCreate("Wscript.Shell")
exec := WshShell.Exec("cscript /nologo usb.vbs")
kbtype := exec.StdOut.ReadAll()
DllCall("FreeConsole")
Process Close, %pid%
if lastkb != %kbtype%
{
if InStr(kbtype, "Apple")
{
; MsgBox, Apple
Run, %A_ScriptDir%\NoShell.vbs %A_ScriptDir%\toggle_kb.bat mac, %A_ScriptDir%
}
else{
; MsgBox, Windows
Run, %A_ScriptDir%\NoShell.vbs %A_ScriptDir%\toggle_kb.bat win, %A_ScriptDir%
}
; MsgBox % kbtype
}
lastkb = %kbtype%
}
SetTitleMatchMode, 2
GroupAdd, terminals, ahk_exe ubuntu.exe

71
windows/usb.vbs Normal file
View File

@@ -0,0 +1,71 @@
Option Explicit
Dim oWMISrv, collDvcs, iUSBDvc , iDvc, sDvcID, sPID, sVID
' add item to array
Function AddItem(arr, val)
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = val
AddItem = arr
End Function
' returns an array of the unique items in for-each-able collection fex
Function uniqFE(fex)
Dim dicTemp : Set dicTemp = CreateObject("Scripting.Dictionary")
Dim xItem
For Each xItem In fex
dicTemp(xItem) = 0
Next
uniqFE = dicTemp.Keys()
End Function
Function ReplaceX(ByVal sValue, ByVal sPattern, ByVal sNValue)
Dim oReg : Set oReg = New RegExp
oReg.Pattern = sPattern
ReplaceX = oReg.Replace(sValue, sNValue)
Set oReg = Nothing
End Function
Set oWMISrv = GetObject("winmgmts:\\.\root\cimv2")
Set collDvcs = oWMISrv.ExecQuery("Select * From Win32_PnPEntity WHERE Service='kbdhid'")
Dim deviceVID : deviceVID=Array()
Dim devicePID : devicePID=Array()
Dim deviceDesc : deviceDesc=Array()
Dim counter: counter=0
For Each iUSBDvc In collDvcs
sVID = ReplaceX(iUSBDvc.DeviceID, ".*VID_(.{4}).*", "$1")
sPID = ReplaceX(iUSBDvc.DeviceID, ".*PID_(.{4}).*", "$1")
deviceVID = AddItem(deviceVID, sVID)
devicePID = AddItem(devicePID, sPID)
deviceDesc = AddItem(deviceDesc, iUSBDvc.Description)
counter = counter + 1
' Wscript.Echo "Name : "& iUSBDvc.Description &"VID_PID : "& sVID & sPID
Next
Dim uniqueVID: uniqueVID = uniqFE(deviceVID)
Dim vcount: vcount = UBound(uniqueVID) + 1
Dim nonApple: nonApple = 0
Dim i
If vcount = 1 Then
If StrComp(deviceVID(0), "05AC") = 0 Then
Wscript.Echo "Apple"
Else
Wscript.Echo "Windows"
End If
Else
For i = 0 To counter-1
If StrComp(deviceVID(i), "05AC") = -1 Then
nonApple = 1
End If
Next
If nonApple = 1 Then
Wscript.Echo "Windows"
Else
Wscript.Echo "Apple"
End If
End If
Set collDvcs = Nothing
Set oWMISrv = Nothing