could you please help me to optimize this script? regarding monitoring "CapsLock" key status. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jsjcjsjc
Posts: 56
Joined: 25 May 2019, 03:11

could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

19 Apr 2021, 08:56

my purpose is AHK can display the status of "CapsLock" key on the screen.
the problem of this script is , if you change "CapsLock" key very fast, the script will the failure
Thanks in advance

Code: Select all

SetCapsLockState, Off
width := A_ScreenWidth/2
height := A_ScreenHeight - 300
 
~*CapsLock::
 
Sleep, 100
if GetKeyState("CapsLock", "T")
{
	Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, A
	Sleep, 1000
	Progress off
}
else
{
	Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, a
	Sleep, 1000
	Progress off
}
 
return
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

19 Apr 2021, 09:25

Code: Select all

SetCapsLockState, Off
Gui, -Caption +AlwaysOnTop
Gui, Font, s60 w700
Gui, Color, 5A5A5A
Gui, Add, Text, x78 y10 cFFFFFF Center vlower, a
Gui, Add, Text, x78 y20 cFFFFFF Center vupper, A

~*CapsLock::SetTimer, ShowCaps, -400
ShowCaps:
GuiControl, % GetKeyState("CapsLock", "T") ? "Show" : "Hide", upper
Gui, Show, % "w200 h120 x" A_ScreenWidth / 2 " y" A_ScreenHeight - 300
Sleep, 1000
Gui, Hide
Return
Last edited by mikeyww on 19 Apr 2021, 09:28, edited 1 time in total.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

19 Apr 2021, 09:27

Another option...have it monitor the state and display when the state changes rather than be triggered by keypresses. Allows for rapid presses of the CapsLock key and keeps up with and doesn't interfere with the changes.

Code: Select all

#Persistent
SetCapsLockState, Off
displayedState := "off"
width := A_ScreenWidth/2
height := A_ScreenHeight - 300
SetTimer, Monitor, 100
return

Monitor:
	if GetKeyState("CapsLock", "T") && (displayedState = "off")
	{
		displayedState := "on"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, A
		SetTimer, ProgressOff, -1000
	}
	else if !GetKeyState("CapsLock", "T") && (displayedState = "on")
	{
		displayedState := "off"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, a
		SetTimer, ProgressOff, -1000
	}
	OldState := State
return

ProgressOff:
	Progress, Off
return
jsjcjsjc
Posts: 56
Joined: 25 May 2019, 03:11

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

19 Apr 2021, 18:16

mikeyww wrote:
19 Apr 2021, 09:25

Code: Select all

SetCapsLockState, Off
Gui, -Caption +AlwaysOnTop
Gui, Font, s60 w700
Gui, Color, 5A5A5A
Gui, Add, Text, x78 y10 cFFFFFF Center vlower, a
Gui, Add, Text, x78 y20 cFFFFFF Center vupper, A

~*CapsLock::SetTimer, ShowCaps, -400
ShowCaps:
GuiControl, % GetKeyState("CapsLock", "T") ? "Show" : "Hide", upper
Gui, Show, % "w200 h120 x" A_ScreenWidth / 2 " y" A_ScreenHeight - 300
Sleep, 1000
Gui, Hide
Return
thanks for your replay, but if you change the key very fast, it will be failed
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

19 Apr 2021, 18:32

jsjcjsjc wrote: if you change the key very fast, it will be failed
Did you try the version I posted? It allows for rapid changing of the CapsLock state and will correctly display its current state.
jsjcjsjc
Posts: 56
Joined: 25 May 2019, 03:11

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

19 Apr 2021, 21:15

boiler wrote:
19 Apr 2021, 18:32
jsjcjsjc wrote: if you change the key very fast, it will be failed
Did you try the version I posted? It allows for rapid changing of the CapsLock state and will correctly display its current state.
your version is great,it works !!! Thank you so much for your kind help ;-)
I mean mikeyww's code will failed if changing status very fast

and one more question, is it possible to make a fade effect for the image windows.
the image with "A"/"a" will fade away
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

19 Apr 2021, 21:56

Try this:

Code: Select all

#Persistent
SetCapsLockState, Off
displayedState := "off"
width := A_ScreenWidth/2
height := A_ScreenHeight - 300
SetTimer, Monitor, 100
return

Monitor:
	if GetKeyState("CapsLock", "T") && (displayedState = "off")
	{
		displayedState := "on"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, A
		gosub, FadeOut
	}
	else if !GetKeyState("CapsLock", "T") && (displayedState = "on")
	{
		displayedState := "off"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, a
		gosub, FadeOut
	}
	OldState := State
return

FadeOut:
	Sleep, 200
	loop, 25 {
		WinSet, Transparent, % 255 - 10 * A_Index, ahk_class AutoHotkey2
		Sleep, 10
	}
	Progress, Off
