Focus window in linux

Changing the Input Focus

When you type on the keyboard, the window manager (page 142) directs the characters you type somewhere, usually to a window. The active window is the window accepting input from the keyboard; it is said to have the input focus. Depending on how you set up your account, you can use the mouse in one of three ways to change the input focus (you can also use the keyboard; see page 116):

• Click-to-focus (explicit focus)—Gives the input focus to a window when you click the window. That window continues to accept input from the keyboard regardless of the location of the mouse pointer. The window loses the focus when you click another window. Although clicking the middle or right mouse button also activates a window, use only the left mouse button for this purpose; other buttons may have unexpected effects when you use them to activate a window.

• Focus-follows-mouse (sloppy focus, enter-only, or focus-under-mouse)— Gives the input focus to a window when you move the mouse pointer onto the window. That window maintains the input focus until you move the mouse pointer onto another window, at which point the new window gets the focus. When you move the mouse pointer off a window and onto the root window, the window that had the input focus does not lose it.

• Focus-strictly-under-mouse (enter-exit focus)—Gives the input focus to a window when you move the mouse pointer onto the window. That window maintains the input focus until you move the mouse pointer off the window, at which point no window has the focus. When you move the mouse pointer off a window and onto the root window, the window that had the input focus loses it, and input from the keyboard is lost.

You can use the Window Preferences window to change the focus policy. Under Fedora, you must install the control-center-extra package as explained on page 501 before you can display this window. To display this window, select Main menu: SystemOPreferences^Windows or give the command gnome-window-properties from a terminal emulator or Run Application window (ALT-F2). Put a tick in the check box next to Select windows when the mouse moves over them to select the focus-follows-mouse policy. When there is no tick in this check box, click-to-focus is in effect. Click Close. Focus-strictly-under-mouse is not available from this window.

To determine which window has the input focus, compare the window borders. The border color of the active window is different from the others or, on a monochrome display, is darker. Another indication that a window is active is that the keyboard cursor is a solid rectangle; in windows that are not active, the cursor is an outline of a rectangle.

Читайте также:  Linux snmp trap receiver

Use the following tests to determine which keyboard focus method you are using. If you position the mouse pointer in a window and that window does not get the input focus, your window manager is configured to use the click-to-focus method. If the border of the window changes, you are using the focus-follows-mouse or focus-strictly-under-mouse method. To determine which of the latter methods you are using, start typing something, with the mouse pointer positioned on the active window. Then move the mouse pointer over the root window and continue typing. If characters continue to appear within the window, you are using focus-follows-mouse; otherwise, you are using focus-strictly-under-mouse.

  • Apropos Searches for a Keyword
  • Customizing Mouse Behavior
  • Installing the Kernel Source Code
  • User ID Mapping Options — Red Hat Enterprise Linux
  • FedoraRHEL Configuration Tools
  • How sendmail Works — Red Hat Enterprise Linux
  • Free Hosting for Your Website
  • Search Engine Traffic Guide
  • Laptop Repair Made Easy
  • Advanced Registry Cleaner PC Diagnosis and Repair
  • Computer Repair Mastery Course

Источник

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

I would like to know how can I ask X11 which windows has the 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 former windows get the focus again. For instance, my application is running with many others (e.g. firefox, gvim, nautilus, . ) Suppose that at first firefox has focus and that the user clicked on my app which now has the focus. I want that my application put the focus on firefox again. Does anyone knows how to achieve this? Books recommendations would be very nice.

4 Answers 4

Take a look at the _NET_ACTIVE_WINDOW value of the root window which is set by most modern window managers:

xprop -root _NET_ACTIVE_WINDOW 

This value can, of course, be obtained using Xlib library calls.

You probably want the XGetInputFocus call.

Window focused; int revert_to; XGetInputFocus(dpy, &focused, &revert_to); 

In this snippet, focused will be the window with current input focus, getting keyboard events and mouse button presses.

This will work even if the window manager does not set the _NET_ACTIVE_WINDOW property on the root window, as specified by EWMH. A few window managers, such as dwm and my 9wm, don’t set this.

He also asked «how to change it», XSetInputFocus can do that, for example: XSetInputFocus(display, window_to_focus, RevertToNone, CurrentTime);

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.

Читайте также:  Postgresql linux зайти в базу

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 

Источник

Bash command to focus a specific window

Is there a way, in bash command line, to give focus to a specific window of a running process. Assume I know the process’ name, number, and anything else I need. For instance, if I have a single instance of Firefox running, but it’s minimized (or there’s some other window on top of it). I need a bash command that brings up and gives focus to the Firefox window, by making it the active window.

