Get active window windows

Get last active window: Get Previously active window

I am working on an application which needs to get the last active window handle. Suppose my application is running then I want to get last active window handle that was just previously open just before my application.

@EDIT1: This is not the duplicate question. I need to get the handle of last active window not the current window.

2 Answers 2

This is similar to alternate SO question, I would assume you would just track the active window and upon change you would then know the previously active

Edit, this is basically code copied from the question I linked that was looking for current active window but with logic to persist the lastHandle and identify when you have a new lastHandle. It’s not a proven, compilable implementation:

I needed the same thing of the last handle from the previous window I had open. The answer from Jamie Altizer was close, but I modified it to keep from overwriting the previous window when my application gets focus again. Here is the full class I made with the timer and everything.

Not the answer you’re looking for? Browse other questions tagged c# or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Потери udp пакетов linux

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Obtain Active window using Python

I would like to get the active window on the screen using python.

For example, the management interface of the router where you enter the username and password as admin

That admin interface is what I want to capture using python to automate the entry of username and password.

What imports would I require in order to do this?

8 Answers 8

On windows, you can use the python for windows extensions (http://sourceforge.net/projects/pywin32/):

Below code is for python 3:

The following script should work on Linux, Windows and Mac. It is currently only tested on Linux (Ubuntu Mate Ubuntu 15.10).

Prerequisites

For Linux:

Install wnck ( sudo apt-get install python-wnck on Ubuntu, see libwnck.)

For Windows:

Make sure win32gui is available

For Mac:

Make sure AppKit is available

The script

Thanks goes to the answer by Nuno André, who showed how to use ctypes to interact with Windows APIs. I have written an example implementation using his hints.

The ctypes library is included with Python since v2.5, which means that almost every user has it. And it’s a way cleaner interface than old and dead libraries like win32gui (last updated in 2017 as of this writing). ((Update in late 2020: The dead win32gui library has come back to life with a rename to pywin32, so if you want a maintained library, it’s now a valid option again. But that library is 6% slower than my code.))

Documentation is here: https://docs.python.org/3/library/ctypes.html (You must read its usage help if you wanna write your own code, otherwise you can cause segmentation fault crashes, hehe.)

Читайте также:  Woman looking in the windows

Basically, ctypes includes bindings for the most common Windows DLLs. Here is how you can retrieve the title of the foreground window in pure Python, with no external libraries needed! Just the built-in ctypes! 🙂

The coolest thing about ctypes is that you can Google any Windows API for anything you need, and if you want to use it, you can do it via ctypes!

Python 3 Code:

Performance is extremely good: 0.01 MILLISECONDS on my computer ( 0.00001 seconds).

Will also work on Python 2 with very minor changes. If you’re on Python 2, I think you only have to remove the type annotations ( from typing import Optional and -> Optional[str] ). 🙂

Win32 Technical Explanations:

The length variable is the length of the actual text in UTF-16 (Windows Wide «Unicode») CHARACTERS. (It is NOT the number of BYTES.) We have to add + 1 to add room for the null terminator at the end of C-style strings. If we don’t do that, we would not have enough space in the buffer to fit the final real character of the actual text, and Windows would truncate the returned string (it does that to ensure that it fits the super important final string Null-terminator).

The create_unicode_buffer function allocates room for that many UTF-16 CHARACTERS.

Most (or all? always read Microsoft’s MSDN docs!) Windows APIs related to Unicode text take the buffer length as CHARACTERS, NOT as bytes.

Also look closely at the function calls. Some end in W (such as GetWindowTextLengthW ). This stands for «Wide string», which is the Windows name for Unicode strings. It’s very important that you do those W calls to get proper Unicode strings (with international character support).

Читайте также:  Запуск скомпилированного файла linux

PS: Windows has been using Unicode for a long time. I know for a fact that Windows 10 is fully Unicode and only wants the W function calls. I don’t know the exact cutoff date when older versions of Windows used other multi-byte string formats, but I think it was before Windows Vista, and who cares? Old Windows versions (even 7 and 8.1) are dead and unsupported by Microsoft.

UPDATE in Late 2020, Benchmark vs the pywin32 library:

Typical result after doing multiple runs on an AMD Ryzen 3900x:

My function: 4.5769994258880615 seconds

Win32UI library: 4.8619983196258545 seconds

Win32UI library is 6.226762715455125 percent slower.

However, the difference is small, so you may want to use the library now that it has come back to life (it had previously been dead since 2017). But you’re going to have to deal with that library’s weird «no window is in the foreground» exception, which my code doesn’t suffer from (see the code comments in the benchmark code).

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