我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用curses.KEY_HOME。
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): if not self.items: return False if ch == curses.KEY_UP: self.selected = max(self.selected - 1, 0) elif ch == curses.KEY_DOWN: self.selected = min(len(self.selectable) - 1, self.selected + 1) elif ch == curses.KEY_HOME: self.selected = 0 elif ch == curses.KEY_END: self.selected = len(self.selectable) - 1 elif ch == curses.KEY_ENTER: key, title = self.items[self.selectable[self.selected]] self.select(key) return True # no update necessary else: return False self.update() return True
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 test_should_go_to_beginning(self): input = TextInput(max_len=10) input.put('a') input.put('b') input.put(curses.KEY_HOME) self.assertEqual(0, input.cursor)
def test_should_go_to_end(self): input = TextInput(max_len=10) input.put('a') input.put('b') input.put(curses.KEY_HOME) input.put(curses.KEY_END) self.assertEqual(2, input.cursor)
def set_up_handlers(self): super().set_up_handlers() self.add_handlers({ curses.KEY_END: self.h_end, curses.KEY_HOME: self.h_home })
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, }
def on_key(self, ch): if not self.items: return False if ch == curses.KEY_UP: if self.selected == 0: return False self.selected -= 1 # Selection is outside view, scroll up if self.selected - self.offset < 0: self.offset = self.selected elif ch == curses.KEY_DOWN: if self.selected == len(self.items) - 1: return False self.selected += 1 # Selection is outside view, scroll down if self.selected - self.offset >= self.h: self.offset = self.selected - self.h + 1 elif ch == curses.KEY_HOME: self.selected = 0 self.offset = 0 elif ch == curses.KEY_END: self.selected = len(self.items) - 1 self.offset = max(0, self.selected - self.h + 1) elif ch == curses.KEY_ENTER: key, title, action = self.items[self.selected] self.select(key) return True # no update necessary else: return False self.update() return True