Don't use the libraries, so can not test, but something similar to this, maybe:
import pythoncom, pyHook
def stroke(event):
if event.WindowName != stroke.current_window:
print event.WindowName
stroke.current_window = event.WindowName
print event.Key
stroke.current_window = None
ph = pyHook.HookManager()
ph.KeyDown = stroke
ph.HookKeyboard()
pythoncom.PumpMessages()
pyTony
pyMod
6,330 posts since Apr 2010
Reputation Points: 879
Solved Threads: 989
Skill Endorsements: 27
As functions are objects which can have attributes in them, we use one as static value, first initializing it to None. Global variables are considered ugly, though you could use them by using global keyword and this gives kind of "half Object Oriented version" (using objects but not writing classes ourself). Alternative would be to give stroke an extra key parameter which would remember the static value, see http://www.daniweb.com/software-development/python/code/366669
pyTony
pyMod
6,330 posts since Apr 2010
Reputation Points: 879
Solved Threads: 989
Skill Endorsements: 27
You have the decorator or class not both. Decorator access variable as local variable ie
current_window
. As you have class you should store it as instance variable self.current_window initialized in __init__ function and take out the decorator stuff.
pyTony
pyMod
6,330 posts since Apr 2010
Reputation Points: 879
Solved Threads: 989
Skill Endorsements: 27