Python: Can bring window to front but cannot set focus (win32gui.SetForegroundWindow)

Question:

My program pops up a window every time the user presses F2 (in any application).

I’m using pynput to capture the F2 button (works ok)

I’m using tkinter to create the popup window (works ok)

I’m using win32gui.SetForegroundWindow(windowHandel) to bring the tkinter window to the front and set the focus. And there is the problem.

If the python windows is selected when I press F2, everything works ok, and the tkinter window both moves to front and gets focus.

BUT – if any other window is selected when I press F2, the tkinter window does moves to the front, but it is not selected (i.e. focused).

Here is the relevant section from the code (find full code below):

while not windowFound and counter < MAX_TRIES_TO_FIND_THE_FLIPPER_WINDOW:

   try:
      windowHandel = win32gui.FindWindow(None, windowName)
      win32gui.SetForegroundWindow(windowHandel)
   except:
      windowFound = False
   else:
      print("Success, Window found on the " + str(counter + 1) + " tries")
      windowFound = True

After looking for an answer for a while, I found someone saying that this can be solved by using win32process. So I tried adding:

windowHandelID, _ = win32process.GetWindowThreadProcessId(windowHandel)
win32process.AttachThreadInput(win32api.GetCurrentThreadId(), windowHandelID, True)
win32gui.SetFocus(windowHandel)

Yet, it resulted in the same behavior.

Here below is the full (simplified, without exit conditions) code.
Try pressing F2 while pythong is focused.

And then try pressing F2 while any other window (e.g. notepad) is focused.
You’ll see that in one case you can just start writing and the tkinter window will receive the input while in the other case, you’ll still have to click the window.

I’d appreciate any help or suggestions.

import pyautogui  # For keyboard shortcuts and moving the cursor and selecting the window
import time  # For the delay function
from pynput import keyboard  # For catching keyboard strokes
import tkinter  # GUI
import threading  # For Threading
import win32gui  # For Setting Focus on the Flipper Window
import win32process
import win32api

# Resetting Variables / Settings
start_flipping_text_sequence = False
ContinueThreads = True
SearchForFlipperWindow = False
window_name = "tk"
MAX_TRIES_TO_FIND_THE_FLIPPER_WINDOW = 10


# This function runs in a separate thread
def selectFlipperWindow(windowName):

    # Since the thread runs constantly, it will only start looking for the flipper window when this variable is True
    global SearchForFlipperWindow

    # How many loops should the program go through before it gives up on finding the window
    global MAX_TRIES_TO_FIND_THE_FLIPPER_WINDOW

    # While program was not ended
    while True:

        # This is False, unless F2 is pressed
        if SearchForFlipperWindow:

            # Did the program find the flipper window
            windowFound = False
            counter = 0

            while not windowFound and counter < MAX_TRIES_TO_FIND_THE_FLIPPER_WINDOW:

                try:
                    windowHandel = win32gui.FindWindow(None, windowName)
                    win32gui.SetForegroundWindow(windowHandel)
                except:
                    windowFound = False
                else:
                    print("Success, Window found on the " + str(counter + 1) + " tries")

                    windowHandelID, _ = win32process.GetWindowThreadProcessId(windowHandel)
                    win32process.AttachThreadInput(win32api.GetCurrentThreadId(), windowHandelID, True)
                    win32gui.SetFocus(windowHandel)
                    windowFound = True

                counter += 1
                time.sleep(0.1)

            SearchForFlipperWindow = False

        time.sleep(0.1)


# Execute functions based on the clicked key
def on_press(key):
    global start_flipping_text_sequence

    # If the user pressed the F2 key
    if key == keyboard.Key.f2:
        start_flipping_text_sequence = True

def okButton():
    root.destroy()

def enter(event):
    okButton()

# Assigning event to function
listener = keyboard.Listener(on_press=on_press)

# initiating listener
listener.start()

# Start a thread for searching for the flipper window
selectWindowThread = threading.Thread(target=selectFlipperWindow, args=(window_name,))
selectWindowThread.start()

while 1 == 1:
    time.sleep(.05)

    if start_flipping_text_sequence:

        SearchForFlipperWindow = True

        root = tkinter.Tk()

        tk_window_input = tkinter.Entry(root, width=100)
        tk_window_input.pack(padx=20)
        tk_window_input.focus()

        # Binds the OK button to the okButton function above
        tk_window_ok = tkinter.Button(root, width=20, text="OK", command=okButton)
        tk_window_ok.pack(pady=20)

        # Binds the "Enter" keyboard key to the "enter" event above
        tk_window_input.bind('<Return>', enter)

        # the main looper of the tkinter window
        # runs until root.destroy() to executed above
        root.mainloop()

        start_flipping_text_sequence = False

```

Asked By: Menny Barzilay

||

Answers:

What you see is an intentional restriction in Windows. The restriction is described by Raymond Chen in article Foreground activation permission is like love: You can’t steal it, it has to be given to you. Remarks section of the SetForegroundWindow documentation gives more technical details about the restriction.

There are ways to be exempt from the restriction. One good way to do so is described by Raymond Chen in article Pressing a registered hotkey gives you the foreground activation love.

The following code shows one more, strange way to bypass the restriction:

kbd.press(keyboard.Key.alt)
try:
    win32gui.SetForegroundWindow(windowHandel)
finally:
    kbd.release(keyboard.Key.alt)

where kbd was created like this:

from pynput.keyboard import Controller
kbd = Controller()

Here is an explanation why this workaround works: link.

A good way to get rid of this workaround may be to require a user to press Alt-F2 in order to switch to your application.

Good luck with coding 😉

Answered By: Pavel Shishpor
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.