#NEWLINE# def keypress( self , size , key ) : #NEWLINE# #INDENT# "\n        Handle editing keystrokes, return others.\n\n        >>> e, size = Edit(), (20,)\n        >>> e.keypress(size, 'x')\n        >>> e.keypress(size, 'left')\n        >>> e.keypress(size, '1')\n        >>> print e.edit_text\n        1x\n        >>> e.keypress(size, 'backspace')\n        >>> e.keypress(size, 'end')\n        >>> e.keypress(size, '2')\n        >>> print e.edit_text\n        x2\n        >>> e.keypress(size, 'shift f1')\n        'shift f1'\n        " #NEWLINE# ( maxcol , ) = size #NEWLINE# p = self . edit_pos #NEWLINE# if self . valid_char ( key ) : #NEWLINE# #INDENT# if ( isinstance ( key , unicode ) and ( not isinstance ( self . _caption , unicode ) ) ) : #NEWLINE# #INDENT# key = key . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# self . insert_text ( key ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( key == 'tab' ) and self . allow_tab ) : #NEWLINE# #INDENT# key = ( ' ' * ( 8 - ( self . edit_pos % 8 ) ) ) #NEWLINE# self . insert_text ( key ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( key == 'enter' ) and self . multiline ) : #NEWLINE# #INDENT# key = '\n' #NEWLINE# self . insert_text ( key ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . _command_map [ key ] == CURSOR_LEFT ) : #NEWLINE# #INDENT# if ( p == 0 ) : #NEWLINE# #INDENT# return key #NEWLINE# #UNINDENT# p = move_prev_char ( self . edit_text , 0 , p ) #NEWLINE# self . set_edit_pos ( p ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . _command_map [ key ] == CURSOR_RIGHT ) : #NEWLINE# #INDENT# if ( p >= len ( self . edit_text ) ) : #NEWLINE# #INDENT# return key #NEWLINE# #UNINDENT# p = move_next_char ( self . edit_text , p , len ( self . edit_text ) ) #NEWLINE# self . set_edit_pos ( p ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . _command_map [ key ] in ( CURSOR_UP , CURSOR_DOWN ) ) : #NEWLINE# #INDENT# self . highlight = None #NEWLINE# ( x , y ) = self . get_cursor_coords ( ( maxcol , ) ) #NEWLINE# pref_col = self . get_pref_col ( ( maxcol , ) ) #NEWLINE# assert ( pref_col is not None ) #NEWLINE# if ( self . _command_map [ key ] == CURSOR_UP ) : #NEWLINE# #INDENT# y -= 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# y += 1 #NEWLINE# #UNINDENT# if ( not self . move_cursor_to_coords ( ( maxcol , ) , pref_col , y ) ) : #NEWLINE# #INDENT# return key #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( key == 'backspace' ) : #NEWLINE# #INDENT# self . pref_col_maxcol = ( None , None ) #NEWLINE# if ( not self . _delete_highlighted ( ) ) : #NEWLINE# #INDENT# if ( p == 0 ) : #NEWLINE# #INDENT# return key #NEWLINE# #UNINDENT# p = move_prev_char ( self . edit_text , 0 , p ) #NEWLINE# self . set_edit_text ( ( self . edit_text [ : p ] + self . edit_text [ self . edit_pos : ] ) ) #NEWLINE# self . set_edit_pos ( p ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( key == 'delete' ) : #NEWLINE# #INDENT# self . pref_col_maxcol = ( None , None ) #NEWLINE# if ( not self . _delete_highlighted ( ) ) : #NEWLINE# #INDENT# if ( p >= len ( self . edit_text ) ) : #NEWLINE# #INDENT# return key #NEWLINE# #UNINDENT# p = move_next_char ( self . edit_text , p , len ( self . edit_text ) ) #NEWLINE# self . set_edit_text ( ( self . edit_text [ : self . edit_pos ] + self . edit_text [ p : ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . _command_map [ key ] in ( CURSOR_MAX_LEFT , CURSOR_MAX_RIGHT ) ) : #NEWLINE# #INDENT# self . highlight = None #NEWLINE# self . pref_col_maxcol = ( None , None ) #NEWLINE# ( x , y ) = self . get_cursor_coords ( ( maxcol , ) ) #NEWLINE# if ( self . _command_map [ key ] == CURSOR_MAX_LEFT ) : #NEWLINE# #INDENT# self . move_cursor_to_coords ( ( maxcol , ) , LEFT , y ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . move_cursor_to_coords ( ( maxcol , ) , RIGHT , y ) #NEWLINE# #UNINDENT# return #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return key