我想监视运行 X 的 Linux 系统上哪个窗口处于活动状态,以及该活动窗口何时调整大小或移动。我可以监视活动窗口(它位于_NET_ACTIVE_WINDOW
根窗口上的属性,我可以注册PropertyNotify
根窗口上的事件以发现该属性何时发生更改)。但是,我不知道如何监视活动窗口以了解它是否调整大小或移动。
import Xlib
import Xlib.display
disp = Xlib.display.Display()
Xroot = disp.screen().root
NET_ACTIVE_WINDOW = disp.intern_atom('_NET_ACTIVE_WINDOW')
Xroot.change_attributes(event_mask=Xlib.X.PropertyChangeMask)
while True:
# loop until an event happens that we care about
# we care about a change to which window is active
# (NET_ACTIVE_WINDOW property changes on the root)
# or about the currently active window changing
# in size or position (don't know how to do this)
event = disp.next_event()
if (event.type == Xlib.X.PropertyNotify and
event.atom == NET_ACTIVE_WINDOW):
active = disp.get_input_focus().focus
try:
name = active.get_wm_class()[1]
except TypeError:
name = "unknown"
print("The active window has changed! It is now", name)
有没有办法做到这一点?它可能涉及监听当前活动窗口上的ConfigureNotify事件(并调用change_attributes
当它变得活跃时在该窗口上设置适当的掩码),但我无法让它工作。
(注意:我没有使用 Gtk,所以请不要提供 Gtk 解决方案。)
更新:有一种相当可疑的方法来检测窗口resizes,通过观察活动窗口的值_NET_WM_OPAQUE_REGION
属性更改(因为我正确接收了 PropertyChange 事件,尽管我没有接收到ConfigureNotify 事件)。但是,尚不清楚所有窗口管理器都设置此属性,并且此属性仅在窗口大小调整时发生变化;它不会随着窗口移动而改变(任何其他属性也不会改变)。
执行此操作的方法是选择SubstructureNotifyMask
在根窗口上,然后阅读全部ConfigureNotify
事件并忽略那些不适合我们关心的窗口的事件,因此:
import Xlib
import Xlib.display
disp = Xlib.display.Display()
Xroot = disp.screen().root
NET_ACTIVE_WINDOW = disp.intern_atom('_NET_ACTIVE_WINDOW')
Xroot.change_attributes(event_mask=Xlib.X.PropertyChangeMask |
Xlib.X.SubstructureNotifyMask)
windows = []
while True:
# loop until an event happens that we care about
# we care about a change to which window is active
# (NET_ACTIVE_WINDOW property changes on the root)
# or about the currently active window changing
# in size or position (ConfigureNotify event for
# our window or one of its ancestors)
event = disp.next_event()
if (event.type == Xlib.X.PropertyNotify and
event.atom == NET_ACTIVE_WINDOW):
active = disp.get_input_focus().focus
try:
name = active.get_wm_class()[1]
except TypeError:
name = "unknown"
print("The active window has changed! It is now", name)
# Because an X window is not necessarily just what one thinks of
# as a window (the window manager may add an invisible frame, and
# so on), we record not just the active window but its ancestors
# up to the root, and treat a ConfigureNotify on any of those
# ancestors as meaning that the active window has been moved or resized
pointer = active
windows = []
while pointer.id != Xroot.id:
windows.append(pointer)
pointer = pointer.query_tree().parent
elif event.type == Xlib.X.ConfigureNotify and event.window in windows:
print("Active window size/position is now", event.x, event.y,
event.width, event.height)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)