/// <summary> /// Handles the events on the element. /// </summary> /// <param name="e">The keyboard event sent from the /// browser.</param> public void handleEvent(BrowserEvent e) { var be = (Bridge.Html5.KeyboardEvent)e.getBrowserEvent(); KeyCodes?keyCode; int charCode; var altKey = be.AltKey; // IE reports the character code in the keyCode field for keypress events. // There are two exceptions however, Enter and Escape. if (goog.userAgent.IE && e.type == goog.events.EventType.KEYPRESS) { keyCode = this.keyCode_; charCode = keyCode != goog.events.KeyCodes.ENTER && keyCode != goog.events.KeyCodes.ESC ? be.KeyCode : 0; // Safari reports the character code in the keyCode field for keypress // events but also has a charCode field. } else if ( (goog.userAgent.WEBKIT || goog.userAgent.EDGE) && e.type == goog.events.EventType.KEYPRESS) { keyCode = this.keyCode_; charCode = be.CharCode >= 0 && be.CharCode < 63232 && goog.events.KeyCodes_isCharacterKey(keyCode.Value) ? be.CharCode : 0; // Opera reports the keycode or the character code in the keyCode field. } else if (goog.userAgent.OPERA && !goog.userAgent.WEBKIT) { keyCode = this.keyCode_; charCode = goog.events.KeyCodes_isCharacterKey(keyCode.Value) ? be.KeyCode : 0; // Mozilla reports the character code in the charCode field. } else { keyCode = be.KeyCode != 0 ? (KeyCodes)be.KeyCode : this.keyCode_; charCode = be.CharCode != 0 ? be.CharCode : 0; if (goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_) { altKey = this.altKey_; } // On the Mac, shift-/ triggers a question mark char code and no key code // (normalized to WIN_KEY), so we synthesize the latter. if (goog.userAgent.MAC && charCode == (int)goog.events.KeyCodes.QUESTION_MARK && keyCode == goog.events.KeyCodes.WIN_KEY) { keyCode = goog.events.KeyCodes.SLASH; } } keyCode = goog.events.KeyCodes_normalizeKeyCode(keyCode.Value); var key = keyCode.Value; var keyIdentifier = be.KeyIdentifier; // Correct the key value for certain browser-specific quirks. if (keyCode.HasValue) { if ((int)keyCode >= 63232 && goog.events.KeyHandler.safariKey_.ContainsKey(keyCode.Value.ToString())) { // NOTE(nicksantos): Safari 3 has fixed this problem, // this is only needed for Safari 2. key = goog.events.KeyHandler.safariKey_[keyCode.Value.ToString()]; } else { // Safari returns 25 for Shift+Tab instead of 9. if ((int)keyCode == 25 && e.shiftKey) { key = (KeyCodes)9; } } } else if ( keyIdentifier != null && goog.events.KeyHandler.keyIdentifier_.ContainsKey(keyIdentifier)) { // This is needed for Safari Windows because it currently doesn't give a // keyCode/which for non printable keys. key = goog.events.KeyHandler.keyIdentifier_[keyIdentifier]; } // If we get the same keycode as a keydown/keypress without having seen a // keyup event, then this event was caused by key repeat. var repeat = key == this.lastKey_; this.lastKey_ = key; var ev = new goog.events.KeyEvent((int)key, charCode, repeat, be); ev.altKey = altKey; this.dispatchEvent(ev); }
/// <summary> /// Handles keyboard events dispatched while the palette has focus. Moves the /// highlight on arrow keys, and selects the highlighted item on Enter or Space. /// Returns true if the event was handled, false otherwise. In particular, if /// the user attempts to navigate out of the grid, the highlight isn't changed, /// and this method returns false; it is then up to the parent component to /// handle the event (e.g. by wrapping the highlight around). Overrides {@link /// goog.ui.Control#handleKeyEvent}. /// </summary> /// <param name="e">Key event to handle.</param> /// <returns>True iff the key event was handled by the component.</returns> public override bool handleKeyEvent(goog.events.KeyEvent e) { var items = this.getContent().As <JsArray <Node> >(); var numItems = items != null ? items.Length : 0; var numColumns = (int)this.size_.width; // If the component is disabled or the palette is empty, bail. if (numItems == 0 || !this.isEnabled()) { return(false); } // User hit ENTER or SPACE; trigger action. if (e.keyCode == (int)goog.events.KeyCodes.ENTER || e.keyCode == (int)goog.events.KeyCodes.SPACE) { return(this.performActionInternal(e)); } // User hit HOME or END; move highlight. if (e.keyCode == (int)goog.events.KeyCodes.HOME) { this.setHighlightedIndex(0); return(true); } else if (e.keyCode == (int)goog.events.KeyCodes.END) { this.setHighlightedIndex(numItems - 1); return(true); } // If nothing is highlighted, start from the selected index. If nothing is // selected either, highlightedIndex is -1. var highlightedIndex = this.highlightedIndex_ < 0 ? this.getSelectedIndex() : this.highlightedIndex_; switch ((goog.events.KeyCodes)e.keyCode) { case goog.events.KeyCodes.LEFT: // If the highlighted index is uninitialized, or is at the beginning, move // it to the end. if (highlightedIndex == -1 || highlightedIndex == 0) { highlightedIndex = numItems; } this.setHighlightedIndex(highlightedIndex - 1); e.preventDefault(); return(true); case goog.events.KeyCodes.RIGHT: // If the highlighted index at the end, move it to the beginning. if (highlightedIndex == numItems - 1) { highlightedIndex = -1; } this.setHighlightedIndex(highlightedIndex + 1); e.preventDefault(); return(true); case goog.events.KeyCodes.UP: if (highlightedIndex == -1) { highlightedIndex = numItems + numColumns - 1; } if (highlightedIndex >= numColumns) { this.setHighlightedIndex(highlightedIndex - numColumns); e.preventDefault(); return(true); } break; case goog.events.KeyCodes.DOWN: if (highlightedIndex == -1) { highlightedIndex = -numColumns; } if (highlightedIndex < numItems - numColumns) { this.setHighlightedIndex(highlightedIndex + numColumns); e.preventDefault(); return(true); } break; } return(false); }