jaberwacky Posted November 26, 2023 Share Posted November 26, 2023 (edited) At my work I use a program which requires all caps. My work is high paced and so switching back and forth all day between all caps is a pain. So I wrote a script that toggles caps lock on and off accordingly. What follows is my version (which may not work at that point) and then followed by ChatGPTs version. I have since modified ChatGPTs version to suit my particular quirky coding style. The biggest changes that ChatGPT made are to the MousePosition, WindowUnderMouse (originally written by Ascendant), and IsCDKActive functions. Sorry. Something funky is going on. I must not have access to the very original code I fed to ChatGPT ... Puny human version (but still pretty good if I do say so myself.): expandcollapse popup#include <APISysConstants.au3> #include <WinAPISysWin.au3> #include <WinAPI.au3> #include <Misc.au3> #include <GUIConstants.au3> Opt("SendCapslockMode", 0) Opt("WinTitleMatchMode", 2) Global Const $mouse_proc_callback = DllCallbackRegister(CapslockSentinel, "long", "int;wparam;lparam") Global Const $hook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($mouse_proc_callback), _WinAPI_GetModuleHandle(0)) OnAutoItExitRegister(OnAutoItExit) While True Sleep(1000) WEnd Func CapslockSentinel($code, $w_param, $l_param) Switch $code >= 0 Case True Switch $w_param Case $wm_mousemove Local Static $hWinPrev = ' ' Local Static $cdk_active = False Local Const $hWin = WindowUnderMouse($l_param) If $hWin <> $hWinPrev Then $hWinPrev = $hWin Local Const $title = WinGetTitle($hWin) Select Case IsCDKActive($title) And Not $cdk_active ConsoleWrite("CDK: Active -- Capslock: On" & @CRLF) If Not IsCapsLockOn() Then ToggleCapslock("On") EndIf $cdk_active = True Case Not IsCDKActive($title) And $cdk_active ConsoleWrite("CDK: Inactive -- Capslock: Off" & @CRLF) If IsCapsLockOn() Then ToggleCapslock("Off") EndIf $cdk_active = False EndSelect EndIf EndSwitch EndSwitch Return _WinAPI_CallNextHookEx($hook, $code, $w_param, $l_param) EndFunc Func MousePos(Const ByRef $l_Param) Local Const $MSLLHOOKSTRUCT = $tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo" Local Const $info = DllStructCreate($MSLLHOOKSTRUCT, $l_Param) Local Const $mousePos[2] = [DllStructGetData($info, 1), DllStructGetData($info, 2)] Return $mousePos EndFunc Func WindowUnderMouse(Const ByRef $l_param) ; Ascendant Local Const $stPoint = DllStructCreate("long; long") Local Const $mousePos = MousePos($l_param) DllStructSetData($stPoint, 1, $mousePos[0]) DllStructSetData($stPoint, 2, $mousePos[1]) Local Const $stInt64 = DllStructCreate("int64", DllStructGetPtr($stPoint)) Local Const $aRet = DllCall("user32.dll", "hwnd", "WindowFromPoint", "int64", DllStructGetData($stInt64, 1))[0] ; Because _WindowFromPoint() can return 'sub' windows, or control handles, we should seek the owner window Return $aRet ? _WinAPI_GetAncestor($aRet, 2) : SetError(1, @error, 0) EndFunc Func IsCDKActive(Const ByRef $title) If StringInStr($title, "CDK Drive") Or _ StringInStr($title, "Tab Name") Or _ StringInStr($title, "Tab Color") Or _ StringInStr($title, "Create Order") Or _ StringInStr($title, "Cashiering") Or _ StringInStr($title, "Enter Change Reason") Then Return True EndIf Return False EndFunc Func IsCapsLockOn() Return BitAND(_WinAPI_GetKeyState(0x14), 0x01) = 1 EndFunc Func ToggleCapslock(Const ByRef $state) Send("{CAPSLOCK " & $state & '}') EndFunc Func OnAutoItExit() _WinAPI_UnhookWindowsHookEx($hook) DllCallbackFree($mouse_proc_callback) EndFunc ChatGPT: expandcollapse popup#include <APISysConstants.au3> #include <WinAPISysWin.au3> #include <WinAPI.au3> #include <Misc.au3> #include <GUIConstants.au3> Opt("SendCapslockMode", 0) Opt("WinTitleMatchMode", 2) Global Const $MOUSE_PROC_CALLBACK = DllCallbackRegister("CapslockSentinel", "long", "int;wparam;lparam") Global Const $HOOK = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($MOUSE_PROC_CALLBACK), _WinAPI_GetModuleHandle(0)) OnAutoItExitRegister("OnAutoItExit") While True Sleep(1000) WEnd Func CapslockSentinel($nCode, $wParam, $lParam) Switch $nCode >= 0 Case True Switch $wParam Case $WM_MOUSEMOVE Local Static $hPrevWin = ' ' Local Static $bCDKActive = False Local Const $hWin = GetWindowUnderMouse($lParam) If $hWin <> $hPrevWin Then $hPrevWin = $hWin Local Const $sTitle = WinGetTitle($hWin) If IsCDKActive($sTitle) And Not $bCDKActive Then ConsoleWrite("CDK: Active -- Capslock: On" & @CRLF) If Not IsCapsLockOn() Then ToggleCapsLock("On") $bCDKActive = True ElseIf Not IsCDKActive($sTitle) And $bCDKActive Then ConsoleWrite("CDK: Inactive -- Capslock: Off" & @CRLF) If IsCapsLockOn() Then ToggleCapsLock("Off") $bCDKActive = False EndIf EndIf EndSwitch EndSwitch Return _WinAPI_CallNextHookEx($HOOK, $nCode, $wParam, $lParam) EndFunc Func GetMousePos(Const ByRef $lParam) Local Const $MSLLHOOKSTRUCT = $tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo" Local Const $info = DllStructCreate($MSLLHOOKSTRUCT, $lParam) Return DllStructCreate("int[2]", DllStructGetPtr($info)) EndFunc Func GetWindowUnderMouse(Const ByRef $lParam) Local Const $stPoint = GetMousePos($lParam) Local Const $stInt64 = DllStructCreate("int64", DllStructGetPtr($stPoint)) Local Const $hWnd = DllCall("user32.dll", "hwnd", "WindowFromPoint", "int64", DllStructGetData($stInt64, 1))[0] Return $hWnd ? _WinAPI_GetAncestor($hWnd, 2) : SetError(1, @error, 0) EndFunc Func IsCDKActive(Const ByRef $sTitle) Local Const $aKeywords = ["CDK Drive", "Tab Name", "Tab Color", "Create Order", "Cashiering", "Enter Change Reason"] For $i = 0 To UBound($aKeywords) - 1 If StringInStr($sTitle, $aKeywords[$i]) Then Return True Next Return False EndFunc Func IsCapsLockOn() Return BitAND(_WinAPI_GetKeyState(0x14), 0x01) = 1 EndFunc Func ToggleCapsLock(Const ByRef $sState) Send("{CAPSLOCK " & $sState & '}') EndFunc Func OnAutoItExit() _WinAPI_UnhookWindowsHookEx($HOOK) DllCallbackFree($MOUSE_PROC_CALLBACK) EndFunc Edited November 26, 2023 by jaberwacky Put the actual orginal code ... TheSaint, Danyfirex and argumentum 3 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now