Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Context Notifier using Messages to contact Listeners


  • Please log in to reply
No replies to this topic
baby-luck
  • Members
  • 15 posts
  • Last active: Aug 10 2006 08:06 PM
  • Joined: 25 Apr 2006
Hi,

I have to say this was a little bit of an adventure and you may or may not find it useful.

Lots of people mentioned timers and how they check which window/control the mouse is over to give context to the hotkeys. Some mentioned how they might need several of these timers in different scripts running at the same time and their possible effect on CPU load. And there's some code duplication, albeit tiny.
Checking context information at some interval and performing some action depending on the mouse context can be achieved with just a few lines in a script.
One of several examples in the forums: shimanov in http://www.autohotke...l&highlight=cpu
SetTimer, timer_WindowMonitor, 1000
return
timer_WindowMonitor:
ifWinActive, Untitled - Notepad
	Suspend, Off
else
	Suspend, On
return

So I thought 'why use 8 lines when a few hundred will do?' and built a notifying context sniffer.

It runs as a separate script that discovers mouse context information and sends this information to other scripts registered as listeners who can then act in response to the context.
For example I am currently using the context notifier with my mouse gesture script (which I'll post soon):
Every 100ms the context script checks various context info (like the ID of the window the mouse is over) and sends this info to the gesture script. The gesture script receives this info and updates its own 'Suspend' state - turning suspend off (active) when the mouse is over an Explorer window and turning suspend on otherwise.Same with a Tooltip help script I use:
The same context script sends context info to this help script. On each occasion the help script merely lets its copy of the context variables get updated. Then whenever I press CapsLock the help script checks its 'mouseover window class' variable to see what type of area/window the mouse is over, and then shows the appropriate help file.
Context info currently returned: Active window ID, Mouseover window ID, Mouseover window class, Mouseover window title, Mouseover window control classnn

To hook a script into the context info, in the including script you need to include the code:
;Include the script to talk to the bl-context.ahk script which maintains context state based on the mouse position
#Include bl-context-include.ahk
;Start the context listening
bl_context_start()
If you need to, also set the initial state:
;Start suspended, if something breaks then at least the invasive gesture listening won't be running
Suspend, On
Then also you must define the function "bl_context_update" which will be called by the context script (via the include) each time the context is updated (at an interval).
For example my mouse gesture script that simple toggles suspension of the script itself:
;Suspend/enable/disable certain commands or whatever based on mouse context
bl_context_update() {
	global bl_context_mousewinclass
	if (bl_context_mousewinclass = "CabinetWClass" || bl_context_mousewinclass = "ExploreWClass")
		Suspend, Off
	else
		Suspend, On
}
That's it. When the context script is started it scans for any AHK scripts and sends each a message, which, if they have the include, they'll respond to with an ID to register as a 'listener' with.
Additionally, if a script is started which uses the include, the include will try for 5 seconds to contact the 'boss' to register itself using its ahk_id.

I think I have commented reasonably liberally and the scripts aren't too long so I suggest reading them for a clearer understanding, and of course the definitive documentation is the code itself :) There are also a few other functions in the include that the including script can call. Most of the script's message sending code is just a modified version of the manual's OnMessage() docs.

https://ahknet.autoh...com/~baby-luck/ is where you can find a zip of the files. I have included the simple Tooltip help script which uses the context script as an example - there's a readme to explain.

Anyway, it works but you'd really have to be *into* it to use it instead of 8 lines.

:)