Mouse and keyboard operation in windows using python
2018-02-07 15:24
489 查看
using the api of win32.
The demo code of operating mouse:
The demo code of operating the keyboard:
The demo code of operating mouse:
import win32api,win32con #鼠标定位到(500,500) win32api.SetCursorPos([500,500]) #执行左单键击,若需要双击则延时几毫秒再点击一次即可 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) #右键单击 win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
The demo code of operating the keyboard:
import time, threading, copy import sys import win32api, win32con SCAN_TIME = 1.0 / 50 KEY_VALUE_LIST = { 'left': 37, 'up': 38, 'right': 39, 'down': 40, } class keyboard(object): __keyList = set() def __init__(self): self.mainThread = threading.Thread(target=self.__main_thread_fn) self.mainThread.setDaemon(True) self.mainThread.start() def __main_thread_fn(self): while 1: for keyValue in copy.copy(self.__keyList): win32api.keybd_event(keyValue, 0, 0, 0) time.sleep(SCAN_TIME) def __get_key_value(self, key): if key.isdigit() and 0 <= int(key) <= 9: return int(key) elif len(key) == 1 and 65 <= ord(key.upper()) <= 90: return ord(key.upper()) elif key in KEY_VALUE_LIST.keys(): return KEY_VALUE_LIST[key] def key_down(self, key): keyValue = self.__get_key_value(key) if keyValue is None: return False self.__keyList.add(keyValue) return True def key_up(self, key): keyValue = self.__get_key_value(key) if keyValue is None: return False if keyValue in self.__keyList: self.__keyList.remove(keyValue) return True if __name__ == '__main__': kb = keyboard() time.sleep(20) kb.key_down('b') time.sleep(20) kb.key_up('b') try: sys.stdin.read() except: print('Finish')
相关文章推荐
- CRUD Operation using Web API and Windows Application
- 【Python错误】windows下使用pip/easy_install提示Fatal error in launcher: Unable to create process using...
- Using Boost Libraries in Windows Store and Phone Applications
- Processing Global Mouse and Keyboard Hooks in C#
- Windows 8: Tips and Tricks for mouse/keyboard users
- Using Boost Libraries in Windows Store and Phone Applications
- How to get the handle in windows using python
- Using Credential Management in Windows XP and Windows Server 2003 【转载】
- Sublime Text 2 for C/C++ and Python in Windows
- Virtualbox mouse move in and out and file share with windows
- Using a Microsoft Account to Logon and Resulting Internet Communication in Windows 8
- Using Windows XP Visual Styles and PrintWindow in Windows Applications
- Detecting Skin in Images & Video Using Python and OpenCV皮肤检测
- Using Keyboard Interface Control in Windows
- [转]Installing, Configuring and Using Windows Server AppFabric and the "Velocity" Memory Cache in 10 minutes
- Processing Global Mouse and Keyboard Hooks in C#
- Using Credential Management in Windows XP and Windows Server 2003 【转载】
- some list operation(#quote from MIT 'introduction to computation and programming using python, Revis
- Virtualbox mouse move in and out and file share with windows
- Read and Write to a Keyboard device in Linux using C++