pythonwindowsmacoscapslock

Create Program with python to Change Caps Lock in Windows Like in MacOS


I want to make Program with python for change My normal Caps Lock in Windows can do the same think in MacOS.

Features -Press Caps Lock to switch language keyboard. ( Keyboard Layout ) -Hold Caps Lock 1000ms to turn on/off Caps Lock.

I new in python, I think about concept it will work like this.

// sudo code
keyboard // keyboard event
keyboard.press // keyboard key press

while (true) // loop for check all the time
{
  if (keyboard.press === 'Caps Lock') { // if press Caps Lock
    n = 0; // create variable
    while (keyboard.press === 'Caps Lock' && n < 1000) { // if press/hold Caps Lock
      delay(1) // delay 1 ms
      n++ // n = n + 1
    }
    // when release key / hold longer 1000ms
    if ( n < 1000 ) {
      // switch language keyboard. ( Keyboard Layout )
    } else {
      // toggle Caps Lock.
    }
  }
}

Solution

  • In the end I use AutoHotKey to switch keyboard language ( Keyboard layout )

    I have 2 options.

    Option 1. This Script Press Caps Lock to switch Layout but Press Shift + Caps Lock to toggle Caps Lock

    For use with Auto Hot Key Script V1

    #NoEnv
    SendMode Input
    SetWorkingDir %A_ScriptDir%
    
    sel := 0
    
    #if (sel=0)
    capslock::
      send {lwin down}{Space}
      sel := 1
    return
    #if
    
    capslock up::
      send {lwin up}
      sel := 0
    return
    

    For use with Auto Hot Key Script V2

    SendMode "Input"
    SetWorkingDir A_ScriptDir
    
    sel := 0
    CapsLock:: {
      if sel = 0 {
        Send "{LWin Down}{Space}"
        global sel := 1
      }
    }
    CapsLock Up:: {
      Send "{LWin Up}"
      global sel := 0
    }
    

    Option 2. This Script Press Caps Lock to switch Layout but Hold Caps Lock for 0.5 Second to toggle Caps Lock

    Win + Space can use only windows 8/8.1/10/11

    Cr. : https://www.autohotkey.com/boards/viewtopic.php?f=6&t=95338&fbclid=IwAR1PGe0NfvkfFvvJGkBe89OoLMPQCCsOvm2kELEIkXczRFqcmfbN1ms3txI

    For use with Auto Hot Key Script V1

    #NoEnv
    SendMode Input
    SetWorkingDir %A_ScriptDir%
    
    $CapsLock::
    KeyWait, CapsLock, T0.5
    If ErrorLevel
    {
        if GetKeyState("CapsLock", "T") = 0
        SetCapsLockState on
        else 
        SetCapsLockState off
        Keywait, CapsLock
    }
    else Send {lWinDown}{Space}{lWinUp} 
    Return