Jump to content



Recommended Posts

If anyone finds this helpful (or wants to help improve my code), I have a small app that I'm testing which will keep track of CAPS-LOCK on a window-by-window basis.

Suppose you are in Excel where you need to type data in all CAPS but then you get a message on TEAMS or WHATSAPP requiring a quick response.  Currently, if you switch to TEAMS or WHATSAPP, it will inherit your PC's CAPSLOCK setting and your responses will all be in UPPERCASE.

With SmartCapsLock running in the background, it will keep track of which apps have CAPSLOCK on or OFF and adjust it accordingly.  If a user explicitly turns CAPSLOCK on, it will only be active for that APP.

Now, when you switch to TEAMS or WHATSAPP, your PC will be set to lowercase and, when you switch back to EXCEL, it will be restored to UPPERCASE.  If you explicitly turn off CAPSLOCK in EXCEL, It will be remembered as well.


#include <WinAPI.au3>
#include <MyFunctions.au3>

Opt('SendCapslockMode', 0)

Dim $actList[0][3]                                                      ; 3-col (pid, name, capslock-flag) active process array (processes that have been active)
$hActive = WinGetHandle("[active]")                                     ; most recently active window handle

While True                                                              ; loop forever
    $winList = WinList()                                                ; Get list of windows
    For $iWin = 1 To $winList[0][0]                                     ; For each window
        $tWnd = $winList[$iWin][0]                                      ; get window title
        $hWnd = $winList[$iWin][1]                                      ; get window handle
        If $tWnd = 'Task Switching' Then ContinueLoop                   ; skip this process
        If $tWnd <> '' Then                                             ; process has a name
            If WinActive($hWnd) Then                                    ; this is the active process with focus
                $iAct = _ArraySearch($actList, $hWnd)                   ; Find in active process list
                If $iAct < 0 Then                                       ; Add new process to active process list
                    $iAct = _ArrayAdd($actList, $hWnd)                  ; [0] window handle
                    $actList[$iAct][1] = $tWnd                          ; [1] window title
                    $actList[$iAct][2] = _SetCapsLock(False)            ; [2] default to lowercase
                Else                                                    ; This process has been active before
                    If $hActive = $hWnd Then                            ; This was the active process last time around
                        $actList[$iAct][2] = _GetCapsLock()             ; Store new capslock setting
                    Else                                                ; Process just became active again
                        _SetCapsLock($actList[$iAct][2])                ; Restore previously saved capslock for this process
                    EndIf                                               ; If $active <> $iAct
                EndIf                                                   ; If $iAct < 0
                $hActive = $hWnd                                        ; Remember previous active window handle
            EndIf                                                       ; If WinActive($tWnd)
        EndIf                                                           ; If $tWnd <> ''
    Next                                                                ; For $iWin = 1 To $winList[0][0]
WEnd                                                                    ; While True

Func _GetCapsLock()                                                     ; Determine capslock setting for newly tracked process
    $VK_CAPS = 0x14
    Local $ret = DllCall('user32.dll', 'long', 'GetKeyState', 'long', $VK_CAPS)
    If @error Then Return SetError(1, 0, 0)
    Return BitAND($Ret[0], 1)
EndFunc                                                                 ;==>_GetCapsLock
Func _SetCapsLock($lock)                                                ; Restore previously saved capslock setting
    If $lock Then
        Send('{CapsLock on}')
        Send('{CapsLock off}')
    Return $lock
EndFunc                                                                 ;==>_SetCapsLock


Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...