예제 #1
0
    def execute_at_time_offset(self, start_time):
        global continue_playback, enable_playback_interruption
        now = perf_counter_ns()

        target_time = start_time + self.time_offset

        while now < target_time:
            diff_in_ms = (target_time - now) // 1000000

            if diff_in_ms > 10:
                time.sleep(diff_in_ms // 10 / 100.)

            else:
                time.sleep(1 / 1000.)

            if not continue_playback:
                return

            if enable_playback_interruption:
                winput.get_message()

            now = perf_counter_ns()

        if not continue_playback:
            return

        self.event.execute()
예제 #2
0
def playMacro(macro):
    global WAIT_BETWEEN_ACTIONS, last_time, root, IS_RELATIVE, options, screen_res, last_flip, MIN_FPS, STOP_PLAYING
    IS_RELATIVE = (IS_RELATIVE or options["relative"].get())
    total_time = 0
    start_time = time.time()
    last_time = 0
    for action in macro:
        if (STOP_PLAYING):
            break
        this_time = time.time()
        total_time += action[0]
        if (this_time - last_flip) > 1. / MIN_FPS:
            root.update()
            winput.get_message()
            last_flip = this_time

        time_delta = max(total_time - (this_time - start_time), 0)

        action_type = int(action[1])

        if WAIT_BETWEEN_ACTIONS:
            if time_delta: time.sleep(time_delta)

        if action_type in (winput.WM_KEYUP, winput.WM_SYSKEYUP,
                           winput.WM_KEYDOWN, winput.WM_SYSKEYDOWN):
            key = int(action[2])
            if action_type == winput.WM_KEYUP:  # key up
                ReleaseKey(key)
            else:
                PressKey(key)

        elif action_type == winput.WM_MOUSEMOVE:
            desired_position = (int(round(action[2] * screen_res[0], 0)),
                                int(round(action[3] * screen_res[1],
                                          0))) if IS_RELATIVE else (int(
                                              action[2]), int(action[3]))
            user32.SetCursorPos(*desired_position)

        else:
            current_mouse_position = getMousePosition()
            relative_position = (
                int(round(action[2] * screen_res[0], 0)) -
                current_mouse_position[0],
                int(round(action[3] * screen_res[1], 0)) -
                current_mouse_position[1]) if IS_RELATIVE else (
                    int(action[2]) - current_mouse_position[0],
                    int(action[3]) - current_mouse_position[1])

            mouse_change = 0x0002 if action_type == winput.WM_LBUTTONDOWN else \
                           0x0004 if action_type == winput.WM_LBUTTONUP else \
                           0x0020 if action_type == winput.WM_MBUTTONDOWN else \
                           0x0040 if action_type == winput.WM_MBUTTONUP else \
                           0x0008 if action_type == winput.WM_RBUTTONDOWN else \
                           0x0010

            MouseEvent(0x0001 + mouse_change, *relative_position)

            root.update()
예제 #3
0
def request_mouse_pos(timeout):
    global last_request_position, last_request_timestamp

    last_request_position = None

    last_request_timestamp = time.time()

    winput.hook_mouse(request_mouse_callback)

    while True:
        now = time.time()

        winput.get_message()

        if last_request_position and last_request_timestamp + timeout < now:
            break

    winput.unhook_mouse()

    return last_request_position
예제 #4
0
def request_key(timeout):
    global requested_key

    requested_key = None

    start = time.time()

    winput.hook_keyboard(request_key_callback)

    while not requested_key:
        now = time.time()

        if now >= start + timeout:
            break

        winput.get_message()

    winput.unhook_keyboard()

    return requested_key
예제 #5
0
import winput, time

def mouse_callback( event ):
    if event.action == winput.WM_LBUTTONDOWN:
        print("Left mouse button press at {}".format( event.position ))
    
def keyboard_callback( event ):
    if event.vkCode == winput.VK_ESCAPE: # quit on pressing escape
        winput.stop()
        
print("Press escape to quit")
    
# hook input    
winput.hook_mouse( mouse_callback )
winput.hook_keyboard( keyboard_callback )

# enter message loop
try:
    while 1:
        time.sleep(1./120)
        msg = (winput.get_message())
        if msg:
            break
except KeyboardInterrupt:
    pass

# remove input hook
winput.unhook_mouse()
winput.unhook_keyboard()
예제 #6
0
def create_macro(name, start_at, screen_width, screen_height):
    global start, raw_data, stop_recording_key

    mode = config.get("recording_mode", "key")

    duration = config.get("recording_duration", 10)

    stop_recording_key = config.get("recording_stop_key", winput.VK_ESCAPE)

    mouse_enabled = config.get("record_mouse", True)

    keyboard_enabled = config.get("record_keyboard", True)

    assert mode in ("timer", "key")

    assert type(duration) in (float, int) and duration > 0

    assert type(stop_recording_key) == int and 0 <= stop_recording_key <= 2**15

    assert type(mouse_enabled) == type(keyboard_enabled) == bool

    assert mouse_enabled or keyboard_enabled

    raw_data = []

    while True:
        if time.time() > start_at:
            break
        time.sleep(0.0001)

    start = perf_counter_ns()

    start_mouse_pos = None

    if mode == "timer":
        if mouse_enabled:
            start_mouse_pos = winput.get_mouse_pos()
            winput.hook_mouse(callback)

        if keyboard_enabled:
            winput.hook_keyboard(callback)

        while True:
            now = perf_counter_ns()

            if now >= start + duration * 1000000000:
                break

            winput.get_message()

    elif mode == "key":
        if mouse_enabled:
            start_mouse_pos = winput.get_mouse_pos()
            winput.hook_mouse(callback)

        if keyboard_enabled:
            winput.hook_keyboard(callback_with_stop_key)
        else:
            winput.hook_keyboard(callback_only_stop_key)

        winput.wait_messages()

    winput.unhook_mouse()
    winput.unhook_keyboard()

    return Macro.from_raw_data(name, start, start_mouse_pos, screen_width,
                               screen_height, raw_data)