There are many python libraries available for controlling the mouse/keyboard. Here are a few I've used or heard of:
Autopy: https://github.com/msanders/autopy
PyMouse: http://code.google.com/p/pymouse/wiki/Documentation
Dogtail: https://fedorahosted.org/dogtail/
If you're on Windows, I believe win32api / win32con also can work.