本文整理匯總了Python中readchar.readkey方法的典型用法代碼示例。如果您正苦於以下問題:Python readchar.readkey方法的具體用法?Python readchar.readkey怎麽用?Python readchar.readkey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類readchar
的用法示例。
在下文中一共展示了readchar.readkey方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_input
# 需要導入模塊: import readchar [as 別名]
# 或者: from readchar import readkey [as 別名]
def get_input(self):
""" Retrieve input from user with every keystroke buffered """
while True:
keypress = readkey()
if keypress == key.CTRL_C:
raise KeyboardInterrupt('Ctrl + C combination detected')
if keypress in (key.ENTER, key.CR):
print('\n\r', end='')
break
if keypress in (key.UP, key.DOWN, key.LEFT, key.RIGHT):
continue
if keypress == key.BACKSPACE:
self.buffer = self.buffer[:-1]
else:
self.buffer = self.buffer + keypress
print("\r\033[K" + self.buffer, end='', flush=True)
result = str(self.buffer)
self.buffer = ""
return result
示例2: display_help
# 需要導入模塊: import readchar [as 別名]
# 或者: from readchar import readkey [as 別名]
def display_help():
# Clear screen and show the help text
call(['clear'])
puts(colored.cyan('Available commands (press any key to exit)'))
puts(' enter - Connect to your selection')
puts(' crtl+c | q - Quit sshmenu')
puts(' k (up) - Move your selection up')
puts(' j (down) - Move your selection down')
puts(' h - Show help menu')
puts(' c - Create new connection')
puts(' d - Delete connection')
puts(' e - Edit connection')
puts(' + (plus) - Move connection up')
puts(' - (minus) - Move connection down')
# Hang until we get a keypress
readchar.readkey()
示例3: select
# 需要導入模塊: import readchar [as 別名]
# 或者: from readchar import readkey [as 別名]
def select(
options: List[str],
selected_index: int = 0) -> int:
print('\n' * (len(options) - 1))
while True:
print(f'\033[{len(options) + 1}A')
for i, option in enumerate(options):
print('\033[K{}{}'.format(
'\033[1m[\033[32;1m x \033[0;1m]\033[0m ' if i == selected_index else
'\033[1m[ ]\033[0m ', option))
keypress = readchar.readkey()
if keypress == readchar.key.UP:
new_index = selected_index
while new_index > 0:
new_index -= 1
selected_index = new_index
break
elif keypress == readchar.key.DOWN:
new_index = selected_index
while new_index < len(options) - 1:
new_index += 1
selected_index = new_index
break
elif keypress == readchar.key.ENTER:
break
elif keypress == readchar.key.CTRL_C:
raise KeyboardInterrupt
return selected_index
示例4: __init__
# 需要導入模塊: import readchar [as 別名]
# 或者: from readchar import readkey [as 別名]
def __init__(self, *data, raise_on_empty=True):
cutie.readchar.readkey = yield_input(*data, raise_on_empty=raise_on_empty)
示例5: __exit__
# 需要導入模塊: import readchar [as 別名]
# 或者: from readchar import readkey [as 別名]
def __exit__(self, *a):
cutie.readchar.readkey = readchar.readkey
示例6: select
# 需要導入模塊: import readchar [as 別名]
# 或者: from readchar import readkey [as 別名]
def select(
options: List[str],
caption_indices: Optional[List[int]] = None,
deselected_prefix: str = '\033[1m[ ]\033[0m ',
selected_prefix: str = '\033[1m[\033[32;1mx\033[0;1m]\033[0m ',
caption_prefix: str = '',
selected_index: int = 0,
confirm_on_select: bool = True) -> int:
"""Select an option from a list.
Args:
options (List[str]): The options to select from.
caption_indices (List[int], optional): Non-selectable indices.
deselected_prefix (str, optional): Prefix for deselected option ([ ]).
selected_prefix (str, optional): Prefix for selected option ([x]).
caption_prefix (str, optional): Prefix for captions ().
selected_index (int, optional): The index to be selected at first.
confirm_on_select (bool, optional): Select keys also confirm.
Returns:
int: The index that has been selected.
"""
print('\n' * (len(options) - 1))
if caption_indices is None:
caption_indices = []
while True:
print(f'\033[{len(options) + 1}A')
for i, option in enumerate(options):
if i not in caption_indices:
print('\033[K{}{}'.format(
selected_prefix if i == selected_index else
deselected_prefix, option))
elif i in caption_indices:
print('\033[K{}{}'.format(caption_prefix, options[i]))
keypress = readchar.readkey()
if keypress in DefaultKeys.up:
new_index = selected_index
while new_index > 0:
new_index -= 1
if new_index not in caption_indices:
selected_index = new_index
break
elif keypress in DefaultKeys.down:
new_index = selected_index
while new_index < len(options) - 1:
new_index += 1
if new_index not in caption_indices:
selected_index = new_index
break
elif keypress in DefaultKeys.confirm or \
confirm_on_select and keypress in DefaultKeys.select:
break
elif keypress in DefaultKeys.interrupt:
raise KeyboardInterrupt
return selected_index