#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    10

    pressing a key programatically


    Kinda silly question
    does any one know how to hit CTRL key programatically in python.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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.

    e.g.
    Code:
    >>> 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

    Comments on this post

    • wx_uab agrees
    Last edited by DevCoach; March 4th, 2005 at 03:13 AM.

IMN logo majestic logo threadwatch logo seochat tools logo