How to detect what monitor/screen a window is on when it comes in focus?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
jonib
Plugin Developer
Posts: 1297
Joined: Thu Mar 26, 2009 9:33 pm
Location: Sweden

How to detect what monitor/screen a window is on when it comes in focus?

Post by jonib » Mon Feb 25, 2019 2:43 pm

On my HTPC I have 2 graphics cards (Intel and Radeon) connected to two different inputs on the same HDTV.

And I would like to change the input on the TV when ever a window gets focus on a different input.

So I need to see which screen a windows is on but I don't see any command in EventGhost that gives me that info, so is there something builtin or in a plugin?

jonib
XBMC2 plugin to control XBMC. If you want to flatter me Image

User avatar
kgschlosser
Site Admin
Posts: 4620
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: How to detect what monitor/screen a window is on when it comes in focus?

Post by kgschlosser » Sat Mar 16, 2019 5:23 pm

This can be done. if you use the Task Monitor + plugin.

The plugin generates an event for when a program gains or loses focus. the event payload is a class instance that has all kinds of properties and functions that can be performed on that specific window. one of them being position.
You can view all of the available methods and properties for the payload here

https://github.com/Boolean263/EventGhos ... dowInfo.py


So you enumerate the displays using eg.WinApi.Utils.GetMonitorDimensions which returns a list of tuples. the list represents the attached displays, index 0 being monitor 1 in Windows. the the tuples at each index contain x, y, width, height. x, y represent the upper left corner of the display and width and height are self explained

code example.

Code: Select all

DISPLAY_NUM = 0
from eg.WinApi.Utils import GetMonitorDimensions


window = eg.event.payload
x, y, = window.GetPositionTuple()

start_x, start_y, width, height = GetMonitorDimensions()[DISPLAY_NUM]

end_x = start_x + width
end_y = start_y + height 

if (start_x <= x <= end_y) and (start_y <= y <= end_y ):
    pass
else:
    eg.StopMacro()
    eg.Exit()
    
create a macro
add the python script action with the code above
add the event for the application that is gaining focus.
then add any other actions that you want to have run if the application is on the display you want.

Change DISPLAY_NUM to the display number less one. so display 1 enter a 0 and display 2 enter a 1. and so on and so forth.

the above is pseudo code. I have not tested it. it should work barring any typos.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4620
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: How to detect what monitor/screen a window is on when it comes in focus?

Post by kgschlosser » Sat Mar 16, 2019 5:33 pm

I didn't really answer your question

Code: Select all

from eg.WinApi.Utils import GetMonitorDimensions

window = eg.event.payload
x, y, = window.GetPositionTuple()

found_displays = []
for i, item in enumerate(GetMonitorDimensions()):
    start_x, start_y, width, height = item
	end_x = start_x + width
	end_y = start_y + height 

	if (start_x <= x <= end_y):
	    found_displays += [i + 1]
	if (start_y <= y <= end_y ):
	    if i + 1 not in found_displays:
	        found_displays += [i + 1]


eg.TriggerEvent(prefix='Application', suffix=window.title + '.Displays',  payload=found_displays[:])
This is going to trigger an event with the payload being a list of display numbers the application is on. the display numbers are going to be the Windows display numbers. If the application spans more then a single display the payload is going to contain more then one number

again pseudo code and untested
If you like the work I have been doing then feel free to Image

jonib
Plugin Developer
Posts: 1297
Joined: Thu Mar 26, 2009 9:33 pm
Location: Sweden

Re: How to detect what monitor/screen a window is on when it comes in focus?

Post by jonib » Sat Mar 16, 2019 5:36 pm

kgschlosser wrote:
Sat Mar 16, 2019 5:23 pm
This can be done. if you use the Task Monitor + plugin.
I'll check the plugin later, thanks.

But I already solved it earlier looking at the code for the Window related actions, I was going to post my solution but wanted to simplify it first, but time flies.

jonib
XBMC2 plugin to control XBMC. If you want to flatter me Image

dan Edens
Posts: 16
Joined: Mon Sep 24, 2018 7:57 pm

Re: How to detect what monitor/screen a window is on when it comes in focus?

Post by dan Edens » Sun Apr 07, 2019 10:23 pm

jonib wrote:
Sat Mar 16, 2019 5:36 pm
kgschlosser wrote:
Sat Mar 16, 2019 5:23 pm
This can be done. if you use the Task Monitor + plugin.
I'll check the plugin later, thanks.

But I already solved it earlier looking at the code for the Window related actions, I was going to post my solution but wanted to simplify it first, but time flies.

jonib
I'm so stoked for this plugin.

Post Reply