I have the following script that sends a keyboard key every xx seconds (F15 by default), inspired by some code found online
I want to remove the types and union related to Mouse events but cannot make it to work. In particular, I don't know how to remove the class MOUSEINPUT and the union of types _INPUTunion (removing them will stop sending the keyboard key).
Any suggestion on how to trim the script to a minimum (i.e. only keep code related to Keyboard)?
The following code will send the key "C" to be able to debug.
#!/python
import ctypes
import sys
import time
LONG = ctypes.c_long
DWORD = ctypes.c_ulong
ULONG_PTR = ctypes.POINTER(DWORD)
WORD = ctypes.c_ushort
class MOUSEINPUT(ctypes.Structure):
    _fields_ = (
        ('dx', LONG), ('dy', LONG), ('mouseData', DWORD),
        ('dwFlags', DWORD), ('time', DWORD),
        ('dwExtraInfo', ULONG_PTR)
    )
class KEYBDINPUT(ctypes.Structure):
    _fields_ = (
        ('wVk', WORD), ('wScan', WORD),
        ('dwFlags', DWORD), ('time', DWORD),
        ('dwExtraInfo', ULONG_PTR)
    )
class _INPUTunion(ctypes.Union):
    _fields_ = (('mi', MOUSEINPUT), ('ki', KEYBDINPUT))
class INPUT(ctypes.Structure):
    _fields_ = (('type', DWORD), ('union', _INPUTunion))
def SendInput(*inputs):
    print(inputs[0].union.mi)
    nInputs = len(inputs)
    LPINPUT = INPUT * nInputs
    pInputs = LPINPUT(*inputs)
    cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
    return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
INPUT_KEYBOARD = 1
def Input(structure):
    if isinstance(structure, KEYBDINPUT):
        return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
    else:
        raise TypeError('Cannot create INPUT structure (keyboard)!')
def Keyboard(code, flags=0):
    return Input(KEYBDINPUT(code, code, flags, 0, None))
if __name__ == '__main__':
    nb_cycles = 10
    while nb_cycles != 0:
            time.sleep(2)  # 3 seconds
            # Key "c" for debug, but ideally use 0x7E for "F15"
            SendInput(Keyboard(ord("C")))
            sys.stdout.write(".")
            nb_cycles -= 1
 
    