Play a sound when typing capital letters

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
trueo
Posts: 4
Joined: 18 Oct 2023, 14:43

Play a sound when typing capital letters

18 Oct 2023, 15:10

Hi, I'm looking, but I can't find a script that would detect the activity of the CAPS LOCK button and when entering any capital letter from A to Z, it would play the selected sound (path to the sound on the computer).
To recap:
1. CAPS LOCK is turned on - each time you enter any letter from A to Z, a sound is played.
2. CAPS LOCK is turned off - every time you enter any letter from a to z, no sound is played.
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: Play a sound when typing capital letters

18 Oct 2023, 18:48

This does not pay attention to Capslock, it just checks to see if the keyboard input is a capitalized ascii character. Do you need it to be specifically dependent upon the Capslock toggle state, as you stated? It can be tweaked to do that instead if needed. You should choose an audio clip that is very brief in duration/length.

This is AHKv1 which is the forum version where you posted your request.

Code: Select all

#Requires AutoHotkey 1.1.36+
#SingleInstance force
#Persistent
ih := InputHook("V"), ih.OnChar := Func("_OnChar"), ih.start()
return
^esc::ExitApp ; Ctrl+esc to end script
_OnChar(ih,char)
{
		if (InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", char, true))
			SoundPlay % "C:\Windows\Media\Windows Information Bar.wav"	; Win7 standard audio clip
			; Soundbeep, 1500, 50  ; alternative
}
trueo
Posts: 4
Joined: 18 Oct 2023, 14:43

Re: Play a sound when typing capital letters

19 Oct 2023, 09:15

Thank you very much, but for now there is something wrong with the script.
After pasting the script, the script doesn't seem to work. Other scripts work fine. Only when I press CTRL + ESC, the entire autohotkey with all Skype scripts closes completely (the icon next to the clock in the taskbar disappears).
I use Windows 10, Autohotkey 1.1.37.01, Avast Free Antivirus.
When the script is turned on, after some time the cursor gets stuck, typing letters is slow in all applications, and the process is "suspended" in the task manager.
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: Play a sound when typing capital letters

19 Oct 2023, 11:14

Try this one.

I removed the ^esc (which was there to exit the script, not to run it.). Now that it has been removed, you will need to end the script manually using the tray icon.

I also changed the method of inspecting the key that is pressed. It might be faster this way. I'm not sure.

I also removed the soundplay, and just using soundbeep. Playing an audio file could be causing an issue.

InputHook can act a little slow, but it's the most convenient way to watch for "any key" to be pressed (that I know of).

If this one causes issues, maybe someone else has another suggestion or approach.

I edited this on my phone, and am unable to test on computer at the moment.

Code: Select all

#Requires AutoHotkey 1.1.36+
#SingleInstance force
#Persistent
ih := InputHook("V"), ih.OnChar := Func("_OnChar"), ih.start()
return
_OnChar(ih,char)
{
		if(asc(char)>=65 and asc(char)<=90) ; A-Z
			Soundbeep, 1500, 50  ; alternative
}
trueo
Posts: 4
Joined: 18 Oct 2023, 14:43

Re: Play a sound when typing capital letters

19 Oct 2023, 12:45

Unfortunately, there is no sound when clicking from A to Z. Of course, the script is pasted, reloaded and the remaining scripts work. The speakers work. ;)
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: Play a sound when typing capital letters

19 Oct 2023, 14:10

trueo wrote:
19 Oct 2023, 12:45
Unfortunately, there is no sound when clicking from A to Z. Of course, the script is pasted, reloaded and the remaining scripts work. The speakers work. ;)
Ok... I'm not sure what the trouble is... I just tested it on my Win10 system, it works for me.

SoundBeep and SoundPlay are both very slow (like several hundredths of a second to execute), but I don't know of an alternative for the sound. Make sure that the SoundBeep is the first line following the if statement (must be directly under it, otherwise it will not execute). Or you can enclose it in braces, like this:

Code: Select all

if (InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", char, true))
{
	Soundbeep
}
Maybe someone else can offer a better solution.
Last edited by andymbody on 19 Oct 2023, 14:35, edited 1 time in total.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Play a sound when typing capital letters

19 Oct 2023, 14:35

Script works fine here.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], macromint, peter_ahk, Rauvagol, Spawnova, wineguy and 284 guests