Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a pyglet window without taking focus

My python application launches a subprocess that creates a pyglet window. When the pyglet window opens, it is in front of all other windows, and takes keyboard focus. I'd like my pyglet window to open in the background, and not take focus. Is this possible?

Stripped-down version of the code I'm using:

import pyglet

pyglet.window.Window()
pyglet.app.run() 

I'm using Windows 7, in case that makes a difference..

like image 775
Andrew Avatar asked Nov 04 '22 09:11

Andrew


2 Answers

Reversing focus is OS specific:

pyglet does not provide OS specific window control. So most likely you will have to use an ad-hoc trick for quick&dirty solutions or try to approach it with extensions like pywin32 using Windows API and/or COM to list through the stack of taskbar applications to reverse stealing of focus. You can also try to create your own window (container - which you can manipulate) first - in order to delegate its context to pyglet.

Delaying stealing of focus

On the other hand if according to your program logic you just want to delay actual showing of you application you can play with visibility:

import pyglet

w = pyglet.window.Window()
w.set_visible(False)
pyglet.app.run() 

So if you don't want to play with delegation of window context, you can probably do the following:

  1. start your application w/o showing the window
  2. find the focus of the current active window by getting its handle/id
  3. show your window
  4. give back the focus to the previous window

The above assumes working with windows API. I think MSDN had examples on focus changing.. If you know PID of your current window (main application) this should simplify the step 2.

like image 73
Yauhen Yakimovich Avatar answered Nov 09 '22 13:11

Yauhen Yakimovich


I am thinking this might be more of a windows / window manager issue than your app - will something like this http://pcsupport.about.com/od/windowsxp/ht/stealingfocus02.htm help ??

like image 29
Richard Green Avatar answered Nov 09 '22 13:11

Richard Green