Demo entry 6781459

test

   

Submitted by anonymous on Jan 04, 2019 at 05:06
Language: Python 3. Code size: 5.1 kB.

import ctypes
from ctypes import wintypes
from pynput import keyboard
import time
from multiprocessing import Process, Value

user32 = ctypes.WinDLL('user32', use_last_error=True)

INPUT_MOUSE    = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2

KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP       = 0x0002
KEYEVENTF_UNICODE     = 0x0004
KEYEVENTF_SCANCODE    = 0x0008

MAPVK_VK_TO_VSC = 0

# msdn.microsoft.com/en-us/library/dd375731
VK_TAB  = 0x09
VK_MENU = 0x12

# C struct definitions

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)
        # some programs use the scan code even if KEYEVENTF_SCANCODE
        # isn't set in dwFflags, so attempt to map the correct code.
        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 _check_count(result, func, args):
    if result == 0:
        raise ctypes.WinError(ctypes.get_last_error())
    return args

user32.SendInput.errcheck = _check_count
user32.SendInput.argtypes = (wintypes.UINT, # nInputs
                             LPINPUT,       # pInputs
                             ctypes.c_int)  # cbSize

# Functions

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 send_keys():

    for i in range(10):
        for j in range(10):
            for k in range(10):
                for v in range(10):
                    PressKey(0x30+i)
                    ReleaseKey(0x30+1)
                    time.sleep(0.01)

                    PressKey(0x30+j)
                    ReleaseKey(0x30+j)
                    time.sleep(0.01)

                    PressKey(0x30+k)
                    ReleaseKey(0x30+k)
                    time.sleep(0.01)

                    PressKey(0x30+v)
                    ReleaseKey(0x30+v)
                    time.sleep(0.01)

                    PressKey(0x45)
                    ReleaseKey(0x45)
                    time.sleep(0.2)

                    yield (i, j, k, v)

def send_keys_iter(running, kill):
    iter_keys = send_keys()
    try:
        while True:
            if kill.value > 0:
                print("KILLING T FROM PROCESS")
                raise StopIteration

            if running.value:
                next(iter_keys)
    except StopIteration:
        print("DONE")
        with running.get_lock():
            running.value = 0
        

def toggle_permute():
    global T, RUNNING

    if T is None:
        restart_permute()

    with RUNNING.get_lock():
        RUNNING.value = int(not RUNNING.value)

def restart_permute():
    global T, RUNNING, KILL

    if T is not None:
        with KILL.get_lock():
            KILL.value = 1
        T.join()
        T = None

    with RUNNING.get_lock():
        RUNNING.value = 1

    with KILL.get_lock():
        KILL.value = 0

    T = Process(target=send_keys_iter, args=(RUNNING, KILL))
    T.start()


def on_press(key):
    if key == keyboard.KeyCode(char='o'):
        toggle_permute()

    elif key == keyboard.KeyCode(char='n'):
        restart_permute()

    elif key == keyboard.KeyCode(char='y'):
        exit()

    print(key)

def on_release(key):
    if key in current:
        current.remove(key)


T = None 
RUNNING = Value('i', 0)
KILL = Value('i', 0)
current = set()

if __name__ == "__main__":
    with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
        listener.join()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).