0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Why did you tag this with the C language? There is nothing "C" in this and tricking people interested in C questions here is just wasting their time. – Ted Lyngmo Jan 28 '23 at 02:54
  • 1
    @TedLyngmo: It may not be obvious to someone with *only* C knowledge and no Python expertise, but this code is using the [`ctypes` foreign function library](https://docs.python.org/3/library/ctypes.html) to interact with Windows C APIs. Both Python and C knowledge are important for answering this question. – user2357112 Jan 28 '23 at 03:06
  • 1
    @user2357112 I don't see how my (limited) C knowledge can help here, but I rolled back my removal of the C tag. – Ted Lyngmo Jan 28 '23 at 03:08
  • See if [How to send mouse clicks?](https://stackoverflow.com/a/73941582/235698) helps. – Mark Tolonen Jan 28 '23 at 03:24
  • Looking back at this question: If it was focused on the interaction between the two languages (in a [mre]) I could perhaps have some input. Right now, this question is all over the place and needs focus. – Ted Lyngmo Jan 28 '23 at 04:05
  • I think the problem is with mouse input not having a thorough constructor like the keyboard as such 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) But I don't know how it was made, the copy and paste with-out the if not self.dwFlags & KEYEVENTF_UNICODE: doesn't work, and not sure what to look at to see what values I have to adjust. – Arthur Suksta Jan 30 '23 at 02:56

0 Answers0