So I would have posted a comment on the original author of this code, which was in response to someone else, here at Simulate physical keypress in python, without raising LowLevelKeyHookInjected (0x10) Flag, but my reputation is not high enough, but I do seek help none the less and would love the help of one who knows what's going on here, because a part of this code is magic that I cannot seem to grasp.
import ctypes
from ctypes import wintypes
import time
user32 = ctypes.WinDLL('user32', use_last_error=True)
INPUT_KEYBOARD = 1
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
MAPVK_VK_TO_VSC = 0
# msdn.microsoft.com/en-us/library/dd375731
wintypes.ULONG_PTR = wintypes.WPARAM
class MOUSEINPUT(ctypes.Structure):
_fields_ = (("dx", wintypes.LONG),
("dy", wintypes.LONG),
("mouseData", wintypes.DWORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
_fields_ = (("wVk", wintypes.WORD),
("wScan", wintypes.WORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
def __init__(self, *args, **kwds):
super(KEYBDINPUT, self).__init__(*args, **kwds)
if not self.dwFlags & KEYEVENTF_UNICODE:
self.wScan = user32.MapVirtualKeyExW(self.wVk,
MAPVK_VK_TO_VSC, 0)
class HARDWAREINPUT(ctypes.Structure):
_fields_ = (("uMsg", wintypes.DWORD),
("wParamL", wintypes.WORD),
("wParamH", wintypes.WORD))
class INPUT(ctypes.Structure):
class _INPUT(ctypes.Union):
_fields_ = (("ki", KEYBDINPUT),
("mi", MOUSEINPUT),
("hi", HARDWAREINPUT))
_anonymous_ = ("_input",)
_fields_ = (("type", wintypes.DWORD),
("_input", _INPUT))
LPINPUT = ctypes.POINTER(INPUT)
def PressKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode,
dwFlags=KEYEVENTF_KEYUP))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def F12():
PressKey(0x7B)
time.sleep(0.5)
ReleaseKey(0x7B)
# you can change 0x30 to any key you want. For more info look at :
# msdn.microsoft.com/en-us/library/dd375731
F12()
I am attempting to create a few functions which will involve the MOUSEINPUT class, but my issue is that the init function is very foreign to me, and it only involves the keyboard currently... I don't even know how I would go about applying the change to also initialize the mouse, I'm sure it is a very simple line of code, but alas.
I have created the mouse functions, they just don't do anything besides compile, and in my previous trouble shooting efforts it was due to this fact(that init func doesn't address the mouse input class.)
I am fully aware that there is another way of setting the cursor absolute position of the screen, but I would prefer to simply utilize this little library right here as it fulfils everything I need, I would just like its full potential unlocked for a couple of additional features it has tucked away.
(I already have a small library built for relative mouse packet data using win32 move_mouse function, which has operantly been deprecated and this this is the proper way of accomplishing the movement now according to documentation, plus my library would have no issue converting into this format with a little tinkering.)
I honestly have tried a couple of small little tinkers but the compiler always fails as I have no idea what it is that I'm doing when it comes to that super init python function which translates/grabs the c class, or something along the lines.
Summary: How can I initialize the MOUSEINPUT structure in the init function so I can use that class as well please?
Here is an adjustment I've made
class MOUSEINPUT(ctypes.Structure):
_fields_ = (("dx", wintypes.LONG),
("dy", wintypes.LONG),
("mouseData", wintypes.DWORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
def __init__(self, *args, **kwds):
super(MOUSEINPUT, self).__init__(*args, **kwds)
This adjustment compiles but when I test it with the following code produces no movement.
def MouseTest():
x = INPUT(type=INPUT_MOUSE,
mi=MOUSEINPUT(dx=5,
dy=5,
dwFlags=MOUSEEVENTF_MOVE))
user32.SendInput(INPUT_MOUSE, ctypes.byref(x), ctypes.sizeof(x))
Sorry for the confusion I have limited knowledge of both C and python.