March 4th, 2005, 03:45 AM
The short answer is you can't, at least not with the standard Python library.
You can do it by calling the operating system routine directly using the ctypes
library. On Windows the function you need is SendInput
Alternatively if you have the win32all extensions installed you can call the SendKeys function using the WSH scripting objects - the docs here
>>> import win32com
>>> from win32com.client import Dispatch
>>> shell = Dispatch('Wscript.Shell')
>>> shell.SendKeys('%f') #send Alt-f
>>> shell.SendKeys('^c') #send Ctrl-c
You cannot send a ctrl, shift or alt key on its own using the SendKeys function - it has to be sent in combination with another character.
Warning: with both functions you have no control over which application the keystrokes are sent to - they will go to whatever has the focus. This makes it a very fragile technique, since if the user switches to a different program while you are sending keys then random and unxepected things will happen.
Dave - The Developers' Coach
Last edited by DevCoach : March 4th, 2005 at 04:13 AM.