Python curses.KEY_LEFT Examples
The following are 30
code examples of curses.KEY_LEFT().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
curses
, or try the search function
.
Example #1
Source File: hello_world.py From pycolab with Apache License 2.0 | 7 votes |
def main(argv=()): del argv # Unused. # Build a Hello World game. game = make_game() # Log a message in its Plot object. game.the_plot.log('Hello, world!') # Make a CursesUi to play it with. ui = human_ui.CursesUi( keys_to_actions={curses.KEY_UP: 0, curses.KEY_DOWN: 1, curses.KEY_LEFT: 2, curses.KEY_RIGHT: 3, 'q': 4, 'Q': 4, -1: 5}, delay=50, colour_fg=HELLO_COLOURS) # Let the game begin! ui.play(game)
Example #2
Source File: prompters.py From questionnaire with MIT License | 6 votes |
def one(prompt, *args, **kwargs): """Instantiates a picker, registers custom handlers for going back, and starts the picker. """ indicator = '‣' if sys.version_info < (3, 0): indicator = '>' def go_back(picker): return None, -1 options, verbose_options = prepare_options(args) idx = kwargs.get('idx', 0) picker = Picker(verbose_options, title=prompt, indicator=indicator, default_index=idx) picker.register_custom_handler(ord('h'), go_back) picker.register_custom_handler(curses.KEY_LEFT, go_back) with stdout_redirected(sys.stderr): option, index = picker.start() if index == -1: raise QuestionnaireGoBack if kwargs.get('return_index', False): # `one` was called by a special client, e.g. `many` return index return options[index]
Example #3
Source File: snake-game-OOP.py From Learning-Python-by-building-games with MIT License | 6 votes |
def __init__(self, x, y, window): self.body_list = [] self.hit_score = 0 self.timeout = TIMEOUT for i in range(SNAKE_LENGTH, 0, -1): self.body_list.append(Body(x - i, y)) self.body_list.append(Body(x, y, '#')) self.window = window self.direction = KEY_RIGHT self.last_head_coor = (x, y) self.direction_map = { KEY_UP: self.move_up, KEY_DOWN: self.move_down, KEY_LEFT: self.move_left, KEY_RIGHT: self.move_right }
Example #4
Source File: better_scrolly_maze.py From pycolab with Apache License 2.0 | 6 votes |
def main(argv=()): level = int(argv[1]) if len(argv) > 1 else 0 # Build a Better Scrolly Maze game. game = make_game(level) # Build the croppers we'll use to scroll around in it, etc. croppers = make_croppers(level) # Make a CursesUi to play it with. ui = human_ui.CursesUi( keys_to_actions={curses.KEY_UP: 0, curses.KEY_DOWN: 1, curses.KEY_LEFT: 2, curses.KEY_RIGHT: 3, -1: 4, 'q': 5, 'Q': 5}, delay=100, colour_fg=COLOUR_FG, colour_bg=COLOUR_BG, croppers=croppers) # Let the game begin! ui.play(game)
Example #5
Source File: extraterrestrial_marauders.py From pycolab with Apache License 2.0 | 6 votes |
def main(argv=()): del argv # Unused. # Build an Extraterrestrial Marauders game. game = make_game() # Build an ObservationCharacterRepainter that will make laser bolts of the # same type all look identical. repainter = rendering.ObservationCharacterRepainter(LASER_REPAINT_MAPPING) # Make a CursesUi to play it with. ui = human_ui.CursesUi( keys_to_actions={curses.KEY_LEFT: 0, curses.KEY_RIGHT: 1, ' ': 2, # shoot -1: 3, # no-op 'q': 4}, # quit repainter=repainter, delay=300, colour_fg=COLOURS_FG, colour_bg=COLOURS_BG) # Let the game begin! ui.play(game)
Example #6
Source File: main.py From dm-snake with MIT License | 6 votes |
def __init__(self, x, y, window): self.body_list = [] self.hit_score = 0 self.timeout = TIMEOUT # buat body snake for i in range(SNAKE_LENGTH, 0, -1): self.body_list.append(Body(x - i, y)) # buat kepala snake self.body_list.append(Body(x, y, '@')) self.window = window self.direction = KEY_RIGHT self.last_head_coor = (x, y) self.direction_map = { KEY_UP: self.move_up, KEY_DOWN: self.move_down, KEY_LEFT: self.move_left, KEY_RIGHT: self.move_right }
Example #7
Source File: wgwidget.py From EDCOP with Apache License 2.0 | 6 votes |
def set_up_handlers(self): """This function should be called somewhere during object initialisation (which all library-defined widgets do). You might like to override this in your own definition, but in most cases the add_handers or add_complex_handlers methods are what you want.""" #called in __init__ self.handlers = { curses.ascii.NL: self.h_exit_down, curses.ascii.CR: self.h_exit_down, curses.ascii.TAB: self.h_exit_down, curses.KEY_BTAB: self.h_exit_up, curses.KEY_DOWN: self.h_exit_down, curses.KEY_UP: self.h_exit_up, curses.KEY_LEFT: self.h_exit_left, curses.KEY_RIGHT: self.h_exit_right, "^P": self.h_exit_up, "^N": self.h_exit_down, curses.ascii.ESC: self.h_exit_escape, curses.KEY_MOUSE: self.h_exit_mouse, } self.complex_handlers = []
Example #8
Source File: sequence_recall.py From pycolab with Apache License 2.0 | 6 votes |
def main(argv): del argv # Unused. # Build a sequence_recall game. game = make_game(FLAGS.sequence_length, FLAGS.demo_light_on_frames, FLAGS.demo_light_off_frames, FLAGS.pause_frames, FLAGS.timeout_frames) # Build an ObservationCharacterRepainter that will turn the light numbers into # actual colours. repainter = rendering.ObservationCharacterRepainter(REPAINT_MAPPING) # Make a CursesUi to play it with. ui = human_ui.CursesUi( keys_to_actions={curses.KEY_UP: 1, curses.KEY_DOWN: 2, curses.KEY_LEFT: 3, curses.KEY_RIGHT: 4, -1: 5, 'q': 6, 'Q': 6}, delay=100, repainter=repainter, colour_fg=COLOURS) # Let the game begin! ui.play(game)
Example #9
Source File: t_maze.py From pycolab with Apache License 2.0 | 6 votes |
def main(argv): del argv # Unused. # Build a t_maze game. game = make_game(FLAGS.difficulty, FLAGS.cue_after_teleport, FLAGS.timeout_frames, FLAGS.teleport_delay, FLAGS.limbo_time) # Build an ObservationCharacterRepainter that will make the teleporter and all # the goals look identical. repainter = rendering.ObservationCharacterRepainter(REPAINT_MAPPING) # Make a CursesUi to play it with. ui = human_ui.CursesUi( keys_to_actions={curses.KEY_UP: 1, curses.KEY_DOWN: 2, curses.KEY_LEFT: 3, curses.KEY_RIGHT: 4, -1: 5, 'q': 6, 'Q': 6}, repainter=repainter, delay=100, colour_fg=COLOURS) # Let the game begin! ui.play(game)
Example #10
Source File: apprehend.py From pycolab with Apache License 2.0 | 6 votes |
def main(argv=()): del argv # Unused. # Build an Apprehend game. game = make_game() # Build an ObservationCharacterRepainter that will make the player and the # ball look identical. repainter = rendering.ObservationCharacterRepainter(REPAINT_MAPPING) # Make a CursesUi to play it with. ui = human_ui.CursesUi( keys_to_actions={curses.KEY_LEFT: 0, curses.KEY_RIGHT: 1, -1: 2}, repainter=repainter, delay=500, colour_fg=COLOURS) # Let the game begin! ui.play(game)
Example #11
Source File: wgtextbox.py From EDCOP with Apache License 2.0 | 6 votes |
def set_up_handlers(self): super(Textfield, self).set_up_handlers() # For OS X del_key = curses.ascii.alt('~') self.handlers.update({curses.KEY_LEFT: self.h_cursor_left, curses.KEY_RIGHT: self.h_cursor_right, curses.KEY_DC: self.h_delete_right, curses.ascii.DEL: self.h_delete_left, curses.ascii.BS: self.h_delete_left, curses.KEY_BACKSPACE: self.h_delete_left, # mac os x curses reports DEL as escape oddly # no solution yet "^K": self.h_erase_right, "^U": self.h_erase_left, }) self.complex_handlers.extend(( (self.t_input_isprint, self.h_addch), # (self.t_is_ck, self.h_erase_right), # (self.t_is_cu, self.h_erase_left), ))
Example #12
Source File: safety_ui.py From ai-safety-gridworlds with Apache License 2.0 | 6 votes |
def make_human_curses_ui(game_bg_colours, game_fg_colours, delay=100): """Instantiate a Python Curses UI for the terminal game. Args: game_bg_colours: dict of game element background colours. game_fg_colours: dict of game element foreground colours. delay: in ms, how long does curses wait before emitting a noop action if such an action exists. If it doesn't it just waits, so this delay has no effect. Our situation is the latter case, as we don't have a noop. Returns: A curses UI game object. """ return SafetyCursesUi( keys_to_actions={curses.KEY_UP: Actions.UP, curses.KEY_DOWN: Actions.DOWN, curses.KEY_LEFT: Actions.LEFT, curses.KEY_RIGHT: Actions.RIGHT, 'q': Actions.QUIT, 'Q': Actions.QUIT}, delay=delay, repainter=None, colour_fg=game_fg_colours, colour_bg=game_bg_colours)
Example #13
Source File: wrapper.py From BinderFilter with MIT License | 6 votes |
def wrapper(func, *args, **kwds): """Wrapper function that initializes curses and calls another function, restoring normal keyboard/screen behavior on error. The callable object 'func' is then passed the main window 'stdscr' as its first argument, followed by any other arguments passed to wrapper(). """ try: # Initialize curses stdscr = curses.initscr() # Turn off echoing of keys, and enter cbreak mode, # where no buffering is performed on keyboard input curses.noecho() curses.cbreak() # In keypad mode, escape sequences for special keys # (like the cursor keys) will be interpreted and # a special value like curses.KEY_LEFT will be returned stdscr.keypad(1) # Start color, too. Harmless if the terminal doesn't have # color; user can test with has_color() later on. The try/catch # works around a minor bit of over-conscientiousness in the curses # module -- the error return from C start_color() is ignorable. try: curses.start_color() except: pass return func(stdscr, *args, **kwds) finally: # Set everything back to normal if 'stdscr' in locals(): stdscr.keypad(0) curses.echo() curses.nocbreak() curses.endwin()
Example #14
Source File: test_keyboard.py From deepWordBug with Apache License 2.0 | 6 votes |
def test_cuf1_and_cub1_as_RIGHT_LEFT(all_terms): "Test that cuf1 and cub1 are assigned KEY_RIGHT and KEY_LEFT." from blessed.keyboard import get_keyboard_sequences @as_subprocess def child(kind): term = TestTerminal(kind=kind, force_styling=True) keymap = get_keyboard_sequences(term) if term._cuf1: assert term._cuf1 in keymap assert keymap[term._cuf1] == term.KEY_RIGHT if term._cub1: assert term._cub1 in keymap if term._cub1 == '\b': assert keymap[term._cub1] == term.KEY_BACKSPACE else: assert keymap[term._cub1] == term.KEY_LEFT child(all_terms)
Example #15
Source File: keyboard.py From deepWordBug with Apache License 2.0 | 6 votes |
def _alternative_left_right(term): r""" Determine and return mapping of left and right arrow keys sequences. :arg blessed.Terminal term: :class:`~.Terminal` instance. :rtype: dict This function supports :func:`get_terminal_sequences` to discover the preferred input sequence for the left and right application keys. Return dict of sequences ``term._cuf1``, and ``term._cub1``, valued as ``KEY_RIGHT``, ``KEY_LEFT`` (when appropriate). It is necessary to check the value of these sequences to ensure we do not use ``u' '`` and ``u'\b'`` for ``KEY_RIGHT`` and ``KEY_LEFT``, preferring their true application key sequence, instead. """ keymap = dict() if term._cuf1 and term._cuf1 != u' ': keymap[term._cuf1] = curses.KEY_RIGHT if term._cub1 and term._cub1 != u'\b': keymap[term._cub1] = curses.KEY_LEFT return keymap
Example #16
Source File: keyboard.py From deepWordBug with Apache License 2.0 | 6 votes |
def resolve_sequence(text, mapper, codes): r""" Return :class:`Keystroke` instance for given sequence ``text``. The given ``text`` may extend beyond a matching sequence, such as ``u\x1b[Dxxx`` returns a :class:`Keystroke` instance of attribute :attr:`Keystroke.sequence` valued only ``u\x1b[D``. It is up to determine that ``xxx`` remains unresolved. :arg text: string of characters received from terminal input stream. :arg OrderedDict mapper: unicode multibyte sequences, such as ``u'\x1b[D'`` paired by their integer value (260) :arg dict codes: a :type:`dict` of integer values (such as 260) paired by their mnemonic name, such as ``'KEY_LEFT'``. :rtype: Keystroke """ for sequence, code in mapper.items(): if text.startswith(sequence): return Keystroke(ucs=sequence, code=code, name=codes[code]) return Keystroke(ucs=text and text[0] or u'')
Example #17
Source File: screen.py From wpm with GNU Affero General Public License v3.0 | 6 votes |
def _get_key_py33(self): """Python 3.3+ implementation of get_key.""" # pylint: disable=too-many-return-statements try: # Curses in Python 3.3 handles unicode via get_wch key = self.window.get_wch() if isinstance(key, int): if key == curses.KEY_BACKSPACE: return "KEY_BACKSPACE" if key == curses.KEY_LEFT: return "KEY_LEFT" if key == curses.KEY_RIGHT: return "KEY_RIGHT" if key == curses.KEY_RESIZE: return "KEY_RESIZE" return None return key except curses.error: return None except KeyboardInterrupt: raise
Example #18
Source File: wgwidget.py From HomePWN with GNU General Public License v3.0 | 6 votes |
def set_up_handlers(self): """This function should be called somewhere during object initialisation (which all library-defined widgets do). You might like to override this in your own definition, but in most cases the add_handers or add_complex_handlers methods are what you want.""" #called in __init__ self.handlers = { curses.ascii.NL: self.h_exit_down, curses.ascii.CR: self.h_exit_down, curses.ascii.TAB: self.h_exit_down, curses.KEY_BTAB: self.h_exit_up, curses.KEY_DOWN: self.h_exit_down, curses.KEY_UP: self.h_exit_up, curses.KEY_LEFT: self.h_exit_left, curses.KEY_RIGHT: self.h_exit_right, # "^P": self.h_exit_up, # "^N": self.h_exit_down, curses.ascii.ESC: self.h_exit_escape, curses.KEY_MOUSE: self.h_exit_mouse, } self.complex_handlers = []
Example #19
Source File: wgwidget.py From apple_bleee with GNU General Public License v3.0 | 6 votes |
def set_up_handlers(self): """This function should be called somewhere during object initialisation (which all library-defined widgets do). You might like to override this in your own definition, but in most cases the add_handers or add_complex_handlers methods are what you want.""" #called in __init__ self.handlers = { curses.ascii.NL: self.h_exit_down, curses.ascii.CR: self.h_exit_down, curses.ascii.TAB: self.h_exit_down, curses.KEY_BTAB: self.h_exit_up, curses.KEY_DOWN: self.h_exit_down, curses.KEY_UP: self.h_exit_up, curses.KEY_LEFT: self.h_exit_left, curses.KEY_RIGHT: self.h_exit_right, # "^P": self.h_exit_up, # "^N": self.h_exit_down, curses.ascii.ESC: self.h_exit_escape, curses.KEY_MOUSE: self.h_exit_mouse, } self.complex_handlers = []
Example #20
Source File: keyboard.py From MARA_Framework with GNU Lesser General Public License v3.0 | 6 votes |
def resolve_sequence(text, mapper, codes): """resolve_sequence(text, mapper, codes) -> Keystroke() Returns first matching Keystroke() instance for sequences found in ``mapper`` beginning with input ``text``, where ``mapper`` is an OrderedDict of unicode multibyte sequences, such as u'\x1b[D' paired by their integer value (260), and ``codes`` is a dict of integer values (260) paired by their mnemonic name, 'KEY_LEFT'. """ for sequence, code in mapper.iteritems(): if text.startswith(sequence): return Keystroke(ucs=sequence, code=code, name=codes[code]) return Keystroke(ucs=text and text[0] or u'') # override a few curses constants with easier mnemonics, # there may only be a 1:1 mapping, so for those who desire # to use 'KEY_DC' from, perhaps, ported code, recommend # that they simply compare with curses.KEY_DC.
Example #21
Source File: keyboard.py From MARA_Framework with GNU Lesser General Public License v3.0 | 6 votes |
def _alternative_left_right(term): """_alternative_left_right(T) -> dict Return dict of sequences ``term._cuf1``, and ``term._cub1``, valued as ``KEY_RIGHT``, ``KEY_LEFT`` when appropriate if available. some terminals report a different value for *kcuf1* than *cuf1*, but actually send the value of *cuf1* for right arrow key (which is non-destructive space). """ keymap = dict() if term._cuf1 and term._cuf1 != u' ': keymap[term._cuf1] = curses.KEY_RIGHT if term._cub1 and term._cub1 != u'\b': keymap[term._cub1] = curses.KEY_LEFT return keymap
Example #22
Source File: test_keyboard.py From MARA_Framework with GNU Lesser General Public License v3.0 | 6 votes |
def test_cuf1_and_cub1_as_RIGHT_LEFT(all_terms): "Test that cuf1 and cub1 are assigned KEY_RIGHT and KEY_LEFT." from blessed.keyboard import get_keyboard_sequences @as_subprocess def child(kind): term = TestTerminal(kind=kind, force_styling=True) keymap = get_keyboard_sequences(term) if term._cuf1: assert term._cuf1 != u' ' assert term._cuf1 in keymap assert keymap[term._cuf1] == term.KEY_RIGHT if term._cub1: assert term._cub1 in keymap if term._cub1 == '\b': assert keymap[term._cub1] == term.KEY_BACKSPACE else: assert keymap[term._cub1] == term.KEY_LEFT child(all_terms)
Example #23
Source File: tui.py From python-curses-scroll-example with MIT License | 6 votes |
def input_stream(self): """Waiting an input and run a proper method according to type of input""" while True: self.display() ch = self.window.getch() if ch == curses.KEY_UP: self.scroll(self.UP) elif ch == curses.KEY_DOWN: self.scroll(self.DOWN) elif ch == curses.KEY_LEFT: self.paging(self.UP) elif ch == curses.KEY_RIGHT: self.paging(self.DOWN) elif ch == curses.ascii.ESC: break
Example #24
Source File: tui.py From awesome-finder with MIT License | 6 votes |
def input_stream(self): """Waiting an input and run a proper method according to type of input""" while True: self.search(self.query) self.display() ch = self.search_window.getch() if curses.ascii.isprint(ch): self.write(ch) self.reset_top() elif ch in (curses.ascii.BS, curses.ascii.DEL, curses.KEY_BACKSPACE): self.delete() self.reset_top() elif ch == curses.KEY_UP: self.scroll(self.UP) elif ch == curses.KEY_DOWN: self.scroll(self.DOWN) elif ch == curses.KEY_LEFT: self.paging(self.UP) elif ch == curses.KEY_RIGHT: self.paging(self.DOWN) elif ch in (curses.ascii.LF, curses.ascii.NL): self.open_link() elif ch == curses.ascii.ESC: break
Example #25
Source File: CLI.py From email_hack with MIT License | 6 votes |
def input_stream(self): """Waiting an input and run a proper method according to type of input""" while any([client.running for client in CLIENTS]): ch = self.window.getch() if ch == curses.KEY_UP: self.scroll(self.UP) elif ch == curses.KEY_DOWN: self.scroll(self.DOWN) elif ch == curses.KEY_LEFT: self.scroll(self.LEFT, horizontal=True) elif ch == curses.KEY_RIGHT: self.scroll(self.RIGHT, horizontal=True) elif ch == ord("q"): self.EXIT_FLAG = 1
Example #26
Source File: test_curses.py From memory-analyzer with MIT License | 6 votes |
def setUp(self): self.mock_curses = mock.patch( "memory_analyzer.frontend.memanz_curses.curses" ).start() self.addCleanup(self.mock_curses.stop) self.mock_curses.LINES = 2 self.mock_curses.COLS = 100 self.mock_curses.KEY_DOWN = curses.KEY_DOWN self.mock_curses.KEY_UP = curses.KEY_UP self.mock_curses.KEY_PPAGE = curses.KEY_PPAGE self.mock_curses.KEY_NPAGE = curses.KEY_NPAGE self.mock_curses.KEY_RIGHT = curses.KEY_RIGHT self.mock_curses.KEY_LEFT = curses.KEY_LEFT self.statusbarstr = " | Navigate with arrows or wasd | Press 'q' to exit" self.pages = [["Page1", 10, 1024], ["Page2", 90, 100]] self.titles = ["Analysis of 1234", "Snapshot Differences"] self.win = memanz_curses.Window(self.mock_curses, self.pages, self.titles)
Example #27
Source File: wgtextbox.py From HomePWN with GNU General Public License v3.0 | 6 votes |
def set_up_handlers(self): super(Textfield, self).set_up_handlers() # For OS X del_key = curses.ascii.alt('~') self.handlers.update({curses.KEY_LEFT: self.h_cursor_left, curses.KEY_RIGHT: self.h_cursor_right, curses.KEY_DC: self.h_delete_right, curses.ascii.DEL: self.h_delete_left, curses.ascii.BS: self.h_delete_left, curses.KEY_BACKSPACE: self.h_delete_left, # mac os x curses reports DEL as escape oddly # no solution yet "^K": self.h_erase_right, "^U": self.h_erase_left, }) self.complex_handlers.extend(( (self.t_input_isprint, self.h_addch), # (self.t_is_ck, self.h_erase_right), # (self.t_is_cu, self.h_erase_left), ))
Example #28
Source File: test_keyboard.py From MARA_Framework with GNU Lesser General Public License v3.0 | 5 votes |
def test_alternative_left_right(): "Test _alternative_left_right behavior for space/backspace." from blessed.keyboard import _alternative_left_right term = mock.Mock() term._cuf1 = u'' term._cub1 = u'' assert not bool(_alternative_left_right(term)) term._cuf1 = u' ' term._cub1 = u'\b' assert not bool(_alternative_left_right(term)) term._cuf1 = u'seq-right' term._cub1 = u'seq-left' assert (_alternative_left_right(term) == { u'seq-right': curses.KEY_RIGHT, u'seq-left': curses.KEY_LEFT})
Example #29
Source File: test_keyboard.py From MARA_Framework with GNU Lesser General Public License v3.0 | 5 votes |
def test_inkey_0s_cbreak_sequence(): "0-second inkey with multibyte sequence; should decode immediately." pid, master_fd = pty.fork() if pid is 0: # child try: cov = __import__('cov_core_init').init() except ImportError: cov = None term = TestTerminal() os.write(sys.__stdout__.fileno(), SEMAPHORE) with term.cbreak(): inp = term.inkey(timeout=0) os.write(sys.__stdout__.fileno(), inp.name.encode('ascii')) sys.stdout.flush() if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): os.write(master_fd, u'\x1b[D'.encode('ascii')) read_until_semaphore(master_fd) stime = time.time() output = read_until_eof(master_fd) pid, status = os.waitpid(pid, 0) assert output == u'KEY_LEFT' assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 0.0
Example #30
Source File: wgslider.py From HomePWN with GNU General Public License v3.0 | 5 votes |
def set_up_handlers(self): super(widget.Widget, self).set_up_handlers() self.handlers.update({ curses.KEY_LEFT: self.h_decrease, curses.KEY_RIGHT: self.h_increase, ord('+'): self.h_increase, ord('-'): self.h_decrease, ord('h'): self.h_decrease, ord('l'): self.h_increase, ord('j'): self.h_exit_down, ord('k'): self.h_exit_up, })