Autohotkey script Triggered by sound alert on windows?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Native W

Autohotkey script Triggered by sound alert on windows?

Post by Native W » 13 Feb 2017, 20:26

hey guys,

Can anyone tell me if it is possible to use alert sounds from windows to trigger my AUTOHOTKEY script ?
I mean, every time windows plays that a sound alert Would start a previously programmed command sequence

Thank you in advance
Cheers

qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Autohotkey script Triggered by sound alert on windows?

Post by qwerty12 » 14 Feb 2017, 03:09

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
#Include *i <Acc>

OnAlertSound(hWinEventHook, event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime) {
	Critical
	MsgBox % ((ofe := Func("Acc_ObjectFromEvent"))) ? ofe.call(_idChild_, hWnd, idObject, idChild).accName : "If you had the Acc library in a standard AHK library folder, you would be seeing the name of the program event here instead"
	Critical Off
}

AtExit()
{
	global hWinEventHook, lpfnWinEventProc
	if (hWinEventHook)
		DllCall("UnhookWinEvent", "Ptr", hWinEventHook), hWinEventHook := 0
	if (lpfnWinEventProc)
		DllCall("GlobalFree", "Ptr", lpfnWinEventProc, "Ptr"), lpfnWinEventProc := 0	
	return 0
}

OnExit("AtExit")
hWinEventHook := DllCall("SetWinEventHook", "UInt", EVENT_SYSTEM_SOUND := 0x0001, "UInt", EVENT_SYSTEM_SOUND, "Ptr", 0, "Ptr", (lpfnWinEventProc := RegisterCallback("OnAlertSound", "")), "UInt", 0, "UInt", 0, "UInt", WINEVENT_OUTOFCONTEXT := 0x0000 | WINEVENT_SKIPOWNPROCESS := 0x0002)
Use Acc.ahk if you want the name of the event relating to the sound played

EDIT: By "sound alert", I assume you mean the standard Windows sounds played when something happens (like Chrome annoyingly calling Beep() when it can't find something on the page). If you mean being notified when an application plays any arbitrary sound, good luck with that. You might be able to rig something up with the Core Audio API to watch the meter for a specific application, but I'm not interested in finding out how to do that

9board6
Posts: 1
Joined: 13 Feb 2017, 19:50

Re: Autohotkey script Triggered by sound alert on windows?

Post by 9board6 » 20 Feb 2017, 20:46

thanks man.
and yes, it would be a pre-defined sound, i can setup anyone I want to
the application is a stock scanner (trade ideas) and it plays a sound alert whenever it finds a stock respecting the filters of my trading strategy
i thought something like that, the audio meter on microsoft windows main taskbar. if is silence = do nothing, if it plays any sound = triggers my autohotkey script


Post Reply

Return to “Ask for Help (v1)”