diff --git a/core/field_textinput.js b/core/field_textinput.js index 8d4c2e8c8..6aeec4295 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -132,7 +132,10 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { htmlInput.select(); } - // Bind to keyup -- trap Enter and Esc; resize after every keystroke. + // Bind to keydown -- trap Enter without IME and Esc to hide. + htmlInput.onKeyDownWrapper_ = + Blockly.bindEvent_(htmlInput, 'keydown', this, this.onHtmlInputKeyDown_); + // Bind to keyup -- trap Enter; resize after every keystroke. htmlInput.onKeyUpWrapper_ = Blockly.bindEvent_(htmlInput, 'keyup', this, this.onHtmlInputChange_); // Bind to keyPress -- repeatedly resize when holding down a key. @@ -144,6 +147,22 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { this.resizeEditor_); }; +/** + * Handle key down to the editor. + * @param {!Event} e Keyboard event. + * @private + */ +Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_ = function(e) { + var htmlInput = Blockly.FieldTextInput.htmlInput_; + var enterKey = 13, escKey = 27; + if (e.keyCode == enterKey) { + Blockly.WidgetDiv.hide(); + } else if (e.keyCode == escKey) { + this.setText(htmlInput.defaultValue); + Blockly.WidgetDiv.hide(); + } +}; + /** * Handle a change to the editor. * @param {!Event} e Keyboard event. @@ -151,14 +170,8 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { */ Blockly.FieldTextInput.prototype.onHtmlInputChange_ = function(e) { var htmlInput = Blockly.FieldTextInput.htmlInput_; - if (e.keyCode == 13) { - // Enter - Blockly.WidgetDiv.hide(); - } else if (e.keyCode == 27) { - // Esc - this.setText(htmlInput.defaultValue); - Blockly.WidgetDiv.hide(); - } else { + var escKey = 27; + if (e.keyCode != escKey) { // Update source block. var text = htmlInput.value; if (text !== htmlInput.oldValue_) { @@ -238,6 +251,7 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() { } thisField.setText(text); thisField.sourceBlock_.rendered && thisField.sourceBlock_.render(); + Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_); Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_); Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_); Blockly.unbindEvent_(htmlInput.onWorkspaceChangeWrapper_);