我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用curses.getmouse()。
def get_mouse(self, screen): try: id, x, y, z, bstate = curses.getmouse() screen_y, screen_x, clip_h, clip_w = self.clip_size() y -= screen_y x -= screen_x # Ignore clicks outside clipped screen if y < clip_h and x < clip_w: return self.mouse_target(screen, y, x) except curses.error: pass
def input(self, engine): '''This method is called by `engine.play_game`. It receives a character from the user and interprets it. Invokes `self.travel` for the steering of the cursor. It doesn't do any output except for printing the field initialization message, and forcing the entire screen to be redrawn on unrecognised input (to de-fuck-up the screen). ''' if self.gametype == 'hex': direction_keys = self.direction_keys['hex'] else: direction_keys = self.direction_keys['square'] look_for = ['reveal', 'flag', 'toggle-attention'] + direction_keys # Receive input from player. ch = self.window.getch() # Interpret. command = None if ch == curses.KEY_MOUSE: _, x, y, _, buttons = curses.getmouse() valid = True if self.gametype == 'hex': valid = self.mouse_travel_hex(x, y, engine.field) else: valid = self.mouse_travel_square(x, y, engine.field) if valid: for tmp_command in ('flag', 'reveal'): for mask in self.cfg['curses-mouse-input'][tmp_command]: if buttons & mask: command = tmp_command else: continue break else: # Keyboard input: for key in look_for: if ch in self.cfg['curses-input'][key]: command = key # Act. if command == 'flag': engine.flag(self.cursor) elif command == 'reveal': pre_game = engine.game_status == 'pre-game' if pre_game: self.message('Initializing field... This may take a while.') curses.reset_shell_mode() # BUG: see comments above __init__ engine.reveal(self.cursor) if pre_game: curses.reset_prog_mode() # BUG: see comments above __init__ # Clear junk that gets on the screen from impatient players. self.window.redrawwin() elif command in direction_keys: self.travel(engine.field, command) elif command == 'toggle-attention': self.attention_mode = not self.attention_mode elif ch != curses.KEY_MOUSE: # Don't do this all the time, that'd be a little wasteful. self.window.redrawwin()
def _encode_mouse_event(self): # convert to escape sequence last = next = self.last_bstate (id,x,y,z,bstate) = curses.getmouse() mod = 0 if bstate & curses.BUTTON_SHIFT: mod |= 4 if bstate & curses.BUTTON_ALT: mod |= 8 if bstate & curses.BUTTON_CTRL: mod |= 16 l = [] def append_button( b ): b |= mod l.extend([ 27, ord('['), ord('M'), b+32, x+33, y+33 ]) if bstate & curses.BUTTON1_PRESSED and last & 1 == 0: append_button( 0 ) next |= 1 if bstate & curses.BUTTON2_PRESSED and last & 2 == 0: append_button( 1 ) next |= 2 if bstate & curses.BUTTON3_PRESSED and last & 4 == 0: append_button( 2 ) next |= 4 if bstate & curses.BUTTON4_PRESSED and last & 8 == 0: append_button( 64 ) next |= 8 if bstate & curses.BUTTON1_RELEASED and last & 1: append_button( 0 + escape.MOUSE_RELEASE_FLAG ) next &= ~ 1 if bstate & curses.BUTTON2_RELEASED and last & 2: append_button( 1 + escape.MOUSE_RELEASE_FLAG ) next &= ~ 2 if bstate & curses.BUTTON3_RELEASED and last & 4: append_button( 2 + escape.MOUSE_RELEASE_FLAG ) next &= ~ 4 if bstate & curses.BUTTON4_RELEASED and last & 8: append_button( 64 + escape.MOUSE_RELEASE_FLAG ) next &= ~ 8 if bstate & curses.BUTTON1_DOUBLE_CLICKED: append_button( 0 + escape.MOUSE_MULTIPLE_CLICK_FLAG ) if bstate & curses.BUTTON2_DOUBLE_CLICKED: append_button( 1 + escape.MOUSE_MULTIPLE_CLICK_FLAG ) if bstate & curses.BUTTON3_DOUBLE_CLICKED: append_button( 2 + escape.MOUSE_MULTIPLE_CLICK_FLAG ) if bstate & curses.BUTTON4_DOUBLE_CLICKED: append_button( 64 + escape.MOUSE_MULTIPLE_CLICK_FLAG ) if bstate & curses.BUTTON1_TRIPLE_CLICKED: append_button( 0 + escape.MOUSE_MULTIPLE_CLICK_FLAG*2 ) if bstate & curses.BUTTON2_TRIPLE_CLICKED: append_button( 1 + escape.MOUSE_MULTIPLE_CLICK_FLAG*2 ) if bstate & curses.BUTTON3_TRIPLE_CLICKED: append_button( 2 + escape.MOUSE_MULTIPLE_CLICK_FLAG*2 ) if bstate & curses.BUTTON4_TRIPLE_CLICKED: append_button( 64 + escape.MOUSE_MULTIPLE_CLICK_FLAG*2 ) self.last_bstate = next return l