def getch(self, win, etc=None): while True: ch = unicurses.wgetch(win) if ch == unicurses.KEY_RESIZE: self.render(etc) else: return ch
def getch(self, win, etc=None): while True: ch = unicurses.wgetch(win) self.log.debug("Key '%s' pressed.", self.showchar(ch)) if ch == unicurses.KEY_RESIZE: self.log.debug("Screen resize detected.") self.render(etc) else: return ch
def input(self): self.key = uc.wgetch(stdscr) if self.key == ord('q') or self.key == ord('Q'): self.close() if self.key == ord('w') or self.key == ord('W'): uc.endwin() exit()
def getch(self, win, etc=None, redact=False): while True: ch = unicurses.wgetch(win) if redact and ch not in self.ctrlchars: self.log.debug("Key '*' pressed.") else: self.log.debug("Key '%s' pressed.", self.showchar(ch)) if ch == unicurses.KEY_RESIZE: self.log.debug("Screen resize detected.") self.render(etc) else: return ch
def process_input(self): """Process all keyboard input.""" key_pressed = False for win in self.get_windows(): key = uc.wgetch(win) if key != -1: key_pressed = True self.state.process_key(key) # If we didn't press a key, kick the state anyway. if not key_pressed: self.state.process_key(None)
def show_submenu(self, key): self.open_submenu = self.menukeys[key] self.selection = self.open_submenu.panel self.selection.show() uni.update_panels() # Listen for keypress with submenu open while True: c = uni.wgetch(self.win) if c in QUIT_KEYS: self.reset() break elif c in self.open_submenu.actions: # option_keys uni.mvaddstr(win.maxy, 0, "DEBUG: Character pressed: {0}".format(c)) uni.refresh() self.open_submenu.action(c)
def close(self): prompt_start_x = self.max_x // 2 - 19 prompt_start_y = self.max_y // 3 if prompt_start_x < 1: prompt_start_x = 1 if prompt_start_y < 1: prompt_start_y = 1 win_prompt = uc.newwin(3, 38, prompt_start_y, prompt_start_x) uc.box(win_prompt, 0, 0) uc.mvwaddstr(win_prompt, 1, 1, 'Do you want to close the game? (y|n)') uc.wbkgd(win_prompt, uc.COLOR_PAIR(1)) uc.wrefresh(win_prompt) answer = uc.wgetch(stdscr) if answer == ord('y') or answer == ord('Y'): uc.endwin() exit() else: uc.delwin(win_prompt)
def input(self): self.key = uc.wgetch(stdscr) if self.key == uc.KEY_RESIZE: self.max_y, self.max_x = uc.getmaxyx(stdscr) if self.max_x > 10 and self.max_y > 10: self.init_wins() if (self.on_menu): if (self.key == uc.KEY_UP): self.menu_hi -= 1 if self.menu_hi == -1: self.menu_hi = len(self.menu) - 1 elif (self.key == uc.KEY_DOWN): self.menu_hi += 1 if self.menu_hi >= len(self.menu): self.menu_hi = 0 elif (self.key == ord('\n')): for ship in self.player.ship_list: if ship.id == self.menu[self.menu_hi].id: ship.placed = True self.new_ship_id = self.menu[self.menu_hi].id self.new_ship_len = self.menu[self.menu_hi].length self.new_ship_x = 0 self.new_ship_y = 0 self.new_ship_hor = True self.on_menu = False else: if (self.key == uc.KEY_LEFT): if self.new_ship_x - 1 >= 0: self.new_ship_x -= 1 elif (self.key == uc.KEY_RIGHT): if self.new_ship_hor: if self.new_ship_x + self.menu[ self.menu_hi].length < self.player.board.size: self.new_ship_x += 1 else: if self.new_ship_x + 1 < self.player.board.size: self.new_ship_x += 1 elif (self.key == uc.KEY_UP): if self.new_ship_y - 1 >= 0: self.new_ship_y -= 1 elif (self.key == uc.KEY_DOWN): if self.new_ship_hor: if self.new_ship_y + 1 < self.player.board.size: self.new_ship_y += 1 else: if self.new_ship_y + self.menu[ self.menu_hi].length < self.player.board.size: self.new_ship_y += 1 elif (self.key == ord('r') or self.key == ord('R')): if self.new_ship_hor: if self.new_ship_y + self.menu[ self.menu_hi].length > self.player.board.size: self.new_ship_y = self.player.board.size - self.menu[ self.menu_hi].length else: if self.new_ship_x + self.menu[ self.menu_hi].length > self.player.board.size: self.new_ship_x = self.player.board.size - self.menu[ self.menu_hi].length self.new_ship_hor = not self.new_ship_hor elif (self.key == ord('s') or self.key == ord('S')): s = self.player.board.suggest(self.menu[self.menu_hi].length) if type(s) != bool: x, y, h = s self.new_ship_x = x self.new_ship_y = y self.new_ship_hor = h elif (self.key == ord('\n')): if self.player.board.check_new_ship( self.new_ship_x, self.new_ship_y, self.menu[self.menu_hi].length, self.new_ship_hor): self.player.board.set_new_ship( self.new_ship_x, self.new_ship_y, self.menu[self.menu_hi].id, self.menu[self.menu_hi].length, self.new_ship_hor) self.num_ships_set += 1 if not self.all_set(): self.on_menu = True self.menu = self.player.left_to_set() self.menu_hi = 0 if self.key == ord('q') or self.key == ord('Q'): self.close() if self.key == ord('w') or self.key == ord('W'): uc.endwin() exit()
def main_loop(self): while True: keypress = uni.wgetch(self.screen) if keypress == 113: break
uni.curs_set(0) startx = int((80 - WIDTH) / 2) starty = int((24 - HEIGHT) / 2) menu_win = uni.newwin(HEIGHT, WIDTH, starty, startx) uni.keypad(menu_win, True) uni.mvaddstr(0, 0, "Click on Exit to quit (works best in a virtual console)") uni.refresh() print_menu(menu_win, 1) uni.mouseinterval(0) uni.mousemask(uni.ALL_MOUSE_EVENTS) msg = "MOUSE: {0}, {1}, {2}, Choice made is: {3}, Chosen string is: {4}" while True: c = uni.wgetch(menu_win) if c == uni.KEY_MOUSE: id, x, y, z, bstate = uni.getmouse() if bstate & uni.BUTTON1_PRESSED: chosen = report_choice(x + 1, y + 1) if chosen is not None: uni.mvaddstr(23, 0, msg.format( x, y, bstate, chosen, choices[chosen-1])) uni.mvaddstr(23, 0, msg.format( x, y, bstate, chosen, choices[chosen-1])) uni.clrtoeol() uni.refresh() if (chosen == -1): uni.endwin() exit(1) print_menu(menu_win, chosen)
uni.clear() uni.noecho() uni.cbreak() uni.curs_set(0) startx = int((80 - WIDTH) / 2) starty = int((24 - HEIGHT) / 2) menu_win = uni.newwin(HEIGHT, WIDTH, starty, startx) uni.keypad(menu_win, True) uni.mvaddstr( 0, 0, "Use arrow keys to go up and down, press ENTER to select a choice") uni.refresh() print_menu(menu_win, highlight) while True: keypress = uni.wgetch(menu_win) if keypress == uni.KEY_UP: if highlight == 1: highlight == n_choices else: highlight -= 1 elif keypress == uni.KEY_DOWN: if highlight == n_choices: highlight = 1 else: highlight += 1 elif keypress in [10, 459]: # ENTER is pressed choice = highlight uni.mvaddstr( 23, 0, "You chose choice {0} with choice string {1}".format( choice, choices[choice - 1]))