Help trying to set CapsLock for a specific program Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pixeled4life
Posts: 2
Joined: 18 May 2022, 05:36

Help trying to set CapsLock for a specific program

Post by pixeled4life » 18 May 2022, 05:44

I'm trying to force CapsLock to on whenever I enter a specific window and restore CapsLock to the state it was before when I leave the window

This is the code I'm using:

Code: Select all

targetActive := false
keystate := false

#Persistent
SetTimer, MayusRight, 250
return 

MayusRight:
IfWinActive, ahk_exe notepad.exe
	if ( targetActive == false ){
		targetActive := true
		if ( GetKeyState(CapsLock, "T") == 1 ){
			keystate := true
		} else {
			keystate := false
		}
		SetCapsLockState, AlwaysOn
	}
IfWinNotActive, ahk_exe notepad.exe
	if ( targetActive == true ){
		targetActive := false
		if ( keystate == true ){
			SetCapsLockState, On
		} else {
			SetCapsLockState, Off
		}
	}
return
At the moment this forces CapsLock on when in notepad and then sets it to off regardless of what state it was in when I entered notepad.

I know that code is inefficient and maybe it's something easy I'm not seeing but I would appreciate any help

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Help trying to set CapsLock for a specific program  Topic is solved

Post by mikeyww » 18 May 2022, 06:01

Welcome to this AutoHotkey forum!

Code: Select all

#SingleInstance Force
winTitle = ahk_exe notepad.exe
Loop {
 WinWaitActive, %winTitle%
 original := GetKeyState("CapsLock", "T")
 SetCapsLockState, On
 SoundBeep, 1500
 WinWaitNotActive
 SetCapsLockState, %original%
 SoundBeep, 1000
}

pixeled4life
Posts: 2
Joined: 18 May 2022, 05:36

Re: Help trying to set CapsLock for a specific program

Post by pixeled4life » 18 May 2022, 06:17

Thanky you very much, I thought the WinWaitActive would stop the other hotkeys on that file to stop from working, that's why I didn't use it but it work great ^^

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Help trying to set CapsLock for a specific program

Post by mikeyww » 18 May 2022, 06:21

OK. Your point is well taken and also demonstrates that AutoHotkey is event-driven: you can trigger a hotkey while another routine is running. AHK v1 is single-threaded, so triggering a hotkey will interrupt a running routine. In this specific case (script), however, this interruption would seldom have an adverse effect. When the hotkey routine ends, the loop continues.

Post Reply

Return to “Ask for Help (v1)”