Keeping the terminal in focus
windows terminal binary
windows terminal update
windows terminal review
windows terminal opacity when not focused
windows terminal icon
windows terminal setup
I have a python script which uses selenium to automate web page, drawing focus away from the terminal where user input is required.
Is there anyway in python to switch focus back to the terminal, programmatically?
I will be running my program in the Windows Command Prompt on Windows 7, if it matters, but a cross platform answer would be most useful.
Looking at the
pywin32 package bindings for the win32 API I have the following:
import win32console import win32gui from selenium import webdriver as wd d = wd.Firefox() win32gui.SetFocus(win32console.GetConsoleWindow()) win32gui.FlashWindow(win32console.GetConsoleWindow(), False) input('Should have focus: ')
SetFocus causes the error
pywintypes.error: (5, 'SetFocus', 'Access is denied.') due to Microsoft removing the ability to take focus from another application.
FlashWindow appears to do nothing.
Here is what I came up with that seems to be working.
class WindowManager: def __init__(self): self._handle = None def _window_enum_callback( self, hwnd, wildcard ): if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None: self._handle = hwnd #CASE SENSITIVE def find_window_wildcard(self, wildcard): self._handle = None win32gui.EnumWindows(self._window_enum_callback, wildcard) def set_foreground(self): win32gui.ShowWindow(self._handle, win32con.SW_RESTORE) win32gui.SetWindowPos(self._handle,win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE) win32gui.SetWindowPos(self._handle,win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE) win32gui.SetWindowPos(self._handle,win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_SHOWWINDOW + win32con.SWP_NOMOVE + win32con.SWP_NOSIZE) shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') win32gui.SetForegroundWindow(self._handle) def find_and_set(self, search): self.find_window_wildcard(search) self.set_foreground()
Then to find a window and make it active you can...
w = WindowManager() w.find_and_set(".*cmd.exe*")
This is in python 2.7, also here are some links I found to explain why you have to go through so much trouble to switch active windows.
win32gui.SetActiveWindow() ERROR : The specified procedure could not be found
Windows 7: how to bring a window to the front no matter what other window has focus?
OS X terminal "open" command and keeping focus on the command , Use the -g flag. From the man page: -g Do not bring the application to the foreground. Example: To open the folder at your current path, behind the current I write code. I use terminal a lot. I ofter use many terminal windows. I hate how tabs look. Are there any applications that allow me to keep multiple terminal windows in focus to streamline my
This doesn't really answer your question, but the easy solution is to not take the focus away in the first place:
driver = webdriver.PhantomJS() # ...
PhantomJS webdriver doesn't have any UI, so does not steal the focus.
command line - Keeping multiple terminal windows in focus, Try Terminator. It allows you to split the terminal window (You can also have tabs and separate windows). simple terminator usage. You can September 30, 2014 7:23 AM PDT. The always on top option is basically a way to pin a window in front of all others, even when it is not in focus. While this feature is most common in media player apps, you may want to use it elsewhere, like a document editor or second Web browser.
For getting focus, check the comments to this answer.
A cross-platform method could be to use Tkinter for the user GUI, as it has methods to grab and set focus for its windows.
microsoft/terminal, Feature Request: Add a settings to keep Acrylic when it is unfocused. Once the terminal loses focus, the system takes acrylic away from us. Open up Windows Terminal. Open up any other window. Make sure windows terminal has the useAcrylic set to tru and the opacity set to something like .5. Snap the two windows to either side. When Terminal is in focus the acrylic will work. When it's not it will not. Expected behavior. The acrylic effect to stay whether or not terminal is in focus.
If you don't care about clearing any figure displayed in the matplotlib frame -- which I believe is normally the case when one wants to get the focus back on the console for user input -- use simply this:
Keybinding to focus next terminal does not update terminal , I have added a keybinding to show the precious/next terminal when terminal has Fixed in master, keeping open to pull into release/1.28 Run the program inside the same session as the one where the focus issue occurs. If the FOREGROUND LOCK Timeout value is not zero, then this is most likely the root cause of the focus issue. It is important to ensure that the tool is running inside the same session as the Seamless published application.
How to stop Escape from Leaving Terminal – IDEs Support (IntelliJ , I want focus to stay in the terminal after I press Escape. Toggling the Override IDE shortcuts setting in Tools -> Terminal makes no difference either. If I'm right, it's probably better to keep the ESC as a Intellij-global way to workbench.action.terminal.focus: Focus the terminal. This is like toggle but focuses the terminal instead of hiding it, if it is visible. This is like toggle but focuses the terminal instead of hiding it, if it is visible.
Terminal Tool View Plugin - KDE Documentation, When the terminal emulator is displayed, it will get the focus, so that you can start Alternatively, you can configure Kate to always keep the terminal in sync with To clear the entire buffer, use Ctrl+L when focus is in the terminal. The mouse can be used to select text in the terminal so it can be copied & pasted elsewhere. To send the entire terminal buffer to a new RStudio editor buffer, use the Copy Terminal to Editor command on the terminal drop-down menu. Advanced - Terminal Architecture and Lifetime
How to switch the cursor between terminal and code in VSCode , This option will hide the terminal when you go back to the editor. An alternative that only shifts focus and keeps terminal visible is here: View: Focus First Editor I have a python script which uses selenium to automate web page, drawing focus away from the terminal where user input is required. Is there anyway in python to switch focus back to the terminal, programmatically? I will be running my program in the Windows Command Prompt on Windows 7, if it matters, but a cross platform answer would be most
- @Saifur Not really, my script is written in python (so it's tough to get that running in a browser developer console) and the automation involves the closing and opening of multiple browsers all with their own developer consoles. Ideally I am looking for a single python package or script I can execute from python which brings focus back to the terminal.
- why not use a VM to host all your selenium-related activities? Maybe use selenium grid or ssh to launch any selenium scripts?
win32gui.SetFocus(win32console.GetConsoleWindow()), I get the following error: "pywintypes.error: (5, 'SetFocus', 'Access is denied.')". It seems you don't.. Any idea why? (I run Python 2.7+)
- This is inspired! With fantastic justification, it answers my question perfectly. Here have some bounty!
- Thanks for the suggestion but the reason I'm using selenium is to automate multiple browsers with UIs.
- Ah, so you're using it for cross-browser testing?
- Yes and no, I have multiple use cases for my automation, one of which is programmatic tests but I will also be using them for showcasing and visual inspections.
- I've attempted to use the win32 API as you suggest but have run into issues, please see my question edit.
- Could you please explain how I would use Tkinter for my use case, I've looked at the documentation but I can't find a solution.
Tkinteryou would create your own console-type window to interact with the user, and
Tkinterhas its own methods for grabbing focus.