5 Answers 5

The wmctrl command seems to do the job. It was already installed for me, but it’s available in the repositories in case anyone needs it.

Lists currently open windows (including the gnome panels).

Gives focus to a window containing STRING in its title. I’m not sure what happens if more than one window meets that condition.
In my case the command was:

This is awesome for setting focus back to gdb (debugger) when it launches a debugger target with a window that steals focus, like kvm. Use gdb command shell wmctrl -a something , where something is something in your debugger terminal title.

Thanks so much, this is pure gold, I was afraid I lost all of my pending work in some Chrome window that just disappeared in the background somehow, it worked!

You could also use xdotool for this: xdotool search —class «class name» windowfocus , or target the window using its name. Find the window info with xev .

Using wmctrl in combination with xdotool you can switch focus to Firefox and then perform keyboard or mouse actions.

wmctrl -R firefox && \ xdotool key --clearmodifiers ctrl+t ctrl+l && \ xdotool type --delay=250 google && \ xdotool key --clearmodifiers Tab Return 

The following steps are executed:

  1. Give focus to the first matching Firefox window
  2. Open a new browser tab
  3. Puts focus in the address bar
  4. Type «google»
  5. Tab to the first browser auto-complete result
  6. Press the Return (or Enter) key

How’s the below script that I use in my ubuntu pc? use case is like this.

 $ ./focus_win.sh 1 # focus on a application window that executed at first $ ./focus_win.sh 2 # second executed application window 

I’m using it after assigning it in keyboard custom shortcut. ctrl+1, ctrl+2, .

#! /bin/sh if [ "" = "$1" ] ; then echo "usage $0 " exit 1; fi WIN_ID=`wmctrl -l | cut -d ' ' -f1 | head -n $1 | tail -n 1` if [ "" = "$WIN_ID" ] ; then echo "fail to get win id of index $1" exit 1; fi wmctrl -i -a $WIN_ID 

Update

Running global.context.unsafe_mode = true in Looking Glass re-enables the functionality, but only temporarily.

Читайте также:  Скопировать строку в линукса

Original answer

On Wayland, sadly wmctrl and xdotool do not function. Instead we can talk to the window manager.

For Gnome, we can run gdbus to send a DBUS message to execute some GJS (JavaScript bindings for the GNOME C APIs).

$ gdbus call \ --session \ --dest org.gnome.Shell \ --object-path /org/gnome/Shell \ --method org.gnome.Shell.Eval " var mw = global.get_window_actors() .map(w=>w.meta_window) .find(mw=>mw.get_title().includes('Firefox')); mw && mw.activate(0)" 

You can play around with what’s possible in GJS using Gnome’s ‘Looking Glass’ debugger: Alt+F2, and run lg

I’m seeing this all over the place. But where exactly do I put gdbus calls? It looks like it’s supposed to just evaluate the GJS. But I’m running it in a shell, from a file, and get no output (error or otherwise) on any of the scripts linked above. I’ve tried to evaluate them through d-feet (org.gnome.Shell.Eval) with: ‘invalid syntax (, line 1)’ error outputs. Separate GJS files evaluate to «global is not defined». Any help? Thank you for being the first person across dozens of threads to consider Wayland, though. Since it runs just fine on looking glass, it’s obviously correct gjs.

in kde, it’s qdbus call. eg: qdbus org.kde.klipper /klipper getClipboardCotents to get clipboard contents. install qdbusviewer and play around.

As another answer already mentioned, wmctrl is a good choice for this task and wmctrl -a WINDOWTITLE allows one to activate the window whose title contains WINDOWTITLE .

However, in many cases the window title does not contain the name of the program used. For this purpose wmctrl also has the -x option that can be used to focus windows by class name.

You can type wmctrl -lx to print all currently open windows, together with their class name. You can use wmctrl -xa NAME to activate a Window whose class name includes NAME .

For example,I had the issue that I wanted to activate the window of the Evince PDF viewer if it was open, and I couldn’t do that from the window name alone as it always only contains the name of the PDF that is viewed. So I used wmctrl -lx and found the line

0x06000007 0 evince.Evince sejmou-desktop 2. User-User Collaborative Filtering 

Where evince.Evince is the window class name mentioned above.

Then, I was able to activate the Evince window programmatically wmctrl -xa Evince . Of course this only works reliably as long as Evince is a unique class name among the windows that are currently open.

Источник

Оцените статью
Adblock
detector