博文目录
使用方式
from SendInput import Keyboard, Mouse
Keyboard.press(0x1e)
Mouse.leftClick()
SendInput.py
import ctypes
class MouseInput(ctypes.Structure):
_fields_ = [("dx", ctypes.c_long),
("dy", ctypes.c_long),
("mouseData", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))]
class KeyboardInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
class Inner(ctypes.Union):
_fields_ = [("ki", KeyboardInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", Inner)]
"""
MouseInput
"""
MOUSEEVENTF_MOVE = 0x0001
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
MOUSEEVENTF_MIDDLEDOWN = 0x0020
MOUSEEVENTF_MIDDLEUP = 0x0040
MOUSEEVENTF_XDOWN = 0x0080
MOUSEEVENTF_XUP = 0x0100
MOUSEEVENTF_WHEEL = 0x0800
MOUSEEVENTF_HWHEEL = 0x1000
MOUSEEVENTF_MOVE_NOCOALESCE = 0x2000
MOUSEEVENTF_VIRTUALDESK = 0x4000
MOUSEEVENTF_ABSOLUTE = 0x8000
XBUTTON1 = 0x0001
XBUTTON2 = 0x0002
WHEEL_DELTA = 120
"""
KeyboardInput
"""
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_SCANCODE = 0x0008
KEYEVENTF_UNICODE = 0x0004
"""
Input
"""
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
"""
SendInput
cInputs: pInputs 数组的个数, 即SendInput可以一下发好几个鼠标事件/键盘事件, 这些事件存储在一个连续的数组空间里
pInputs: 输入事件的实例的指针
cbSize: 每一个INPUT事件的结构体空间, 鼠标事件和键盘事件应该不能同时放到pInputs数组中, 因为他们的size不同
该函数返回成功插入键盘或鼠标输入流的事件数。如果函数返回零,则输入已被另一个线程阻塞。要获取扩展错误信息,请调用GetLastError.
"""
def SendInput(*inputs):
nInputs = len(inputs)
pointer = Input * nInputs
pInputs = pointer(*inputs)
cbSize = ctypes.sizeof(Input)
return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
class Keyboard:
@staticmethod
def press(wVk):
return SendInput(Input(INPUT_KEYBOARD, Inner(ki=KeyboardInput(wVk, 0, 0, 0, None))))
@staticmethod
def release(wVk):
return SendInput(Input(INPUT_KEYBOARD, Inner(ki=KeyboardInput(wVk, 0, KEYEVENTF_KEYUP, 0, None))))
@staticmethod
def pressByScanCode(wScan):
return SendInput(Input(INPUT_KEYBOARD, Inner(ki=KeyboardInput(0, wScan, KEYEVENTF_SCANCODE, 0, None))))
@staticmethod
def releaseByScanCode(wScan):
return SendInput(Input(INPUT_KEYBOARD, Inner(ki=KeyboardInput(0, wScan, KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP, 0, None))))
@staticmethod
def pressByUnicode(wScan):
return SendInput(Input(INPUT_KEYBOARD, Inner(ki=KeyboardInput(0, wScan, KEYEVENTF_UNICODE, 0, None))))
@staticmethod
def releaseByUnicode(wScan):
return SendInput(Input(INPUT_KEYBOARD, Inner(ki=KeyboardInput(0, wScan, KEYEVENTF_UNICODE | KEYEVENTF_KEYUP, 0, None))))
class Mouse:
@staticmethod
def leftDown():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_LEFTDOWN, 0, None))))
@staticmethod
def leftUp():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_LEFTUP, 0, None))))
@staticmethod
def leftClick():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, None))))
@staticmethod
def leftDoubleClick():
event = Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, None)))
return SendInput(event, event)
@staticmethod
def rightDown():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_RIGHTDOWN, 0, None))))
@staticmethod
def rightUp():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_RIGHTUP, 0, None))))
@staticmethod
def rightClick():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, None))))
@staticmethod
def middleDown():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_MIDDLEDOWN, 0, None))))
@staticmethod
def middleUp():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_MIDDLEUP, 0, None))))
@staticmethod
def middleClick():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, 0, MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP, 0, None))))
@staticmethod
def x1Down():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, XBUTTON1, MOUSEEVENTF_XDOWN, 0, None))))
@staticmethod
def x1Up():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, XBUTTON1, MOUSEEVENTF_XUP, 0, None))))
@staticmethod
def x1Click():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, XBUTTON1, MOUSEEVENTF_XDOWN | MOUSEEVENTF_XUP, 0, None))))
@staticmethod
def x2Down():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, XBUTTON2, MOUSEEVENTF_XDOWN, 0, None))))
@staticmethod
def x2Up():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, XBUTTON2, MOUSEEVENTF_XUP, 0, None))))
@staticmethod
def x2Click():
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, XBUTTON2, MOUSEEVENTF_XDOWN | MOUSEEVENTF_XUP, 0, None))))
@staticmethod
def move(x, y, absolute=False):
if not absolute:
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(x, y, 0, MOUSEEVENTF_MOVE, 0, None))))
else:
w, h = ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1)
rx, ry = int(x * 65535 / w), int(y * 65535 / h)
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(rx, ry, 0, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, 0, None))))
@staticmethod
def scroll(delta, vertical=True):
dwFlags = MOUSEEVENTF_WHEEL if vertical else MOUSEEVENTF_HWHEEL
return SendInput(Input(INPUT_MOUSE, Inner(mi=MouseInput(0, 0, delta * WHEEL_DELTA, dwFlags, 0, None))))
if __name__ == '__main__':
print(Keyboard.press(0x51))
print(Keyboard.pressByScanCode(0x1e))
print(Keyboard.pressByUnicode(0x4e00))