How to know which window has focus and how to change it?

前端 未结 4 1847
太阳男子
太阳男子 2020-12-05 15:17

I would like to know how can I ask X11 which windows has focus. And if for any reason my own application (that may be visible or not) got the focus I want be able to let the

4条回答
  •  心在旅途
    2020-12-05 15:57

    I recommend an application called XDoTool. It supports quite a lot of queries, controls, and even hooks.

    > xdotool getwindowfocus               # 29360135
    > xdotool getwindowfocus getwindowpid  # 12988
    > xdotool getwindowfocus getwindowname # tilda
    > xdotool getwindowfocus behave '%@' blur getmouselocation
    #      or focus, mouse-enter, etc.
    x:514 y:317 screen:0 window:56623121
    x:271 y:26 screen:0 window:56623121
    ...
    

    Commands like behave accept a callback, which can be built-in like getmouselocation or external like exec notify-send 'focused window', exec zsh myscript.zsh, etc., however you want to use it.

    Edit - you can focus using xdotool windowfocus [options] [window], as in xdotool search --class firefox windowfocus. In my case this causes errors because Firefox shows up as a couple dozen 'windows', but all have the same PID; it works given the right ID. Hopefully that's a start.

    Edit 2 - the 'window ID' is the decimal representation of the window pointer, e.g. from xprop:

    > xprop -root _NET_ACTIVE_WINDOW
    _NET_ACTIVE_WINDOW(WINDOW): window id # 0x1c00007, 0x0
    > xdotool getwindowfocus
    29360135
    > printf '%d\n' '0x1c00007'
    29360135
    

提交回复
热议问题