send keys to focused window

B

Broken

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 :)
 
L

Lawrence D'Oliveiro

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

I don't understand why you need to automate a GUI front-end, meant for human
use, to a function that can be directly performed without that front-end
anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top