SendKeys for Python 3.1 on Windows

前端 未结 4 1670
小鲜肉
小鲜肉 2020-12-03 09:44

The latest Python Sendkeys module is for Python 2.6. I can\'t upgrade it myself as it needs the C module to be recompiled.

Does anyone know of a fairly easy alternat

4条回答
  •  不知归路
    2020-12-03 09:48

    Got something that works, using win32api.keybd_event.

    Looks like SendInput would be safer, but pywin32 does not include it.

    [ edit: See a SendInput version in the accepted answer below. I leave this message here in case someone prefers to use sendkeys. jh ]

    This page helped: http://social.msdn.microsoft.com/Search/en-us/?Query=keybd_event

    Code that works:

    import win32api; import win32ui

    PyCWnd1 = win32ui.FindWindow( None, "an_app" )
    PyCWnd1.SetForegroundWindow()
    PyCWnd1.SetFocus()
    
    win32api.keybd_event(0x12, 0, ) # Alt
    win32api.keybd_event(0x12, 0, 2 ) # Alt release
    win32api.keybd_event(0x46, 0, ) # F
    win32api.keybd_event(0x52, 0, ) # R
    

    This does File > Reload in the app. Does not work if the app is minimized

    Without releasing Alt, after running this, the keyboard behaved like i was holding the Alt key! Don't seem to need to release the other keys

提交回复
热议问题