我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用curses.KEY_DC。
def put(self, char): pos = self._cursor.position cnt = len(self._text) if isinstance(char, str) and cnt < self._max_len and ord(char) >= 0x20: self._text = self._text[:pos] + char + self._text[pos:] self._cursor.count = len(self._text) self._cursor.position = pos + 1 elif char == curses.KEY_LEFT and pos > 0: self._cursor.position = pos - 1 elif char == curses.KEY_RIGHT and pos < cnt: self._cursor.position = pos + 1 elif char == curses.KEY_HOME: self._cursor.position = 0 elif char == curses.KEY_END: self._cursor.position = cnt elif char == curses.KEY_BACKSPACE and pos > 0: self._text = self._text[:pos - 1] + self._text[pos:] self._cursor.count = len(self._text) self._cursor.position = pos - 1 elif char == curses.KEY_DC: self._text = self._text[:pos] + self._text[pos + 1:] self._cursor.count = len(self._text)
def on_key(self, ch): x = self.pos # ascii 32-126 (inclusive) if curses.ascii.isprint(ch): if len(self.value) < self.max_length: self.value = self.value[:x] + chr(ch) + self.value[x:] self.pos += 1 elif ch == curses.KEY_LEFT: if x > 0: self.pos -= 1 elif ch == curses.KEY_RIGHT: if x < len(self.value): self.pos += 1 elif ch == curses.KEY_BACKSPACE: if x > 0: self.value = self.value[:x - 1] + self.value[x:] self.pos -= 1 elif ch == curses.KEY_DC: if x < len(self.value): self.value = self.value[:x] + self.value[x + 1:] elif ch == curses.KEY_HOME: self.pos = 0 elif ch == curses.KEY_END: self.pos = len(self.value) else: return False self.update() return True
def _sanitise_key(k): if k in (127, curses.KEY_BACKSPACE, curses.KEY_DC): k = solent_keycode('backspace') return k
def test_should_delete_char_at_cursor(self): input = TextInput(max_len=10) input.put('a') input.put('b') input.put(curses.KEY_LEFT) input.put(curses.KEY_LEFT) input.put(curses.KEY_DC) self.assertEqual('b', input.text) self.assertEqual(0, input.cursor)
def get_keyboard_codes(): """ Return mapping of keycode integer values paired by their curses key-name. :rtype: dict Returns dictionary of (code, name) pairs for curses keyboard constant values and their mnemonic name. Such as key ``260``, with the value of its identity, ``u'KEY_LEFT'``. These are derived from the attributes by the same of the curses module, with the following exceptions: * ``KEY_DELETE`` in place of ``KEY_DC`` * ``KEY_INSERT`` in place of ``KEY_IC`` * ``KEY_PGUP`` in place of ``KEY_PPAGE`` * ``KEY_PGDOWN`` in place of ``KEY_NPAGE`` * ``KEY_ESCAPE`` in place of ``KEY_EXIT`` * ``KEY_SUP`` in place of ``KEY_SR`` * ``KEY_SDOWN`` in place of ``KEY_SF`` This function is the inverse of :func:`get_curses_keycodes`. With the given override "mixins" listed above, the keycode for the delete key will map to our imaginary ``KEY_DELETE`` mnemonic, effectively erasing the phrase ``KEY_DC`` from our code vocabulary for anyone that wishes to use the return value to determine the key-name by keycode. """ keycodes = OrderedDict(get_curses_keycodes()) keycodes.update(CURSES_KEYCODE_OVERRIDE_MIXIN) # invert dictionary (key, values) => (values, key), preferring the # last-most inserted value ('KEY_DELETE' over 'KEY_DC'). return dict(zip(keycodes.values(), keycodes.keys()))
def start(self): """ start iterates the file and paints the text on the screen. exits when q is pressed. """ try: fileptr = 0 key = '' lines = 1 while True: # get the cursor position for further manipulation y, x = self.stdscr.getyx() key = self.stdscr.getch() if key == curses.KEY_BACKSPACE or key == curses.KEY_DC or \ key == curses.KEY_DL or key == 127 : # handle backspace if x == 0 and y == 0: continue # take the file pointer back one step fileptr -= 1 # update the screen if x == 0: lines -= 1 self.stdscr.addstr(y-1, len(self.virtualfile[lines-1]), ' ') self.stdscr.move(y-1, len(self.virtualfile[lines-1])) else: self.stdscr.addstr(y, x-1, ' ') self.stdscr.move(y, x-1) elif key == curses.KEY_UP or key == curses.KEY_DOWN or \ key == curses.KEY_RESIZE or key == -1: # ignore continue else: text = self.get_text(fileptr) # increase the lines if there are "\n" s lines += sum([1 if c == '\n' else 0 for c in text]) fileptr += self.n self.stdscr.addstr(text) self.stdscr.refresh() # graceful exit curses.endwin() except KeyboardInterrupt: curses.endwin() exit()
def keys_init(self): """Define methods for each key. """ self.keys = { curses.KEY_BACKSPACE: self.backspace, CTRL('h'): self.backspace, curses.ascii.BS: self.backspace, curses.ascii.DEL: self.backspace, curses.ascii.ETX: self.close, curses.KEY_DC: self.del_char, CTRL('d'): self.del_char, CTRL('u'): self.del_to_bol, CTRL('k'): self.del_to_eol, curses.KEY_DOWN: self.down, CTRL('n'): self.down, curses.KEY_END: self.end, CTRL('e'): self.end, curses.KEY_F1: self.help, curses.KEY_HOME: self.home, CTRL('a'): self.home, curses.KEY_ENTER: self.insert_line_or_quit, curses.ascii.NL: self.insert_line_or_quit, curses.ascii.LF: self.insert_line_or_quit, "\n": self.insert_line_or_quit, curses.KEY_LEFT: self.left, CTRL('b'): self.left, curses.KEY_NPAGE: self.page_down, curses.KEY_PPAGE: self.page_up, CTRL('v'): self.paste, CTRL('x'): self.quit, curses.KEY_F2: self.quit, curses.KEY_F3: self.quit_nosave, curses.ascii.ESC: self.quit_nosave, curses.KEY_RESIZE: self.resize, -1: self.resize, curses.KEY_RIGHT: self.right, CTRL('f'): self.right, curses.KEY_UP: self.up, CTRL('p'): self.up, }