get_char(timeout=None)

Read a keypress and return the resulting character as a string.

Returns:
  • char( str ) –

    the pressed key or the key description (e.g. "home")

Source code in pzp/input_win.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def get_char(timeout: Optional[int] = None) -> str:
    """
    Read a keypress and return the resulting character as a string.

    Returns:
        char: the pressed key or the key description (e.g. "home")
    """
    ch: str = getwch(timeout)
    if ch is None:
        return ""
    elif ch == WIN_ESC or ch == NULL:  # When reading arrow/insert/del key, the first call returnx 0xe0
        keys_mapping = KEYS_MAPPING
        while keys_mapping:
            ch = ch + getwch()
            keys_mapping = {k: v for k, v in keys_mapping.items() if k.startswith(ch)}
            if len(keys_mapping) == 1 and next(iter(keys_mapping.keys())) == ch:
                result = next(iter(keys_mapping.values()))
                return result
        return ""
    return ch