From 6484171158df453c6a89075b28a11f8f5f435beb Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 30 Oct 2013 14:24:23 +0000 Subject: convert keyboard input to new prim format --- diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py index 9666573..43ae9bc 100644 --- a/TurtleArt/taconstants.py +++ b/TurtleArt/taconstants.py @@ -79,6 +79,69 @@ XO4 = 'xo4' UNKNOWN = 'unknown' TMP_SVG_PATH = '/tmp/turtle_output.svg' +KEY_DICT = { + 'Left': 1, + 'KP_Left': 1, + 'Up': 2, + 'KP_Up': 2, + 'Right': 3, + 'KP_Right': 3, + 'Down': 4, + 'KP_Down': 4, + 'BackSpace': 8, + 'Tab': 9, + 'Return': 13, + 'Escape': 27, + 'space': 32, + ' ': 32, + 'exclam': 33, + 'quotedbl': 34, + 'numbersign': 35, + 'dollar': 36, + 'percent': 37, + 'ampersand': 38, + 'apostrophe': 39, + 'parenleft': 40, + 'parenright': 41, + 'asterisk': 42, + 'plus': 43, + 'comma': 44, + 'minus': 45, + 'period': 46, + 'slash': 47, + 'colon': 58, + 'semicolon': 59, + 'less': 60, + 'equal': 61, + 'greater': 62, + 'question': 63, + 'at': 64, + 'underscore': 95, + 'bracketleft': 91, + 'backslash': 92, + 'bracketright': 93, + 'asciicircum': 94, + 'grave': 96, + 'braceleft': 123, + 'bar': 124, + 'braceright': 125, + 'asciitilde': 126, + 'Delete': 127, + } +REVERSE_KEY_DICT = { + 1: _('left'), + 2: _('up'), + 3: _('right'), + 4: _('down'), + 8: _('backspace'), + 9: _('tab'), + # TRANS: enter is the name of the enter (or return) key + 13: _('enter'), + 27: 'esc', + # TRANS: space is the name of the space key + 32: _('space'), + 127: _('delete') + } class Color(object): diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index b1db4ff..8a64f7b 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -164,7 +164,6 @@ class LogoCode: self.hidden_turtle = None - self.keyboard = 0 self.trace = 0 self.update_values = False self.gplay = None diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index 2513391..d6baa78 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -58,7 +58,8 @@ from taconstants import (HORIZONTAL_PALETTE, VERTICAL_PALETTE, BLOCK_SCALE, PYTHON_SKIN, PALETTE_HEIGHT, STATUS_LAYER, OLD_DOCK, EXPANDABLE_ARGS, XO1, XO15, XO175, XO30, XO4, TITLEXY, CONTENT_ARGS, CONSTANTS, EXPAND_SKIN, PROTO_LAYER, - EXPANDABLE_FLOW, SUFFIX, TMP_SVG_PATH, Color) + EXPANDABLE_FLOW, SUFFIX, TMP_SVG_PATH, Color, + KEY_DICT) from tapalette import (palette_names, palette_blocks, expandable_blocks, block_names, content_blocks, default_values, special_names, block_styles, help_strings, @@ -170,6 +171,7 @@ class TurtleArtWindow(): self._autohide_shape = True self.keypress = '' self.keyvalue = 0 + self.keyboard = 0 self._focus_out_id = None self._insert_text_id = None self._text_to_check = False @@ -3615,6 +3617,19 @@ before making changes to your program')) return True + def get_keyboard_input(self): + """ Query keyboard and update cached keyboard input """ + if len(self.keypress) == 1: + self.keyboard = ord(self.keypress[0]) + elif self.keypress in KEY_DICT: + self.keyboard = KEY_DICT[self.keypress] + else: + self.keyboard = 0 + + def get_keyboard(self): + """ Return cached keyboard input """ + return self.keyboard + def _process_keyboard_commands(self, keyname, block_flag=True): ''' Use the keyboard to move blocks and turtle ''' mov_dict = {'KP_Up': [0, 20], 'j': [0, 20], 'Up': [0, 20], diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py index 5f1a8fa..087d550 100644 --- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py +++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py @@ -33,7 +33,7 @@ from TurtleArt.talogo import (primitive_dictionary, logoerror, from TurtleArt.taconstants import (DEFAULT_SCALE, CONSTANTS, MEDIA_SHAPES, SKIN_PATHS, BLOCKS_WITH_SKIN, PYTHON_SKIN, MEDIA_BLOCK2TYPE, VOICES, - MACROS, Color) + MACROS, Color, KEY_DICT, REVERSE_KEY_DICT) from TurtleArt.tautils import (round_int, debug_output, get_path, data_to_string, find_group, image_to_base64, hat_on_top, listify, data_from_file) @@ -56,7 +56,6 @@ class Turtle_blocks_extras(Plugin): SKIN_PATHS.append('plugins/turtle_blocks_extras/images') self.heap = self.tw.lc.heap - self.keyboard = self.tw.lc.keyboard self.title_height = int((self.tw.canvas.height / 20) * self.tw.scale) # set up Turtle Block palettes @@ -376,7 +375,6 @@ pressed')) return_type=TYPE_NUMBER, call_afterwards=self.after_mouse_y)) - primitive_dictionary['kbinput'] = self._prim_kbinput palette.add_block('kbinput', style='basic-style-extended-vertical', label=_('query keyboard'), @@ -384,9 +382,9 @@ pressed')) help_string=_('query for keyboard input (results \ stored in keyboard block)')) self.tw.lc.def_prim('kbinput', 0, - lambda self: primitive_dictionary['kbinput']()) + Primitive(self.tw.get_keyboard_input, + call_afterwards=self.after_keypress)) - primitive_dictionary['keyboard'] = self._prim_keyboard palette.add_block('keyboard', style='box-style', label=_('keyboard'), @@ -396,7 +394,8 @@ stored in keyboard block)')) help_string=_('holds results of query-keyboard \ block as ASCII')) self.tw.lc.def_prim('keyboard', 0, - lambda self: primitive_dictionary['keyboard']()) + Primitive(self.tw.get_keyboard, + return_type=TYPE_NUMBER)) primitive_dictionary['readpixel'] = self._prim_readpixel palette.add_block('readpixel', @@ -1109,93 +1108,19 @@ Journal objects')) # Block primitives - def _prim_keyboard(self): - """ Return last character typed """ - return self.tw.lc.keyboard - - def _prim_kbinput(self): - """ Query keyboard """ - DICT = { - 'Left': 1, - 'KP_Left': 1, - 'Up': 2, - 'KP_Up': 2, - 'Right': 3, - 'KP_Right': 3, - 'Down': 4, - 'KP_Down': 4, - 'BackSpace': 8, - 'Tab': 9, - 'Return': 13, - 'Escape': 27, - 'space': 32, - ' ': 32, - 'exclam': 33, - 'quotedbl': 34, - 'numbersign': 35, - 'dollar': 36, - 'percent': 37, - 'ampersand': 38, - 'apostrophe': 39, - 'parenleft': 40, - 'parenright': 41, - 'asterisk': 42, - 'plus': 43, - 'comma': 44, - 'minus': 45, - 'period': 46, - 'slash': 47, - 'colon': 58, - 'semicolon': 59, - 'less': 60, - 'equal': 61, - 'greater': 62, - 'question': 63, - 'at': 64, - 'underscore': 95, - 'bracketleft': 91, - 'backslash': 92, - 'bracketright': 93, - 'asciicircum': 94, - 'grave': 96, - 'braceleft': 123, - 'bar': 124, - 'braceright': 125, - 'asciitilde': 126, - 'Delete': 127, - } - REVERSE_DICT = { - 1: _('left'), - 2: _('up'), - 3: _('right'), - 4: _('down'), - 8: _('backspace'), - 9: _('tab'), - # TRANS: enter is the name of the enter (or return) key - 13: _('enter'), - 27: 'esc', - # TRANS: space is the name of the space key - 32: _('space'), - 127: _('delete') - } - - if len(self.tw.keypress) == 1: - self.tw.lc.keyboard = ord(self.tw.keypress[0]) - elif self.tw.keypress in DICT: - self.tw.lc.keyboard = DICT[self.tw.keypress] - else: - self.tw.lc.keyboard = 0 + def after_keypress(self): if self.tw.lc.update_values: - if self.tw.keypress in DICT: - if DICT[self.tw.keypress] in REVERSE_DICT: + if self.tw.keypress in KEY_DICT: + if KEY_DICT[self.tw.keypress] in REVERSE_KEY_DICT: self.tw.lc.update_label_value( - 'keyboard', REVERSE_DICT[DICT[self.tw.keypress]]) + 'keyboard', REVERSE_KEY_DICT[ + KEY_DICT[self.tw.keypress]]) else: - self.tw.lc.update_label_value('keyboard', - chr(DICT[self.tw.keypress])) - elif self.tw.lc.keyboard > 0: + self.tw.lc.update_label_value( + 'keyboard', chr(KEY_DICT[self.tw.keypress])) + elif self.tw.keyboard > 0: self.tw.lc.update_label_value('keyboard', - chr(self.tw.lc.keyboard)) + chr(self.tw.keyboard)) self.tw.keypress = '' def _prim_list(self, blklist): -- cgit v0.9.1