'Python send keys on windows

I'm trying to send keyboard commands through python 2.7 on a windows machine. I understand that the SendKeys module does not work with 2.7. So I use keybd_event from the win32 api instead.

The key that i want to send is shift+left to select a word. This is the code:

win32api.keybd_event(win32con.VK_LSHIFT,0,win32con.KEYEVENTF_EXTENDEDKEY | 0,0)
win32api.keybd_event(win32con.VK_LEFT,0,0,0)
win32api.keybd_event(win32con.VK_LSHIFT,0,win32con.KEYEVENTF_EXTENDEDKEY | win32con.KEYEVENTF_KEYUP,0)

This all works. However, the shift key stays depressed! Even after I kill the process. The only way to release the shift key after is to physically press capslock, then shift + a key, then capslock off. Its very similiar to this problem: http://www.bigresource.com/Tracker/Track-vb-RvVMlrAbZn/

Do you have any idea what to do?

Thanks in advance!



Solution 1:[1]

While i couldnt find a way to get win32api to work with shift, I'm using SendKeys from http://code.google.com/p/sendkeys-ctypes/ right now, which seems to work with 2.7.

Solution 2:[2]

SendInput from the windows API may be helpful. The recent Raymond Chen blog entry suggests you can even use it to control screensaver start/stop.

Solution 3:[3]

You could try the pywin32 package, please note it is old and unmaintained.

Here's an example from StackOverflow

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohamad
Solution 2 Boofhead
Solution 3 Community