Winkiez LockKing Source code

Back to information page

; Winkiez LockKing -- by Winkie
; -- version 1.1 -- last changed: 25 may 2006

; KeyState places a single TrayIcon in the SystemTray which displays
; the state of CapsLock, NumLock AND ScrollLock.
; It also has some other options regarding (some of) this keys and the Insert key.

; More information:
; http://file.autohotkey.net/Winkie/lockking/index.htm
; This script is licensed under a Creative Commons Licence:
; http://creativecommons.org/licenses/by/2.5/

; *** Auto-execute ***
#SingleInstance, force
#Persistent
#InstallKeybdHook ; Better safe than sorry
#MaxThreadsPerHotkey 1 ; Better safe than sorry
SetBatchLines, -1 ; Speed up script
SetWinDelay, 0 ; Speed up script

; Localizing
; Change these if you want to localize this script
; so it has no effect on commands
g_On = On
g_Off = Off

; Set Default States
StringTrimRight, AppName, A_ScriptName 4
Gosub ReadIni

If g_bSetDefCl
   v_Tmp1 = On
Else
   v_Tmp1 = Off
If g_bSetDefNl
   v_Tmp2 = On
Else
   v_Tmp2 = Off
If g_bSetDefSl
   v_Tmp3 = On
Else
   v_Tmp3 = Off
SetCapslockState, %v_Tmp1%
SetNumlockState, %v_Tmp2%
SetScrolllockState, %v_Tmp3%
v_Tmp1 =
v_Tmp2 =
v_Tmp3 =

Gosub,  TrayMenu

; Watch States
SetTimer, Start
Return

; *** Keys ***
*CapsLock:: ; Wildcard
   Gosub, Caps
Return
; Other Lock keys not needed, because this is handled by the Timer

~Insert:: ; Native function allowed
   Gosub PressInsert
Return

; *** Subroutines ***
Start:
   If GetKeyState("CapsLock","T")
      g_bClState :=true
   Else
      g_bClState :=false
   If GetKeyState("Numlock","T")
      g_bNlState :=true
   Else
      g_bNlState :=false
   If GetKeyState("ScrollLock","T")
      g_bSlState :=true
   Else
      g_bSlState :=false

   v_OldIcon = %g_Icon%
   
   ; Determine which Icon must be loaded
   If (g_bClState = true and g_bNlState = true and g_bSlState = true)
   {
      g_Icon := 1
   }
   Else If (g_bClState = true and g_bNlState = true and g_bSlState = false)
   {
      g_Icon := 2
   }
   Else If (g_bClState = true and g_bNlState = false and g_bSlState = true)
   {
      g_Icon := 3
   }
   Else If (g_bClState = false and g_bNlState = true and g_bSlState = true)
   {
      g_Icon := 4
   }
   Else If (g_bClState = true and g_bNlState = false and g_bSlState = false)
   {
      g_Icon := 5
   }
   Else If (g_bClState = false and g_bNlState = true and g_bSlState = false)
   {
      g_Icon := 6
   }
   Else If (g_bClState = false and g_bNlState = false and g_bSlState = true)
   {
      g_Icon := 7
   }
   Else ; (g_bClState = false and g_bNlState = false and g_bSlState = false)
   {
      g_Icon := 8
   }
   
   ; Change Icon (if nessesary)
   If g_Icon <> %v_OldIcon%
   {
      If g_bSetDefPlay
      {
         If v_OldIcon  ; No SoundPlay on start-up (g_Icon = )
            SoundPlay, music1.wav
      }
      Menu, Tray, Icon, lockking.dll, %g_Icon%
      Gosub, NowTrayTip
   } ; Else: Do Nothing
Return

PressInsert:
   If g_bSetInsert
   {
      ; Determine which Icon must be loaded
      If (g_Icon = 1 or g_Icon = 3)
         v_Insert = 9
      Else If (g_Icon = 2 or g_Icon = 5)
         v_Insert = 10
      Else If (g_Icon = 4 or g_Icon = 7)
         v_Insert = 11
      Else ; (g_Icon = 6 or g_Icon = 8)
         v_Insert = 12

      ; Perform warning
      If g_bSetInsertPlay
         SoundPlay, music2.wav
      
      Menu, Tray, Icon, lockking.dll, %v_Insert%
      Sleep, 500
      Menu, Tray, Icon, lockking.dll, %g_Icon%
      Sleep, 500
      If g_bSetInsertPlay
         SoundPlay, music2.wav
      
      Menu, Tray, Icon, lockking.dll, %v_Insert%
      Sleep, 500
      Menu, Tray, Icon, lockking.dll, %g_Icon%
   }
Return

Caps:
   If g_bSetMultiCaps
      Gosub, MultiCaps
   Else
      Gosub, CapsWarning
Return

