def keyboard_example(layout_name: kl.LayoutName):
    window = tk.Tk()
    window.resizable(False, False)

    key_info = kl.KeyInfo(margin=10,
                          color=grey,
                          txt_color=dark_grey,
                          txt_font=tkf.Font(family='Arial',
                                            size=key_size // 4),
                          txt_padding=(key_size // 6, key_size // 10))
    keyboard = get_keyboard(window, layout_name, key_info)

    run_until_user_closes_window(window, keyboard, key_info)
 def get_infos():
     grey = pygame.Color('grey')
     key_size = 60
     letter_key_size = (key_size, key_size)  # width, height
     key_info = kl.KeyInfo(
         margin=10,
         color=grey,
         txt_color=~grey,  # invert grey
         txt_font=pygame.font.SysFont('Arial', key_size // 4),
         txt_padding=(key_size // 10, key_size // 10),
     )
     keyboard_info = kl.KeyboardInfo(position=(0, 0),
                                     padding=2,
                                     color=~grey)
     return keyboard_info, letter_key_size, key_info
def run_until_user_closes_window(
    window: tk.Tk,
    keyboard: klt.KeyboardLayout,
    released_key_info: kl.KeyInfo,
):
    pressed_key_info = kl.KeyInfo(margin=14,
                                  color='red',
                                  txt_color='white',
                                  txt_font=tkf.Font(family='Arial',
                                                    size=key_size // 4),
                                  txt_padding=(key_size // 6, key_size // 10))
    """
    in the azerty layout store the pressed state of the key ˆ (upper case ¨)
    because tkinter does not register a key up event for that key
    That key adds accents to the key pressed after this key
    """
    dead_key_pressed = False
    dead_key_keys = {kl.Key.CIRCUMFLEX, kl.Key.DIACRATICAL}

    def keyup(e):
        key = keyboard.get_key(e)
        if key is None:
            return
        keyboard.update_key(key, released_key_info)
        nonlocal dead_key_pressed
        if dead_key_pressed and key not in dead_key_keys:
            dead_key_pressed = False
            keyboard.update_key(kl.Key.CIRCUMFLEX, released_key_info)

    def keydown(e):
        key = keyboard.get_key(e)
        if key is None:
            return
        keyboard.update_key(key, pressed_key_info)
        if key in dead_key_keys:
            nonlocal dead_key_pressed
            dead_key_pressed = True

    keyboard.bind("<KeyPress>", keydown)
    keyboard.bind("<KeyRelease>", keyup)
    keyboard.focus_set()
    window.mainloop()
def keyboard_example(layout_name: kl.LayoutName):
    pygame.init()
    # block events that we don't want
    pygame.event.set_blocked(None)
    pygame.event.set_allowed([pygame.KEYDOWN, pygame.KEYUP, pygame.QUIT])

    key_size = 60
    key_info = kl.KeyInfo(margin=10,
                          color=grey,
                          txt_color=dark_grey,
                          txt_font=pygame.font.SysFont('Arial', key_size // 4),
                          txt_padding=(key_size // 6, key_size // 10))
    keyboard = get_keyboard(layout_name, key_size, key_info)

    screen = pygame.display.set_mode(
        (keyboard.rect.width, keyboard.rect.height))
    screen.fill(pygame.Color('black'))

    keyboard.draw(screen)
    pygame.display.update()
    run_until_user_closes_window(screen, keyboard, key_size, key_info)
Example #5
0
def get_keyboard(window: tk.Tk,
                 layout_name: kl.LayoutName) -> klt.KeyboardLayout:
    key_size = 60
    grey = '#bebebe'
    dark_grey = '#414141'
    keyboard_info = kl.KeyboardInfo(position=(0, 0),
                                    padding=2,
                                    color=dark_grey)
    key_info = kl.KeyInfo(margin=10,
                          color=grey,
                          txt_color=dark_grey,
                          txt_font=tkf.Font(family='Arial',
                                            size=key_size // 4),
                          txt_padding=(key_size // 6, key_size // 10))
    letter_key_size = (key_size, key_size)  # width, height
    keyboard_layout = klt.KeyboardLayout(layout_name,
                                         keyboard_info,
                                         letter_key_size,
                                         key_info,
                                         master=window)
    return keyboard_layout
Example #6
0
def get_keyboard(layout_name: kl.LayoutName) -> klp.KeyboardLayout:
    key_size = 60
    grey = pygame.Color('grey')
    keyboard_info = kl.KeyboardInfo(
        position=(0, 0),
        padding=2,
        color=~grey
    )
    key_info = kl.KeyInfo(
        margin=10,
        color=grey,
        txt_color=~grey,  # invert grey
        txt_font=pygame.font.SysFont('Arial', key_size//4),
        txt_padding=(key_size//6, key_size//10)
    )
    letter_key_size = (key_size, key_size)  # width, height
    keyboard_layout = klp.KeyboardLayout(
        layout_name,
        keyboard_info,
        letter_key_size,
        key_info
    )
    return keyboard_layout
def run_until_user_closes_window(
    screen: pygame.Surface,
    keyboard: klp.KeyboardLayout,
    key_size: int,
    released_key_info: kl.KeyInfo,
):
    pressed_key_info = kl.KeyInfo(margin=14,
                                  color=pygame.Color('red'),
                                  txt_color=pygame.Color('white'),
                                  txt_font=pygame.font.SysFont(
                                      'Arial', key_size // 4),
                                  txt_padding=(key_size // 6, key_size // 10))

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                break
            elif event.key == pygame.K_ESCAPE:
                playing = False
                break

            key_name = pygame.key.name(event.key)
            key = keyboard.get_key(event)
            if key is None:
                continue

            if event.type == pygame.KEYDOWN:
                keyboard.update_key(key, pressed_key_info)
            elif event.type == pygame.KEYUP:
                keyboard.update_key(key, released_key_info)
            keyboard.draw(screen)
            pygame.display.update()

    pygame.display.quit()
    pygame.quit()
    def test_colored_example(self):
        TestCase = namedtuple('TestCase',
                              'key_color keyboard_color overrides txt_color')
        key_size = 32
        margin = 2
        first_letter = kl.KeyInfo(
            margin=0,
            color=pygame.Color('cyan'),
            txt_color=pygame.Color('black'),
            txt_font=pygame.font.SysFont('Arial', key_size // 4),
            txt_padding=(key_size // 10, key_size // 10),
        )
        second_letter = kl.KeyInfo(
            margin=0,
            color=pygame.Color('magenta'),
            txt_color=pygame.Color('black'),
            txt_font=pygame.font.SysFont('Arial', key_size // 4),
            txt_padding=(key_size // 10, key_size // 10),
        )
        third_letter = kl.KeyInfo(
            margin=0,
            color=pygame.Color('yellow'),
            txt_color=pygame.Color('black'),
            txt_font=pygame.font.SysFont('Arial', key_size // 4),
            txt_padding=(key_size // 10, key_size // 10),
        )
        third_letter_small = kl.KeyInfo(
            margin=14,
            color=pygame.Color('yellow'),
            txt_color=pygame.Color('black'),
            txt_font=pygame.font.SysFont('Arial', key_size // 4),
            txt_padding=(key_size // 10, key_size // 10),
        )
        overrides = {
            # spells HI!
            # H
            "q": first_letter,
            'a': first_letter,
            'z': first_letter,
            's': first_letter,
            'e': first_letter,
            'd': first_letter,
            'c': first_letter,
            # I
            't': second_letter,
            'y': second_letter,
            'u': second_letter,
            'h': second_letter,
            'b': second_letter,
            'n': second_letter,
            'm': second_letter,
            # !
            'o': third_letter,
            'l': third_letter,
            '.': third_letter_small,
        }
        test_cases = [
            TestCase(key_color='lightgoldenrod',
                     keyboard_color='orangered',
                     txt_color='orangered',
                     overrides=None),
            TestCase(key_color='pink',
                     keyboard_color='deeppink',
                     txt_color='deeppink',
                     overrides=None),
            TestCase(key_color='cornflowerblue',
                     keyboard_color='darkblue',
                     txt_color='darkblue',
                     overrides=None),
            TestCase(key_color='black',
                     keyboard_color='grey20',
                     txt_color='grey80',
                     overrides=overrides),
        ]
        keyboards = []
        letter_key_size = (key_size, key_size)  # width, height
        y = 0
        layout_name = kl.LayoutName.QWERTY
        for test_case in test_cases:
            key_info = kl.KeyInfo(
                margin=margin,
                color=pygame.Color(test_case.key_color),
                txt_color=pygame.Color(test_case.txt_color),
                txt_font=pygame.font.SysFont('Arial', key_size // 4),
                txt_padding=(key_size // 10, key_size // 10),
            )
            keyboard_info = kl.KeyboardInfo(position=(0, y),
                                            padding=2,
                                            color=pygame.Color(
                                                test_case.keyboard_color))

            keyboard = klp.KeyboardLayout(layout_name, keyboard_info,
                                          letter_key_size, key_info,
                                          test_case.overrides)
            keyboards.append(keyboard)
            y += keyboard.rect.height

        screen = pygame.display.set_mode((keyboards[0].rect.width, y))
        screen.fill(pygame.Color('black'))

        for keyboard in keyboards:
            keyboard.draw(screen)
        pygame.display.update()
        pygame.image.save(
            screen, self.sample_images_folder +
            "{}_colored.jpg".format(layout_name.value))