Script oftens fails to respond to key press

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kiwichick
Posts: 169
Joined: 21 Jan 2014, 22:03

Script oftens fails to respond to key press

Post by kiwichick » 04 Sep 2022, 17:20

I'm running an up-to-date Windows 10 Pro. I have a script I found online that toggles an icon in the notification area to show whether Caps Lock is on or off. Unfortunately it has a problem where it regularly fails to respond to the Caps Lock key press and doesn't change the icon. Then I have to click the icon with my mouse to 'manually' change the icon so it starts working again. There's nothing in particular I do that causes it to stop working. I can press the Caps Lock key and it works, do nothing except press the Caps Lock key again and it doesn't work. Very random. I've tried running it as a script and as a compiled exe but it does it in both cases. Does anyone have any idea why this happens? Here's the code for the script:

Code: Select all

#Warn,UseUnsetLocal 
#NoEnv 
#SingleInstance Force 
SetBatchLines,-1 

Gosub,InitializeVars 
Gosub,ConfigureInitialTray 

CapsLock:: 
KeyWait,CapsLock 
Gosub,ToggleCapsLock 
Return

InitializeVars:
TrayIconON:=A_ScriptDir . "\CapsLockON.ico" 
TrayIconOFF:=A_ScriptDir . "\CapsLockOFF.ico" 
StartupLink:=A_Startup . "\CapsLockOSD.lnk" 
StartupLink:=A_StartupCommon . "\CapsLockOSD.lnk" 
CapsLockIcon:=A_ScriptDir . "\CapsLockOSD.ico" 
FileInstall,CapsLockON.ico,%TrayIconON% 
FileInstall,CapsLockOFF.ico,%TrayIconOFF% 
FileInstall,CapsLockOSD.ico,%CapsLockIcon% 
Return

ConfigureInitialTray:
Menu,Tray,NoStandard 
Menu,Tray,Add,&Toggle CapsLock,ContextMenu 
Menu,Tray,Add,&Start with Windows,ContextMenu 
Menu,Tray,Add,E&xit,ContextMenu 
Menu,Tray,Default,&Toggle CapsLock 
Menu,Tray,Click,1 
Gosub,GetKeyStateIconTip 
Menu,Tray,Icon,%TrayIcon% 
Menu,Tray,Tip,%TrayTip% 
If (FileExist(StartupLink)) 
  Menu,Tray,Check,&Start with Windows 
Else
  Menu,Tray,Uncheck,&Start with Windows 
Return

GetKeyStateIconTip:
If (GetKeyState("CapsLock","T")) 
{
  TrayIcon:=TrayIconON 
  TrayTip:="CapsLock ON" 
}
Else
{
  TrayIcon:=TrayIconOFF 
  TrayTip:="CapsLock OFF" 
}
Return

ToggleCapsLock:
If (GetKeyState("CapsLock","T")) 
{
  SetCapsLockState,Off 
  TrayIcon:=TrayIconOFF 
  TrayTip:="CapsLock OFF" 
}
Else
{
  SetCapsLockState,On  
  TrayIcon:=TrayIconON 
  TrayTip:="CapsLock ON" 
}
Menu,Tray,Icon,%TrayIcon% 
Menu,Tray,Tip,%TrayTip% 
Return

ContextMenu:
If (A_ThisMenuItem="&Toggle CapsLock") 
{
  Gosub,ToggleCapsLock 
  Return
}
If (A_ThisMenuItem="&Start with Windows") 
{
  If (FileExist(StartupLink)) 
  {
    
    Menu,Tray,Uncheck,&Start with Windows 
    FileDelete,%StartupLink% 
    Return
  }
  Else
  {
    
    Menu,Tray,Check,&Start with Windows 
    FileCreateShortcut,%A_ScriptFullPath%,%StartupLink%,,,CapsLockOSD,%CapsLockIcon% 
    If (ErrorLevel!=0) 
    {
      MsgBox,4112,Fatal Error,Error code=%ErrorLevel% trying to create shortcut to:`n%A_ScriptName%`nThis should never happen.
      ExitApp
    }
    Return
  }
}
If (A_ThisMenuItem="E&xit") 
{
  MsgBox,4388,Terminate CapsLockOSD?,Are you sure you want to quit? 
  IfMsgBox,No
    Return 
}
ExitApp 


jbearnolimits
Posts: 58
Joined: 09 Jul 2017, 23:44

Re: Script oftens fails to respond to key press

Post by jbearnolimits » 04 Sep 2022, 20:23

I don't see any areas for the code to sleep. Sometimes a script can be too fast for the computer to keep up. As an example of I wrote:

Send, this is my text

It may only actually send this is my txt.

So you need to slow it down at that point. Maybe add a setkeydelay, 700.

kiwichick
Posts: 169
Joined: 21 Jan 2014, 22:03

Re: Script oftens fails to respond to key press

Post by kiwichick » 05 Sep 2022, 16:25

jbearnolimits wrote:
04 Sep 2022, 20:23
I don't see any areas for the code to sleep. Sometimes a script can be too fast for the computer to keep up. As an example of I wrote:

Send, this is my text

It may only actually send this is my txt.

So you need to slow it down at that point. Maybe add a setkeydelay, 700.
Sorry but I don't understand - why sleep is necessary - or how the script can be too fast for the computer. Could you please elaborate so I have a better understanding of what's happening?

jbearnolimits
Posts: 58
Joined: 09 Jul 2017, 23:44

Re: Script oftens fails to respond to key press

Post by jbearnolimits » 02 Nov 2022, 20:06

Sorry it took so long to respond. I've run into times that the script will attempt to, lets say send the letter a. And I also want the next thing it sends to be the letter b. What I've notice happens at times is it will skip a and just send b. I discovered this is because it is running so fast that the computer can't keep up with the commands. Thus it skips something at times. I know it's odd. It's just what I have found to happen. So setting a sleep timer between actions gives the computer a moment to process the information before another command is sent. I'll often set a sleep timer for 500. It's just half a second, but it helps.

Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Script oftens fails to respond to key press

Post by Lepes » 03 Nov 2022, 03:16

I use this one and it works, you can compare the code if you want: viewtopic.php?t=62611

Post Reply

Return to “Ask for Help (v1)”