MultiCaps:
   If v_CapsPress > 0 ;SetTimer already started, log the keypress instead
   {
      v_CapsPress += 1
      Return
   }
   ; Else: this is the first press of a new series
   ;Set count to 1 and start the timer
   v_CapsPress = 1
   SetTimer, KeyCaps, %g_iSetCapsSpeed% ; Wait for more presses within x milliseconds

   ; Prevent 'state flickering' every x milliseconds when CapsWarning isn't active
   If (g_bSetCaps = false)
   {
      Loop,
      {
         Sleep, 100
         GetKeyState, v_State, CapsLock, P
         If v_State=U
            Break
      }
      Return
   }

   Gosub, CapsWarning
Return

CapsWarning:
   If g_bSetCaps
   {
      v_Counter = 1000
      Loop, 10
      {
         ToolTip, CapsLock key is down`n%v_counter% milliseconds
         Sleep, 100
         v_Counter -= 100
         If v_Counter = 700
         {
            If g_bSetCapsPlay
               SoundPlay, music3.wav
         }
         GetKeyState, v_State, CapsLock, P
         If v_State=U
            Break
      }
      ToolTip,
      If v_Counter > 0
         Return
      ; Else
      If g_bClState
      {
         v_State1 = %g_Off%
      }
      Else
      {
         v_State1 = %g_On%
      }

      Menu, Caps, Add
      Menu, Caps, Delete
      Menu, Caps, Add, Caps Lock %v_State1%, ToggleCapsLock
      Menu, Caps, Add ; separator
      Menu, Caps, Add, Cancel (Esc), CapsCancel
      Menu, Caps, Default, Caps Lock %v_State1%
      Menu, Caps, Show
   }
   Else
      If (g_bSetMultiCaps = false)
         Gosub, ToggleCapsLock
Return

CapsCancel:
Return

KeyCaps:
   SetTimer, KeyCaps, off
   If v_CapsPress > 1
   {
      Gosub, ToggleCapsLock
   }
   ; Reset counter
   v_CapsPress = 0
return

ToggleCapsLock:
   If g_bClState
   {
      SetCapslockState, Off
   }
   Else
   {
      SetCapslockState, On
   }
Return

ToggleNumLock:
   If g_bNlState
   {
      SetNumlockState, Off
   }
   Else
   {
      SetNumlockState, On
   }
Return

ToggleScrollLock:
   If g_bSlState
   {
      SetScrolllockState, Off
   }
   Else
   {
      SetScrolllockState, On
   }
Return

; *** INI & Settings Specific Subroutines ***
ReadIni:
   IfNotExist, %AppName%.ini 
   {
      v_Ini=[Settings]
      v_Ini=%v_Ini%`nSetDefCl=0
      v_Ini=%v_Ini%`nSetDefNl=1
      v_Ini=%v_Ini%`nSetDefSl=0
      v_Ini=%v_Ini%`nSetDefPlay=1
      v_Ini=%v_Ini%`nSetInsert=1
      v_Ini=%v_Ini%`nSetInsertPlay=1
      v_Ini=%v_Ini%`nSetMultiCaps=1
      v_Ini=%v_Ini%`nSetCapsSpeed=600
      v_Ini=%v_Ini%`nSetCaps=1
      v_Ini=%v_Ini%`nSetCapsPlay=0
   }
   FileAppend, %v_Ini%, %AppName%.ini
   ini=
   
   IniRead, g_bSetDefCl, %AppName%.ini, Settings, SetDefCl,0
   IniRead, g_bSetDefNl, %AppName%.ini, Settings, SetDefNl,1
   IniRead, g_bSetDefSl, %AppName%.ini, Settings, SetDefSl,0
   IniRead, g_bSetDefPlay, %AppName%.ini, Settings, SetDefPlay,1
   IniRead, g_bSetInsert, %AppName%.ini, Settings, SetInsert,1
   IniRead, g_bSetInsertPlay, %AppName%.ini, Settings, SetInsertPlay,1
   IniRead, g_bSetMultiCaps, %AppName%.ini, Settings, SetMultiCaps,1
   IniRead, g_iSetCapsSpeed, %AppName%.ini, Settings, SetCapsSpeed,600
   IniRead, g_bSetCaps, %AppName%.ini, Settings, SetCaps,1
   IniRead, g_bSetCapsPlay, %AppName%.ini, Settings, SetCapsPlay,0
Return

GuiSettings:
   ; Make DropDown list
   v_SpeedNow = 300
   v_SetListItems = 300
   Loop, 45
   {
      If v_SpeedNow = %g_iSetCapsSpeed%
         v_SetItem = %A_Index% ; start postion

      v_SpeedNow += 100
      v_SetListItems =%v_SetListItems%|%v_SpeedNow%
   }

   Gui, 1:Destroy
   Gosub, ReadIni
    Gui, 1:Font, Underline Bold
   Gui, 1:Add, Text, xm+20 y+20, Sound play at state change
    Gui, 1:Font, Norm
   Gui, 1:Add, Checkbox, xs Checked%g_bSetDefPlay% vg_bSetDefPlay, &Play sound when Caps Lock, Num Lock or Scroll Lock are turned On or Off
    Gui, 1:Font, Underline Bold
   Gui, 1:Add, Text, xs y+20, Check the items which must be set On at program start up
    Gui, 1:Font, Norm
   Gui, 1:Add, Checkbox, xs Checked%g_bSetDefCl% vg_bSetDefCl, &Caps Lock
   Gui, 1:Add, Checkbox, xs Checked%g_bSetDefNl% vg_bSetDefNl, &Num Lock
   Gui, 1:Add, Checkbox, xs Checked%g_bSetDefSl% vg_bSetDefSl, &Scroll Lock
    Gui, 1:Add, Text, xs y+20 h1 w400 Border,
    Gui, 1:Font, Underline Bold
   Gui, 1:Add, Text, xs y+20,Caps Lock options
    Gui, 1:Font, Norm
   Gui, 1:Add, Text, xs w400,All Caps Lock options can be used at the same time, although it can slow down Caps Lock performance
   Gui, 1:Add, Checkbox, xs y+8 Checked%g_bSetMultiCaps% vg_bSetMultiCaps, Force multi-pressing Caps Lock (more than 1) to toggle state
   Gui, 1:Add, Text, xs+17, Maximum interval between presses:
   Gui, 1:Add, DropDownList, x+8 yp-3 w90 vg_iSetCapsSpeed Choose%v_SetItem%, %v_SetListItems%
   Gui, 1:Add, Text, x+8 yp+3,milliseconds
   Gui, 1:Add, Checkbox, xs y+15 Checked%g_bSetCaps% vg_bSetCaps, Give warning if Caps &Lock key is down
   Gui, 1:Add, Checkbox, xs+17 Checked%g_bSetCapsPlay% vg_bSetCapsPlay, Add sound to warning
    Gui, 1:Font, Underline Bold
   Gui, 1:Add, Text, xs y+20,Insert options
    Gui, 1:Font, Norm
   Gui, 1:Add, Checkbox, xs Checked%g_bSetInsert% vg_bSetInsert,Give warning if &Insert key is pressed
   Gui, 1:Add, Checkbox, xs+17  Checked%g_bSetInsertPlay% vg_bSetInsertPlay, Add sound to warning
   Gui, 1:Add, Text, xs h20,
   Gui, 1:Add, Button, ym+17 w75 Default Section gSettingsOk, &OK
   Gui, 1:Add, Text, x+0 w20
   Gui, 1:Add, Button, xs w75 gSettingsEsc, &Cancel
   Gui, 1:Show, Center, Winkiez LockKing Settings
Return

SettingsOk:
   Gui, 1:Submit
;    FileDelete, %AppName%.ini
   IniWrite,%g_bSetDefCl%,%AppName%.ini,Settings,SetDefCl
   IniWrite,%g_bSetDefNl%,%AppName%.ini,Settings,SetDefNl
   IniWrite,%g_bSetDefSl%,%AppName%.ini,Settings,SetDefSl
   IniWrite,%g_bSetDefPlay%,%AppName%.ini,Settings,SetDefPlay
   IniWrite,%g_bSetInsert%,%AppName%.ini,Settings,SetInsert
   IniWrite,%g_bSetInsertPlay%,%AppName%.ini,Settings,SetInsertPlay
   IniWrite,%g_bSetMultiCaps%,%AppName%.ini,Settings,SetMultiCaps
   IniWrite,%g_iSetCapsSpeed%,%AppName%.ini, Settings, SetCapsSpeed
   IniWrite,%g_bSetCaps%,%AppName%.ini,Settings,SetCaps
   IniWrite,%g_bSetCapsPlay%,%AppName%.ini,Settings,SetCapsPlay
   Gui, 1:Destroy
   Gosub, NowTrayTip
Return

SettingsEsc:
   Gui, 1:Destroy
Return

DefaultSettings:
   FileDelete, %AppName%.ini
   Gosub, ReadIni
Return

; *** TrayMenu Specific Subroutines ***
TrayMenu:
   Menu, Tray, NoStandard 
   Menu, Tray, Add, &LockKing Info, About
   Menu,Tray,Default, &LockKing Info
   Menu, Tray, Add ; separator   
   Menu, Tray, Add, &Settings, GuiSettings
   Menu, Tray, Add, Set &Default Settings, DefaultSettings
   Menu, Tray, Add ; separator
   Menu, Tray, Add, Toggle &Caps Lock, ToggleCapsLock
   Menu, Tray, Add, Toggle &Num Lock, ToggleNumLock
   Menu, Tray, Add, Toggle &Scroll Lock, ToggleScrollLock
   Menu, Tray, Add ; separator   
   Menu, Tray, Add, &Reload, AhkReload
   Menu, Tray, Add, &Pause, AhkPause
   Menu, Tray, Add, E&xit, AhkExit
Return

NowTrayTip:
   If g_bClState
   {
      v_State1 = %g_On%
   }
   Else
   {
      v_State1 = %g_Off%
   }
   If g_bNlState
   {
      v_State2 = %g_On%
   }
   Else
   {
      v_State2 = %g_Off%
   }
   If g_bSlState
   {
      v_State3 = %g_On%
   }
   Else
   {
      v_State3 = %g_Off%
   }
   If g_bSetMultiCaps
      v_State4 = %g_On%
   Else
      v_State4 = %g_Off%
      
   If g_bSetCaps
      v_State5 = %g_On%
   Else
      v_State5 = %g_Off%
      
   If g_bSetInsert
      v_State6 = %g_On%
   Else
      v_State6 = %g_Off%
   
   v_TrayText = Caps Lock: %v_State1%`n
   v_TrayText = %v_TrayText%Num Lock: %v_State2%`n
   v_TrayText = %v_TrayText%Scroll Lock: %v_State3%`n`n
   v_TrayText = %v_TrayText%Caps Lock multi pressing: %v_State4%`n
   v_TrayText = %v_TrayText%Caps Lock key warning: %v_State5%`n
   v_TrayText = %v_TrayText%Insert key warning: %v_State6%
   Menu, Tray, Tip, %v_TrayText%
Return

AhkReload:
   Reload
Return

AhkPause:
   Suspend
   Pause
Return

AhkExit:
   Exitapp

; *** Info Dialog ***
About:
   Gui, 2:Destroy
   Gui, 2:Font, Bold
   Gui, 2:Add, Text, xm+20 y+20 w320, Winkiez LockKing is a small utility that shows the current state of the Lock keys and has some other options
   Gui, 2:Font
   Gui, 2:Add, Text, xm+20  y+10 Section, Version:
   Gui, 2:Add, Text, xs, Author:
   Gui, 2:Add, Text, xs, Website:
   Gui, 2:Add, Text, xs, Made with:
   Gui, 2:Add, Text, ys Section,1.1
   Gui, 2:Add, Text, xs, Winkie
   Gui, 2:Font, cBlue Underline
   Gui, 2:Add, Text, xs gLink1, http://file.autohotkey.net/Winkie/
   Gui, 2:Add, Text, xs gLink2, AutoHotkey`nhttp://www.autohotkey.com/
   Gui, 2:Font
   Gui, 2:Font, Bold Underline
   Gui, 2:Add, Text, xm+20 y+20 Section, A short description how the Lock states are shown:
   Gui, 2:Font
   Gui, 2:Add, Text, xs,(Other combinations are a diversion of these two)
   Gui, 2:Add, Picture, xs h32 w32 Icon1, lockking.dll
   Gui, 2:Add, Text, x+8, - Capital N: Caps Lock is On`n- Blue background: Num Lock is On`n- Arrows: Scroll Lock is On
   Gui, 2:Add, Picture, xs y+10 h32 w32 Icon8, lockking.dll
   Gui, 2:Add, Text, x+8, - Small n: Caps Lock is Off`n- Black background: Num Lock is Off`n- No arrows: Scroll Lock is Off
   Gui, 2:Add, Text, xs h20,
   Gui, 2:Add, Button, ym+17 w90 Section Default gAboutOk, &OK
   Gui, 2:Add, Text, x+0 w20
   Gui, 2:Add, Button, xs w90 gAboutSettings, &Settings...
   Gui, 2:Show, Center, Winkiez LockKing Info

;Load the cursor and start the "hook" 
hCurs:=DllCall("LoadCursor","UInt",NULL,"Int",32649,"UInt") ;IDC_HAND 
OnMessage(0x200,"WM_MOUSEMOVE") 
Return

Link1:
   Run, http://www.autohotkey.com/,,UseErrorLevel 
Return

Link2:
   Run, http://file.autohotkey.net/Winkie/,,UseErrorLevel
Return

AboutOk:
   Gui, 2:Destroy
   ;Disable the "hook" and destroy the cursor 
   OnMessage(0x200,"") 
   DllCall("DestroyCursor","Uint",hCurs) 
Return

AboutSettings:
   Gosub, AboutOk
   Gosub, GuiSettings
Return

; Function for links on Gui (the "hook")
WM_MOUSEMOVE(wParam,lParam) 
{ 
  Global hCurs 
  MouseGetPos,,,,ctrl 
  ;Only change over certain controls
  If ctrl in Static7,Static9
    DllCall("SetCursor","UInt",hCurs)
  Return 
}
; *** End Script ***

Back to information page