send keys to focused window

Broken m at n.d
Thu Jul 9 12:32:41 EDT 2009


Hi,
I am new to Python, and I'm miserably failing to send specific keys to 
(say) notepad.

Here is the scenario:
I have notepad open.
My python script is running in the background.
When I press ALT+a I want to intercept the keys and send "ä"(ASCII code: 
ALT+0228) instead.

OS: Windows 7
Libraries used: pyHook, SendKeys, pyWin32

Here is my code:

###############################
# -*- coding: cp1252 -*-
import pythoncom, pyHook
from SendKeys import SendKeys

def OnKeyboardEvent(event):
    if event.Alt == 32:         #alt pressed
        if event.Ascii == 97:   #a pressed
            SendKeys("ä")
        return False #don't pass to other handlers

# return True to pass the event to other handlers
    return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

###############################

It's largely copied from the pyhook example.
My problem is, I need to somehow disable ALT while sonding the key.

Please help :)



More information about the Python-list mailing list