How to prevent new windows from stealing focus?

12,076
  1. Install CompizConfig Settings Manager

    • either from Ubuntu Software Center
    • or via terminal:

      sudo apt-get install compizconfig-settings-manager
      
  2. Open CompizConfig Settings Manager and go to General -> General options -> Focus and raise behaviour

  3. Change the 'Focus prevention level' from 'Low' to 'Normal'.

Share:
12,076

Related videos on Youtube

Imre Major
Author by

Imre Major

Updated on September 18, 2022

Comments

  • Imre Major
    Imre Major over 1 year

    Typical situation is that I type something (worst case is my password) in some application, when some other application pops up a window and steals the focus, so my typing (password) goes to something else. I would like to prevent this, at least when I actively type something in an edit box. I always really hated this on Windows, but I never found a way to prevent this. I started using Ubuntu a while ago, and so far I was able to do everything I wanted, so I thought maybe this is also possible. Thanks in advance!

    • vanadium
      vanadium over 5 years
      Try to change a setting with the terminal command gsettings set org.gnome.desktop.wm.preferences focus-new-windows 'strict'. Let us know if that works in your case. To reset to default, give the command gsettings reset org.gnome.desktop.wm.preferences focus-new-windows
    • Imre Major
      Imre Major over 5 years
      Thanks. With this change the new windows appear in the background, but interstingly the focus is lost. So for example I start typing in the terminal, and meanwhile I start some application with starts up quite slowly. I click on the application on the left side in the favorites, click back to the terminal window to continue typing. In a few seconds the application starts, its window is put behind the terminal window (which is good) but I lose the focus, and cannot type until I click on the terminal window again.
    • vanadium
      vanadium over 5 years
      A pity this did not work completely. It remains something that is difficult to control. Many related questions over the years unfortunatelly remained unanswered.
    • Joe
      Joe over 5 years
      It's not a bug, it's a feature! :( Another variation of this which affects my project is that when my app displays a pop-up (using yad) and the user clicks anywhere else, from then on that pop-up and all subsequent ones from the same app never get the focus back, even when they're on top! (The same thing would happen if another app displayed a window as in your case.) ...
    • Joe
      Joe over 5 years
      ... I haven't tried it yet, but I'm thinking about writing another little app (that my main app can start in the background and subsequently stop when it no longer needs it) that just sits in the background looking for my pop-ups and gives the focus back to them. It appears to be easy enough to write using bash and xdotool, but a lot of things look simple before you try to implement them. This would have to know what windows it was looking for in advance, so it's not a generic solution to your issue.
    • Joe
      Joe over 5 years
      Just brainstorming: It might be possible to write a desktop automation script using AutoKey or a similar tool that you could activate using a hotkey as soon as you open a window you want to keep focused. It would grab the info on the current window and use it to start my little helper script from the previous comment. This would be awkward, but it would probably work. That these suggestions are overly complex indicates a shortcoming in the Free Desktop windows specifications (which are otherwise pretty good.)
    • Pablo Bianchi
      Pablo Bianchi over 5 years
      gsettings set org.gnome.desktop.wm.preferences auto-raise false? Or maybe with wmctrl?
    • Giszmo
      Giszmo over 3 years
      The top and most upvoted comment by @vanadium doesn't work at all. If in the shell I type gedit &[enter]ls", "ls" ends up in gedit, not on the shell.
    • uncleremus
      uncleremus over 2 years
      Giszmo is right. I just tried this with gnome-shell 41.3, X11. The "strict" setting seems to have no effect.
    • uncleremus
      uncleremus over 2 years
      Focus grabbing can be done "right" - the ssh or gpg askpass dialogs do this, for example. The entire screen changes color and everything is frozen. It's practially impossible not to notice that something important happened, and to type private content into a "wrong" window inadvertently. But stealing focus silently, like gedit does, is really dangerous.
  • pomsky
    pomsky almost 4 years
    Not applicable to Ubuntu 18.04 with GNOME 3, it doesn't even have Compiz.
  • pomsky
    pomsky almost 4 years
    The bug you linked is for elementary OS and Gala. Not sure how useful it is for Ubuntu 18.04 with GNOME 3.
  • Vassilis Barzokas
    Vassilis Barzokas almost 4 years
    @pomsky I just today did the above steps on my Ubuntu 18.04 and they worked. I don't remember having added Compiz individually, other than the mentioned package. How could it be working?
  • pomsky
    pomsky almost 4 years
    Is your system upgraded from an older Ubuntu release?
  • Vassilis Barzokas
    Vassilis Barzokas almost 4 years
    No, it was installed as 18.04.1 and has been upgraded just up to 18.04.4 and I can verify that my window manager is Gnome 3, so I haven't changed it. Have you tried the above steps on your Ubuntu 18.04 and didn't work? I wonder if adding CompizConfig Settings Manager also adds any prerequisite that make this requested feature working. In any case I only did the above and I am no longer experiencing the issue.
  • pomsky
    pomsky almost 4 years
    Okay, fair enough!
  • Qumber
    Qumber almost 4 years
    Installing this on Pop!_OS 20.04 (based on Ubuntu, uses Gnome 3) worked via apt. But it couldn't help at all at any of the "Focus prevention levels" from Low to Very High. I tried adding apps - Terminal & Upwork - to using the option given to add window, and changing prevention level; no cigar.
  • Saren Tasciyan
    Saren Tasciyan over 3 years
    This doesn't change anything about focus.
  • axolotl
    axolotl over 3 years
    Especially, even with always on top and always on visible workspace enabled, Skype still steals focus when you switch to another workspace
  • Rick-777
    Rick-777 about 3 years
    Also, it doesn't work for MS Teams, which doesn't support the normal window options - there's no way to tell it to stay on any workspace.
  • JoKalliauer
    JoKalliauer almost 3 years
    Sorry it does not pop up but it steals the active window, so the typing goes to a window in the background which is even worse.
  • EffectiX
    EffectiX over 2 years
    On Ubuntu 20.04 I used this solution and it required one more step for it to work. I had to Alt+F2 and run the "r" command to restart the x server. I assume the settings get reloaded after that and THEN, new windows stopped stealing current window focus.
  • Imre Major
    Imre Major about 2 years
    Doesn't work for me unfortunately. I keep typing in a terminal window, meanwhile start spotify for example. As soon as the spotify window appears my typing stops working. Ubuntu 21.10, GNOME 40.4.0