我想做同样的事情:使用ctypes在Python中使用How to use extended scancodes in SendInput。
我可以用Sendinput发送常规的扫描码,但是我似乎不能发送Input数组,因此无法发送扩展的扫描码。到目前为止,我是这样做的。有谁能给我指个方向吗?这根本不起作用,我希望它按右CTRL键。
import ctypes
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
_fields_ = [("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
_fields_ = [("uMsg", ctypes.c_ulong),
("wParamL", ctypes.c_short),
("wParamH", ctypes.c_ushort)]
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", PUL)]
class InputI(ctypes.Union):
_fields_ = [("ki", KeyBdInput),
("mi", MouseInput),
("hi", HardwareInput)]
class Input(ctypes.Structure):
_fields_ = [("type", ctypes.c_ulong),
("ii", InputI)]
Inputx = Input * 2 # to create an array of Input, as mentionned in ctypes documentation
def press(scan_code):
extra1 = ctypes.c_ulong(0)
ii1_ = InputI()
ii1_.ki = KeyBdInput(0, 0xE0, 0x0008, 0, ctypes.pointer(extra1))
x1 = Input(ctypes.c_ulong(1), ii1_)
extra2 = ctypes.c_ulong(0)
ii_2 = InputI()
ii_2.ki = KeyBdInput(1, scan_code, 0x0008, 0, ctypes.pointer(extra2))
x2 = Input(ctypes.c_ulong(1), ii_2)
x = Inputx(x1, x2)
ctypes.windll.user32.SendInput(2, ctypes.pointer(x), ctypes.sizeof(x))
press(0x1D)发布于 2020-12-26 00:22:19
我刚刚阅读了MSDN上的SendInput documentation,并找到了解决方案:
右控件的扫描码为0xE0 0x1D,因此在KeyBdInput中使用的扫描码必须为0x1D,要指定其前缀为0xE0,flag参数必须将其位#0设置为1,从而导致以下结果:
def press_ext(scan_code):
extra = ctypes.c_ulong(0)
ii_ = InputI()
ii_.ki = KeyBdInput(0, scan_code, 0x0008 | 0x0001, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
def release_ext(scan_code):
extra = ctypes.c_ulong(0)
ii_ = InputI()
ii_.ki = KeyBdInput(0, scan_code, 0x0008 | 0x0002 | 0x0001, 0, ctypes.pointer(extra))
x = Input(ctypes.c_ulong(1), ii_)
ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
press_ext(0x1D) # press RIGHT CTRL
release_ext(0x1D) #release ithttps://stackoverflow.com/questions/65436284
复制相似问题