예제 #1
0
파일: keyboard.py 프로젝트: byunei/keyboard
def play(events, speed_factor=1.0):
    """
    Plays a sequence of recorded events, maintaining the relative time
    intervals. If speed_factor is invalid (<= 0) the actions are replayed
    instantly.
    """
    last_time = None
    for event in events:
        if speed_factor > 0 and last_time is not None:
            time.sleep((event.time - last_time) / speed_factor)
        last_time = event.time

        if event.event_type == KEY_DOWN:
            os_keyboard.press(event.scan_code)
        else:
            os_keyboard.release(event.scan_code)
예제 #2
0
파일: keyboard.py 프로젝트: byunei/keyboard
def write(text, delay=0):
    """
    Sends artificial keyboard events to the OS, simulating the typing of a given
    text. Composite characters such as à are not available. Raises ValueError
    for unavailable characters.
    """
    for letter in text:
        scan_code, shift = os_keyboard.map_char(letter)
        if shift:
            send('shift', True, False)
        os_keyboard.press(scan_code)
        os_keyboard.release(scan_code)
        if shift:
            send('shift', False, True)
        if delay:
            time.sleep(delay)
예제 #3
0
파일: keyboard.py 프로젝트: byunei/keyboard
def send(combination, do_press=True, do_release=True):
    """
    Performs a given hotkey combination.

    Ex: "ctrl+alt+del", "alt+F4, enter", "shift+s"
    """
    for step in _split_combination(combination):
        get_scan_code = os_keyboard.scan_code_table.get_scan_code 
        scan_codes = [get_scan_code(normalize_name(part)) for part in step]

        if do_press:
            for scan_code in scan_codes:
                os_keyboard.press(scan_code)

        if do_release:
            for scan_code in reversed(scan_codes):
                os_keyboard.release(scan_code)