def build_keymap(args):
"""
Function build_keymap returns a dictionary from curses keys to game.Keys.
"""
if args.vimkeys:
return {
ord('k'): Keys.UP,
ord('j'): Keys.DOWN,
ord('h'): Keys.LEFT,
ord('l'): Keys.RIGHT,
ord(' '): Keys.PROBE,
ord('f'): Keys.FLAG,
}
return {
curses.KEY_UP: Keys.UP,
curses.KEY_DOWN: Keys.DOWN,
curses.KEY_LEFT: Keys.LEFT,
curses.KEY_RIGHT: Keys.RIGHT,
ord('\n'): Keys.PROBE,
ord('f'): Keys.FLAG,
}
python类KEY_LEFT的实例源码
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]
def update(cls):
key = cls.getch()
if key is not None:
if key == curses.KEY_UP:
cls.PAD_Y = max(cls.PAD_Y - 1, 0)
elif key == curses.KEY_DOWN:
cls.PAD_Y = min(
cls.PAD_Y + 1,
cls.PAD_HEIGHT - (cls.HEIGHT + 1)
)
elif key == curses.KEY_LEFT:
cls.PAD_X = max(cls.PAD_X - 1, 0)
elif key == curses.KEY_RIGHT:
cls.PAD_X = min(
cls.PAD_X + 1,
cls.PAD_WIDTH - (cls.WIDTH + 1)
)
elif key == ord('q'):
cls.trigger('quit')
for y in range(cls.PAD_Y, cls.PAD_Y + cls.HEIGHT):
s = cls.MAP[y][cls.PAD_X:cls.PAD_X + cls.PAD_HEIGHT]
s = ''.join(x for x in s)
cls.addstr(s, cls.PAD_X, y)
cls.refresh()
def focus(self):
modeline.update_activeWindow("Map")
curses.curs_set(0)
while True:
event = self.mapwin.getch()
# if event == -1:
# continue
logging.info(event)
try_handle_global_event(event)
if event == curses.KEY_UP:
self.pan("up")
if event == curses.KEY_DOWN:
self.pan("down")
if event == curses.KEY_LEFT:
self.pan("left")
if event == curses.KEY_RIGHT:
self.pan("right")
if event == curses.KEY_NPAGE:
self.zoom_out(5)
if event == curses.KEY_PPAGE:
self.zoom_in(5)
def update_screen():
"""
Updates the screen each time a key is pressed.
"""
if not GUI.gui_stopped:
if GUI.key == curses.KEY_DOWN:
GUI.on_key_down()
elif GUI.key == curses.KEY_UP:
GUI.on_key_up()
elif GUI.key == curses.KEY_LEFT:
GUI.on_key_left()
elif GUI.key == curses.KEY_RIGHT:
GUI.on_key_right()
if GUI.key == ord("\n") and GUI.row_num != 0:
GUI.on_key_enter()
GUI.box.erase()
GUI.display_list()
GUI.add_bottom_menus()
GUI.screen.refresh()
GUI.box.refresh()
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, '0'))
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
}
def __init__(self, x, y, window):
self.body_list = []
self.hit_score = 0
self.timeout = TIMEOUT
#0 Append The Snake's range to the Body object for Snakes Body
for i in range(SNAKE_LENGTH, 0, -1):
self.body_list.append(Body(x - i, y))
#1 Define and append the snakes head
self.body_list.append(Body(x, y, '0'))
#2 define the window
self.window = window
#3 Move snake to right when game starts
self.direction = KEY_RIGHT
#4 set snakes lst head coordinate
self.last_head_coor = (x, y)
#5 define direction map
self.direction_map = {
KEY_UP: self.move_up,
KEY_DOWN: self.move_down,
KEY_LEFT: self.move_left,
KEY_RIGHT: self.move_right
}
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, '0'))
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
}
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, '0'))
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
}
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, '0'))
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
}
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 handle_key(self, k):
if k == ord('q'):
self.close(None)
elif k == ord('d'):
self._change_date()
elif k == ord('l'):
self._change_level()
elif k == ord('f'):
self._change_facility()
elif k == ord('h'):
self._change_host()
elif k == ord('p'):
self._change_program()
elif k == curses.KEY_NPAGE:
self._buf.go_to_next_page()
elif k == curses.KEY_PPAGE:
self._buf.go_to_previous_page()
elif k == curses.KEY_DOWN:
self._buf.go_to_next_line()
elif k == curses.KEY_UP:
self._buf.go_to_previous_line()
elif k == curses.KEY_RIGHT:
self.scroll_right()
elif k == curses.KEY_LEFT:
self.scroll_left()
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
def main(win,args):
SCSP = ScreenSpace(win)
SNK = Snake(win,args)
try:
while True:
SNK.render(SCSP,args)
SCSP.render(clear=False,ref=False)
gc = SCSP.win.getch()
if gc in [ord("a"),curses.KEY_LEFT]:
SNK.dir = SNK.turnleft()
if gc in [ord("d"),curses.KEY_RIGHT]:
SNK.dir = SNK.turnright()
if gc == ord("q"):
SCSP.destroy()
sys.exit()
SCSP.win.erase()
time.sleep(args.delay)
except KeyboardInterrupt:
pass
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()
def runDay(scr, rollover, topString, bottomString, start=None):
if(rollover):
if(start is None):
day = date.today()
else:
day = date(start.year, start.month, start.day)
else:
if(start is None):
day = datetime.date.today()
else:
day = datetime.date(start.year, start.month, start.day)
c = curses.KEY_MAX
cursor = 3
while(c != 10):
displayDay(scr, day, cursor, topString, bottomString)
c = scr.getch()
if(c == curses.KEY_RIGHT) and cursor < len(str(day))-1:
cursor += 1
if(cursor == 4 or cursor == 7):
cursor += 1
elif(c == curses.KEY_LEFT) and cursor > 0:
cursor -= 1
if(cursor == 4 or cursor == 7):
cursor -= 1
elif(c == curses.KEY_UP):
day = alterDigitDay(cursor, day, 1)
elif(c == curses.KEY_DOWN):
day = alterDigitDay(cursor, day, -1)
else:
try:
i = int(c) - 48
if(i >= 0 and i < 10):
day = updateDigitDay(cursor, day, i)
except ValueError:
pass
return datetime.date(day.year, day.month, day.day)
def runTime(scr, rollover, topString, bottomString, start=None):
if(rollover):
if(start is None):
t = time()
else:
t = time(start.hour, start.minute, start.second)
else:
if(start is None):
t = datetime.time()
else:
t = datetime.time(start.hour, start.minute, start.second)
c = curses.KEY_MAX
cursor = 3
while(c != 10):
displayTime(scr, t, cursor, topString, bottomString)
c = scr.getch()
if(c == curses.KEY_RIGHT) and cursor < len(str(t))-1:
cursor += 1
if(cursor == 2 or cursor == 5):
cursor += 1
elif(c == curses.KEY_LEFT) and cursor > 0:
cursor -= 1
if(cursor == 2 or cursor == 5):
cursor -= 1
elif(c == curses.KEY_UP):
t = alterDigitTime(cursor, t, 1)
elif(c == curses.KEY_DOWN):
t = alterDigitTime(cursor, t, -1)
else:
try:
i = int(c) - 48
if(i >= 0 and i < 10):
t = updateDigitTime(cursor, t, i)
except ValueError:
pass
return datetime.time(t.hour, t.minute, t.second)
def processkey(self, key, win):
h, w = win.getmaxyx()
if (key == ord(' ')):
weapon = self.arsenal[self.active_weapon]
if (weapon[1] != 0):
self.shoot()
if(weapon[1] != -1):
weapon[1] = max(0, weapon[1] - 1)
return True
else:
self.weapon_display_timer = 50
elif (key == curses.KEY_LEFT):
if self.angle <= pi/2:
self.angle = pi - self.angle
else:
self.move(-1)
elif (key == curses.KEY_RIGHT):
if self.angle >= pi/2:
self.angle = pi - self.angle
else:
self.move(1)
elif (key == curses.KEY_UP):
if (self.angle <= pi/2):
self.angle = min(pi/2.001, self.angle+0.01)
else:
self.angle = max(pi/1.999, self.angle-0.01)
elif (key == curses.KEY_DOWN):
if (self.angle <= pi/2):
self.angle = max(0, self.angle-0.01+pi/8)-pi/8
else:
self.angle = min(pi*9/8, self.angle+0.01)
elif (key == ord('+')):
self.power = min(1.00, self.power+0.01)
elif (key == ord('-')):
self.power = max(0.00, self.power-0.01)
elif (key in map(lambda k : ord(str(k)), range(10))):
n = int(chr(key))
self.active_weapon = (n-1) % len(self.arsenal)
self.weapon_display_timer = 50
return False
def run_main_loop(self):
self.draw_ui()
while 1:
c = self.window.getch()
if curses.keyname(c) in [b'h', b'H']:
self.show_help()
elif curses.keyname(c) in [b'p', b'P']:
self.play()
elif curses.keyname(c) in [b'q', b'Q']:
return None
elif c == 27:
self.returnString = self.dir.get_current_path()
return self.returnString
elif c == curses.KEY_ENTER or c == 10:
self.returnString = self.dir.get_current_path()
return self.returnString
elif c == curses.KEY_UP:
if self.selected_index > 0:
self.selected_index -= 1
self.draw_ui()
elif c == curses.KEY_DOWN:
if self.selected_index < self.dir.max_entries_on_level()-1:
self.selected_index += 1
self.draw_ui()
elif c == curses.KEY_LEFT:
self.leave_dir()
self.draw_ui()
elif c == curses.KEY_RIGHT:
self.enter_dir()
self.draw_ui()
elif c == curses.KEY_RESIZE:
self.draw_ui()
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()
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()
def on_key(self, ch):
if ch == curses.KEY_LEFT:
if self.selected > 0:
self.selected -= 1
elif ch == curses.KEY_RIGHT:
if self.selected + 1 < len(self.items):
self.selected += 1
else:
return False
self.value = self.items[self.selected]
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 on_key(self, ch):
if not self.active:
return
if self.selected:
if ch in (curses.ascii.ESC, curses.KEY_BACKSPACE):
self.cancel_selection()
elif ch == curses.KEY_LEFT:
if self.selection > 0 and self.selection < 4 or self.selection > 4:
self.set_selection(self.selection - 1)
elif ch == curses.KEY_RIGHT:
if self.selection < 3 or self.selection > 3 and self.selection < 7:
self.set_selection(self.selection + 1)
elif ch == curses.KEY_UP:
if self.selection > 3:
self.set_selection(self.selection - 4)
elif ch == curses.KEY_DOWN:
if self.selection < 4 and self.selected_source == 'hand':
self.set_selection(self.selection + 4)
elif ch in (curses.KEY_ENTER, curses.ascii.SP):
self.confirm_selection()
else:
if ch == curses.KEY_LEFT:
if self.selection > 0:
self.set_selection(self.selection - 1)
elif ch == curses.KEY_RIGHT:
if self.selection + 1 < len(self.selectable_sources):
self.set_selection(self.selection + 1)
elif ch in (curses.KEY_ENTER, curses.ascii.SP):
self.confirm_selection()
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()
def poll_event(self):
"""
Checks if an event happens and returns a string related to the event.
Returns -1 if nothing happened during self.screen.timeout milliseconds.
If the event is a normal (letter) key press,
the letter is returned (case sensitive)
:return: Event type
"""
self.mutex.acquire()
ch = self.screen.getch()
self.mutex.release()
if ch == -1:
return EVENT_SKIP
elif ch == 27:
return EVENT_ESC
elif ch == curses.KEY_RESIZE:
return EVENT_RESIZE
elif ch == 10 or ch == curses.KEY_ENTER:
return EVENT_ENTER
elif ch == 127 or ch == curses.KEY_BACKSPACE:
return EVENT_BACKSPACE
elif ch == curses.KEY_UP:
return EVENT_UP
elif ch == curses.KEY_DOWN:
return EVENT_DOWN
elif ch == curses.KEY_LEFT:
return EVENT_LEFT
elif ch == curses.KEY_RIGHT:
return EVENT_RIGHT
elif ch == 3:
return EVENT_CTRL_C
elif ch == 409:
return EVENT_CLICK
elif 0 <= ch < 256:
return chr(ch)
else:
return EVENT_UNHANDLED
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()
def test_should_move_cursor_left(self):
input = TextInput(max_len=10)
input.put('a')
input.put(curses.KEY_LEFT)
self.assertEqual(0, input.cursor)
def test_should_move_cursor_right(self):
input = TextInput(max_len=10)
input.put('a')
input.put(curses.KEY_LEFT)
input.put(curses.KEY_RIGHT)
self.assertEqual(1, input.cursor)
def test_should_not_move_cursor_before_left_boundary(self):
input = TextInput(max_len=10)
input.put('a')
input.put(curses.KEY_LEFT)
input.put(curses.KEY_LEFT)
self.assertEqual(0, input.cursor)