return
jsjcjsjc
Posts: 56
Joined: 25 May 2019, 03:11

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

20 Apr 2021, 06:40

boiler wrote:
19 Apr 2021, 21:56
Try this:

Code: Select all

#Persistent
SetCapsLockState, Off
displayedState := "off"
width := A_ScreenWidth/2
height := A_ScreenHeight - 300
SetTimer, Monitor, 100
return

Monitor:
	if GetKeyState("CapsLock", "T") && (displayedState = "off")
	{
		displayedState := "on"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, A
		gosub, FadeOut
	}
	else if !GetKeyState("CapsLock", "T") && (displayedState = "on")
	{
		displayedState := "off"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, a
		gosub, FadeOut
	}
	OldState := State
return

FadeOut:
	Sleep, 200
	loop, 25 {
		WinSet, Transparent, % 255 - 10 * A_Index, ahk_class AutoHotkey2
		Sleep, 10
	}
	Progress, Off
return

Thanks again,it is great!
Compared to the previous version, the only problem is if changing the key very fast, it looks not very smooth.
so I copied your code and made this one ;-)

Code: Select all

#Persistent
SetCapsLockState, Off
displayedState := "off"
width := A_ScreenWidth/2
height := A_ScreenHeight - 300
SetTimer, Monitor, 100
return

Monitor:
	if GetKeyState("CapsLock", "T") && (displayedState = "off")
	{
		displayedState := "on"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, A
		SetTimer, ProgressOff, -1000
	}
	else if !GetKeyState("CapsLock", "T") && (displayedState = "on")
	{
		displayedState := "off"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, a
		SetTimer, ProgressOff, -1000
	}
	OldState := State
return

ProgressOff:
	Sleep, 1500
	loop, 25 {
		WinSet, Transparent, % 255 - 10 * A_Index, ahk_class AutoHotkey2
		Sleep, 10
	}
	Progress, Off
return

Oh, NO
after testing, there is a bug...
in ProgressOff, the sleep time is 1500, so if changing the key at 1200, the new key's status only stay on the screen 300...it's too fast
could you please give me some advise ?
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

20 Apr 2021, 09:15

It's more complicated if you want to have the fade out and still want it to react immediately. It could be done, but it would get pretty complex.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.  Topic is solved

20 Apr 2021, 09:29

This seems to do what you want. Less complex than I thought it might have to be.

Code: Select all

#Persistent
SetCapsLockState, Off
duration := 500 ; numbers higher than 255 keep it solid longer before starting to fade
displayedState := "off"
width := A_ScreenWidth/2
height := A_ScreenHeight - 300
SetTimer, Monitor, 100
return

Monitor:
	if GetKeyState("CapsLock", "T") && (displayedState = "off")
	{
		displayedState := "on"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, A
		opacity := duration
		SetTimer, FadeOut, 30
	}
	else if !GetKeyState("CapsLock", "T") && (displayedState = "on")
	{
		displayedState := "off"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, a
		opacity := duration
		SetTimer, FadeOut, 30
	}
return

FadeOut:
	WinSet, Transparent, % Opacity, ahk_class AutoHotkey2
	opacity -= 25
	if (opacity < 10) {
		SetTimer, FadeOut, Off
		Progress, Off
	}
return
jsjcjsjc
Posts: 56
Joined: 25 May 2019, 03:11

Re: could you please help me to optimize this script? regarding monitoring "CapsLock" key status.

20 Apr 2021, 18:10

boiler wrote:
20 Apr 2021, 09:29
This seems to do what you want. Less complex than I thought it might have to be.

Code: Select all

#Persistent
SetCapsLockState, Off
duration := 500 ; numbers higher than 255 keep it solid longer before starting to fade
displayedState := "off"
width := A_ScreenWidth/2
height := A_ScreenHeight - 300
SetTimer, Monitor, 100
return

Monitor:
	if GetKeyState("CapsLock", "T") && (displayedState = "off")
	{
		displayedState := "on"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, A
		opacity := duration
		SetTimer, FadeOut, 30
	}
	else if !GetKeyState("CapsLock", "T") && (displayedState = "on")
	{
		displayedState := "off"
		Progress, B1 W200 H120 ZH0 FS80 WS700 x%width% y%height% CTFFFFFF CW5A5A5A, a
		opacity := duration
		SetTimer, FadeOut, 30
	}
return

FadeOut:
	WinSet, Transparent, % Opacity, ahk_class AutoHotkey2
	opacity -= 25
	if (opacity < 10) {
		SetTimer, FadeOut, Off
		Progress, Off
	}
return

YES !! prefect !!!!!
I greatly appreciate your answers and AHK :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], marypoppins_1, OrangeCat, RussF, Spawnova and 119 guests