March 3rd, 2005, 06:53 PM
pressing a key programatically
Kinda silly question
does any one know how to hit CTRL key programatically in python.
March 4th, 2005, 02: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.
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.
>>> import win32com
>>> from win32com.client import Dispatch
>>> shell = Dispatch('Wscript.Shell')
>>> shell.SendKeys('%f') #send Alt-f
>>> shell.SendKeys('^c') #send Ctrl-c
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
Comments on this post
Last edited by DevCoach; March 4th, 2005 at 03:13 AM.