Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to detect when a user switches between windows (in WinXP)?

Reply
Thread Tools

How to detect when a user switches between windows (in WinXP)?

 
 
mangoldproject@gmail.com
Guest
Posts: n/a
 
      12-25-2007
Hi everyone,

I'm a beginning-to-intermediate Python programmer with some experience
in other languages. At the moment I am trying to write a Python
program that will run in the background and execute a series of
commands whenever I switch between windows (my OS is Windows XP). For
example, I want my program to do something when I switch to my Firefox
browser, and then do something else when I switch to a certain sub-
window in Photoshop (which has a certain title, and a certain photo
associated with it), then do yet another thing when I switch to
another sub-window (with a different title and photo) within
Photoshop.
The particular actions will be based on the window's title. I've
already figured out how to retrieve that, using
GetWindowText(GetForegroundWindow()) from the win32gui module.
My question is, how can I detect when the user switches between
windows? I haven't been able to figure that part out yet.
Searching the group didn't give me any answers.
Your help is greatly appreciated. Thanks!

Assaf.
 
Reply With Quote
 
 
 
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      12-25-2007
On Dec 25, 4:31 am, (E-Mail Removed) wrote:
> Hi everyone,
>
> I'm a beginning-to-intermediate Python programmer with some experience
> in other languages. At the moment I am trying to write a Python
> program that will run in the background and execute a series of
> commands whenever I switch between windows (my OS is Windows XP). For
> example, I want my program to do something when I switch to my Firefox
> browser, and then do something else when I switch to a certain sub-
> window in Photoshop (which has a certain title, and a certain photo
> associated with it), then do yet another thing when I switch to
> another sub-window (with a different title and photo) within
> Photoshop.
> The particular actions will be based on the window's title. I've
> already figured out how to retrieve that, using
> GetWindowText(GetForegroundWindow()) from the win32gui module.
> My question is, how can I detect when the user switches between
> windows? I haven't been able to figure that part out yet.
> Searching the group didn't give me any answers.
> Your help is greatly appreciated. Thanks!
>
> Assaf.


What you most likely want to do is run the win32gui call inside an
infinite loop. Something along these lines:

# untested code
import time
while True:
winTitle = GetWindowText(GetForegroundWindow())
if winTitle == 'some string':
# do something
time.sleep(1) # nap for a second


This will check once a second to see what window you have up. You'll
probably want to set some kind of sentinel value when it does
something to the current window so it doesn't repeatedly do that
something each second. Thus, the if-then logic will need to be more
complex, but this should get you going.

HTH

Mike
 
Reply With Quote
 
 
 
 
Tim Roberts
Guest
Posts: n/a
 
      12-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
>I'm a beginning-to-intermediate Python programmer with some experience
>in other languages. At the moment I am trying to write a Python
>program that will run in the background and execute a series of
>commands whenever I switch between windows (my OS is Windows XP). For
>example, I want my program to do something when I switch to my Firefox
>browser, and then do something else when I switch to a certain sub-
>window in Photoshop (which has a certain title, and a certain photo
>associated with it), then do yet another thing when I switch to
>another sub-window (with a different title and photo) within
>Photoshop.
>The particular actions will be based on the window's title. I've
>already figured out how to retrieve that, using
>GetWindowText(GetForegroundWindow()) from the win32gui module.
>My question is, how can I detect when the user switches between
>windows? I haven't been able to figure that part out yet.
>Searching the group didn't give me any answers.


The way you do this is to write a Windows hook. The WH_CBT hook intercepts
WM_ACTIVATE and WM_DEACTIVATE calls system-wide.

However, that requires injecting the hook DLL into every process with a
Windows, and you certainly don't want to do that in Python. Write a
minimal C DLL to be the hook, and have it send messages to your Python
process.
--
Tim Roberts, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      12-27-2007
Tim Roberts wrote:
> (E-Mail Removed) wrote:
>> I'm a beginning-to-intermediate Python programmer with some experience
>> in other languages. At the moment I am trying to write a Python
>> program that will run in the background and execute a series of
>> commands whenever I switch between windows (my OS is Windows XP). For
>> example, I want my program to do something when I switch to my Firefox
>> browser, and then do something else when I switch to a certain sub-
>> window in Photoshop (which has a certain title, and a certain photo
>> associated with it), then do yet another thing when I switch to
>> another sub-window (with a different title and photo) within
>> Photoshop.
>> The particular actions will be based on the window's title. I've
>> already figured out how to retrieve that, using
>> GetWindowText(GetForegroundWindow()) from the win32gui module.
>> My question is, how can I detect when the user switches between
>> windows? I haven't been able to figure that part out yet.
>> Searching the group didn't give me any answers.

>
> The way you do this is to write a Windows hook. The WH_CBT hook intercepts
> WM_ACTIVATE and WM_DEACTIVATE calls system-wide.
>
> However, that requires injecting the hook DLL into every process with a
> Windows, and you certainly don't want to do that in Python. Write a
> minimal C DLL to be the hook, and have it send messages to your Python
> process.


Also, aside from the technicalities of how you do this, be sure
you've thought through the effect on user-interaction (if the
user isn't you). Switching between windows is one of those things
a user has a very definite expectation about. If you subvert that
by doing "magic things" when you switch between this and that
window then you might end up alienating the user.

But, as Tim points out, system hooks are the way to go.

TJG
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco Switches vs HP ProCurve Switches Greg Cisco 5 07-01-2013 01:48 AM
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
switches, spanning tree question regarding designated ports and switches alefveld@versatel.nl Cisco 1 12-30-2008 01:24 AM
Cisco Switches with ACL's between VLAN's and windows servers chart@homesoc.com Cisco 1 10-26-2005 04:39 PM
native vlan mismatch on 2 2924 switches w/ only 1 vlan defined (same on both switches) avraham shir-el Cisco 4 07-20-2004 08:08 AM



Advertisments