handleKeydown method

  1. @HostListener('keydown', [r'$event'])
void handleKeydown(
  1. KeyboardEvent event, {
  2. bool shouldPreventDefault = true,
})

Implementation

@HostListener('keydown', [r'$event'])
void handleKeydown(KeyboardEvent event, {bool shouldPreventDefault = true}) {
  _isMouseDriven = false;

  if (event.keyCode == KeyCode.TAB) return;

  var keyCode = event.keyCode;
  var activeMenuItem = activeModel?.activeItem as MenuItem?;

  switch (keyCode) {
    case KeyCode.UP:
      _activeHoveredItemIfNone();
      activeModel?.activatePrevious();
      _focusActiveItem();
      break;
    case KeyCode.DOWN:
      _activeHoveredItemIfNone();
      activeModel?.activateNext();
      _focusActiveItem();
      break;
    case KeyCode.RIGHT:
      if (activeMenuItem?.hasSubMenu == true) {
        _openSubMenu(activeModel?.activeItem as MenuItem,
            isOpenedByKeyboard: true);
      }
      break;
    case KeyCode.LEFT:
      if (_closeOnPressLeft) {
        _dropdownHandle?.close();
      }
      break;
    default:
      // Try triggering any affix shortcut actions. If successful, then
      // prevent browser default behaviour for this key down action.
      if (!_triggerAffixShortcutActions(activeMenuItem, keyCode)) {
        shouldPreventDefault = false;
      }
      break;
  }

  if (shouldPreventDefault) event.preventDefault();
}