Java Code Examples for com.google.gwt.user.client.Event#getKeyCode()

The following examples show how to use com.google.gwt.user.client.Event#getKeyCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AriaLink.java    From core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {

    int type = DOM.eventGetType(event);
    switch (type) {
        case Event.ONKEYDOWN:
            if(event.getKeyCode()== KeyCodes.KEY_ENTER)
            {
                if(handler!=null)
                    handler.onClick(null);
                event.stopPropagation();
            }
            break;
        default:
            return;

    }

}
 
Example 2
Source File: CubaNotification.java    From cuba with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onEventPreview(Event event) {
    int type = DOM.eventGetType(event);

    if ((type == Event.ONCLICK || type == Event.ONTOUCHEND)
            && event.getEventTarget() != null) {
        Element target = Element.as(event.getEventTarget());
        if (containsSubstring(target.getClassName(), CUBA_NOTIFICATION_MODALITY_CURTAIN)) {
            hide();
            return false;
        }
    }

    if (type == Event.ONKEYDOWN && event.getKeyCode() == KeyCodes.KEY_ESCAPE) {
        if (!getElement().getClassName().contains(TRAY_STYLE)) {
            hide();
            return false;
        }
    }

    return super.onEventPreview(event);
}
 
Example 3
Source File: ImageButton.java    From unitime with Apache License 2.0 6 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
	if (!isEnabled()) return;

	super.onBrowserEvent(event);

    if ((event.getTypeInt() & Event.KEYEVENTS) != 0) {
    	int type = DOM.eventGetType(event);
    	char keyCode = (char) event.getKeyCode();
    	switch (type) {
    	case Event.ONKEYDOWN:
    		if (keyCode == ' ' || keyCode == '\n' || keyCode == '\r') {
    			if (iDown != null) setResource(iDown);
    		}
    		break;
        case Event.ONKEYUP:
        	if (keyCode == ' ' || keyCode == '\n' || keyCode == '\r') {
        		setResource(iUp);
        		onClick();
        	}
        	break;
    	}
    }
}
 
Example 4
Source File: CellBrowser.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void onBrowserEvent2(Event event) {
  super.onBrowserEvent2(event);

  // Handle keyboard navigation between lists.
  String eventType = event.getType();
  if (BrowserEvents.KEYDOWN.equals(eventType) && !isKeyboardNavigationSuppressed()) {
    int keyCode = event.getKeyCode();
    boolean isRtl = LocaleInfo.getCurrentLocale().isRTL();
    keyCode = KeyCodes.maybeSwapArrowKeysForRtl(keyCode, isRtl);
    switch (keyCode) {
      case KeyCodes.KEY_LEFT:
        keyboardNavigateShallow();
        return;
      case KeyCodes.KEY_RIGHT:
        keyboardNavigateDeep();
        return;
    }
  }
}
 
Example 5
Source File: InputMultiSelect.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
	super.onBrowserEvent(event);
	boolean mustKillEvent = false;
	if (DOM.eventGetType(event) == Event.ONKEYDOWN) {
		switch (event.getKeyCode()) {
			case KeyCodes.KEY_ENTER:
				if (this.getDropdown().isOpen()) {
					((MultiSelectionHandler) this.getSelectionHandler()).onEnterKeyDown();
					mustKillEvent = true;
				}
				break;
			case KeyCodes.KEY_TAB:
			case KeyCodes.KEY_ESCAPE:
				this.getDropdown().close();
				break;
			default:
				break;
		}
	}
	if (mustKillEvent) {
		event.preventDefault();
		event.stopPropagation();
	}
}
 
Example 6
Source File: InputSelect.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
	super.onBrowserEvent(event);
	boolean mustKillEvent = false;
	if (DOM.eventGetType(event) == Event.ONKEYDOWN) {
		switch (event.getKeyCode()) {
			case KeyCodes.KEY_TAB:
			case KeyCodes.KEY_ESCAPE:
				this.getDropdown().close();
				break;
			default:
				break;
		}
	}
	if (mustKillEvent) {
		event.preventDefault();
		event.stopPropagation();
	}
}
 
Example 7
Source File: CubaSourceCodeEditorWidget.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
    int type = DOM.eventGetType(event);
    if (type == Event.ONKEYDOWN
            && event.getKeyCode() == KeyCodes.KEY_ENTER
            && !event.getAltKey()
            && !event.getShiftKey()
            && !event.getCtrlKey()) {
        event.stopPropagation();
        return;
    }

    super.onBrowserEvent(event);
}
 
Example 8
Source File: ClickableHint.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
	super.onBrowserEvent(event);

    if ((event.getTypeInt() & Event.KEYEVENTS) != 0) {
    	int type = DOM.eventGetType(event);
    	char keyCode = (char) event.getKeyCode();
    	switch (type) {
        case Event.ONKEYUP:
        	if (keyCode == ' ' || keyCode == '\n' || keyCode == '\r')
        		onClick();
        	break;
    	}
    }
}
 
Example 9
Source File: AriaToggleButton.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
	if (event.getTypeInt() == Event.ONKEYUP && event.getKeyCode() == KeyCodes.KEY_SPACE) {
		onClick();
	}
	if (event.getTypeInt() == Event.ONCLICK) {
		setValue(!getValue());
	}
	super.onBrowserEvent(event);
}
 
Example 10
Source File: Tab.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
    int type = DOM.eventGetType(event);
    switch (type) {
        case Event.ONKEYDOWN:
            if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
                tabLayout.selectTab(index);
                event.stopPropagation();
            }
            break;
        default:
            break;
    }
}
 
Example 11
Source File: EventWrapper.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Wrapper for GWT's get[Key Char]Code() that conflates the two values. If
 * there is no keyCode present, it returns charCode instead. This matches the
 * values in {{@link #keyMap} above.
 */
public static int getKeyCode(Event evt) {
  int keyCode = evt.getKeyCode();
  if (keyCode == 0) {
    keyCode = evt.getCharCode();
  }
  return keyCode;
}
 
Example 12
Source File: AbstractInputSelect.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onBrowserEvent(Event event) {
	super.onBrowserEvent(event);
	boolean mustKillEvent = false;
	switch (DOM.eventGetType(event)) {
		case Event.ONKEYDOWN:
			switch (event.getKeyCode()) {
				case KeyCodes.KEY_HOME:
					this.selectionHandler.onHomeKeyDown();
					mustKillEvent = true;
					break;
				case KeyCodes.KEY_END:
					this.selectionHandler.onEndKeyDown();
					mustKillEvent = true;
					break;
				case KeyCodes.KEY_UP:
					this.selectionHandler.onUpKeyDown();
					mustKillEvent = true;
					break;
				case KeyCodes.KEY_DOWN:
					this.selectionHandler.onDownKeyDown();
					mustKillEvent = true;
					break;
				default:
					break;
			}
			break;
		case Event.ONKEYPRESS:
			this.keyPressHandler.handleKeyPress(event.getCharCode());
			break;
		default:
			break;
	}

	if (mustKillEvent) {
		event.preventDefault();
		event.stopPropagation();
	}
}
 
Example 13
Source File: EventWrapper.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Wrapper for GWT's get[Key Char]Code() that conflates the two values. If
 * there is no keyCode present, it returns charCode instead. This matches the
 * values in {{@link #keyMap} above.
 */
public static int getKeyCode(Event evt) {
  int keyCode = evt.getKeyCode();
  if (keyCode == 0) {
    keyCode = evt.getCharCode();
  }
  return keyCode;
}
 
Example 14
Source File: CubaButtonWidget.java    From cuba with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean handleKeyboardEvents(Event event) {
    int type = DOM.eventGetType(event);
    // Synthesize clicks based on keyboard events AFTER the normal key
    // handling.
    if ((event.getTypeInt() & Event.KEYEVENTS) != 0) {
        switch (type) {
            case Event.ONKEYDOWN:
                // Button should not react on shortcuts with ENTER and SPACE
                if (isShortcut(event)
                        && (event.getKeyCode() == KeyCodes.KEY_ENTER
                        || event.getKeyCode() == KeyCodes.KEY_SPACE)) {
                    return true;
                }
                // Stop propagation when the user starts pressing a button that
                // we are handling to prevent actions from getting triggered
                if (event.getKeyCode() == KeyCodes.KEY_SPACE) {
                    isFocusing = true;
                    event.preventDefault();
                    event.stopPropagation();
                    return true;
                } else if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
                    isFocusing = true;
                    event.preventDefault();
                    event.stopPropagation();
                    return true;
                }
                break;

            // CAUTION IE sometimes does not generate ONKEYPRESS for ENTER, so we override default Vaadin behavior
            case Event.ONKEYUP:
                if (isFocusing) {
                    if (event.getKeyCode() == KeyCodes.KEY_SPACE) {
                        isFocusing = false;
                        onClick();
                        event.stopPropagation();
                        event.preventDefault();
                        return true;
                    } else if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
                        isFocusing = false;
                        onClick();
                        event.stopPropagation();
                        event.preventDefault();
                        return true;
                    }
                } else if (isShortcut(event)
                        && (event.getKeyCode() == KeyCodes.KEY_ENTER
                        || event.getKeyCode() == KeyCodes.KEY_SPACE)) {
                    return true;
                }
                break;
            case Event.ONKEYPRESS: {
                // Button should not react on shortcuts with ENTER and SPACE
                if (isShortcut(event)
                        && (event.getKeyCode() == KeyCodes.KEY_ENTER
                        || event.getKeyCode() == KeyCodes.KEY_SPACE)) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 15
Source File: EventPreviewAutoHiderRegistrar.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
@Override
public void onPreviewNativeEvent(NativePreviewEvent previewEvent) {
  if (autoHiders.isEmpty()) {
    return;
  }

  // TODO(danilatos,user,user): Push signal down a layer - clean this up.
  Event event = Event.as(previewEvent.getNativeEvent());
  int lowLevelType = event.getTypeInt();

  // TODO(danilatos): Insert this logic deeply rather than
  // sprinkling it in event handlers. Also the return value
  // of onEventPreview is the reverse of signal handlers.
  SignalEvent signal = SignalEventImpl.create(event, false);
  if (signal == null) {
    return;
  }

  // Key events (excluding escape) and mousewheel events use hideTopmostAutoHiderForKeyEvent
  if (lowLevelType == Event.ONMOUSEWHEEL || signal.isKeyEvent()) {
    if (hideTopmostAutoHiderForKeyEvent(false)) {
      // TODO(user): We don't call previewEvent.cancel() here, since for the floating-buttons
      // menu we want, for example, space-bar to still shift focus to the next blip.
      // The to-do is to audit the previewEvent.cancel call below and see why it's there (and if
      // it's not needed, eliminate it).
      return;
    }
  }

  // Pressing escape at any time causes us to close and discard the event.
  if (signal.getKeySignalType() == KeySignalType.NOEFFECT &&
      event.getKeyCode() == KeyCodes.KEY_ESCAPE) {
    if (hideTopmostAutoHiderForKeyEvent(true)) {
      previewEvent.cancel();
      return;
    }
  }

  // Click events and mouse-wheel events that fall through use hideAllAfter.
  if (lowLevelType == Event.ONMOUSEDOWN || lowLevelType == Event.ONMOUSEWHEEL) {
    hideAllAfter(signal.getTarget());
  }

  // Otherwise we don't do anything and the event continues as usual.
}
 
Example 16
Source File: EventPreviewAutoHiderRegistrar.java    From swellrt with Apache License 2.0 4 votes vote down vote up
@Override
public void onPreviewNativeEvent(NativePreviewEvent previewEvent) {
  if (autoHiders.isEmpty()) {
    return;
  }

  // TODO(danilatos,user,user): Push signal down a layer - clean this up.
  Event event = Event.as(previewEvent.getNativeEvent());
  int lowLevelType = event.getTypeInt();

  // TODO(danilatos): Insert this logic deeply rather than
  // sprinkling it in event handlers. Also the return value
  // of onEventPreview is the reverse of signal handlers.
  SignalEvent signal = SignalEventImpl.create(event, false);
  if (signal == null) {
    return;
  }

  // Key events (excluding escape) and mousewheel events use hideTopmostAutoHiderForKeyEvent
  if (lowLevelType == Event.ONMOUSEWHEEL || signal.isKeyEvent()) {
    if (hideTopmostAutoHiderForKeyEvent(false)) {
      // TODO(user): We don't call previewEvent.cancel() here, since for the floating-buttons
      // menu we want, for example, space-bar to still shift focus to the next blip.
      // The to-do is to audit the previewEvent.cancel call below and see why it's there (and if
      // it's not needed, eliminate it).
      return;
    }
  }

  // Pressing escape at any time causes us to close and discard the event.
  if (signal.getKeySignalType() == KeySignalType.NOEFFECT &&
      event.getKeyCode() == KeyCodes.KEY_ESCAPE) {
    if (hideTopmostAutoHiderForKeyEvent(true)) {
      previewEvent.cancel();
      return;
    }
  }

  // Click events and mouse-wheel events that fall through use hideAllAfter.
  if (lowLevelType == Event.ONMOUSEDOWN || lowLevelType == Event.ONMOUSEWHEEL) {
    hideAllAfter(signal.getTarget());
  }

  // Otherwise we don't do anything and the event continues as usual.
}
 
Example 17
Source File: CubaPopupButtonWidget.java    From cuba with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean handleKeyboardEvents(Event event) {
    int type = DOM.eventGetType(event);
    // Synthesize clicks based on keyboard events AFTER the normal key
    // handling.
    if ((event.getTypeInt() & Event.KEYEVENTS) != 0) {
        switch (type) {
            case Event.ONKEYDOWN:
                // Button should not react on shortcuts with ENTER and SPACE
                if (isShortcut(event)
                        && (event.getKeyCode() == KeyCodes.KEY_ENTER
                        || event.getKeyCode() == KeyCodes.KEY_SPACE)) {
                    return true;
                }
                // Stop propagation when the user starts pressing a button that
                // we are handling to prevent actions from getting triggered
                if (event.getKeyCode() == KeyCodes.KEY_SPACE) {
                    isFocusing = true;
                    event.preventDefault();
                    event.stopPropagation();
                    return true;
                } else if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
                    isFocusing = true;
                    event.stopPropagation();
                    return true;
                } else if (event.getKeyCode() == KeyCodes.KEY_DOWN) {
                    isFocusing = true;
                    event.stopPropagation();
                }
                break;

            // CAUTION IE sometimes does not generate ONKEYPRESS for ENTER, so we override default Vaadin behavior
            case Event.ONKEYUP:
                if (isFocusing) {
                    if (event.getKeyCode() == KeyCodes.KEY_SPACE) {
                        isFocusing = false;
                        onClick();
                        event.stopPropagation();
                        event.preventDefault();
                        return true;
                    } else if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
                        isFocusing = false;
                        onClick();
                        event.stopPropagation();
                        event.preventDefault();
                        return true;
                    } else if (event.getKeyCode() == KeyCodes.KEY_DOWN) {
                        isFocusing = false;
                        onClick();
                        event.stopPropagation();
                        event.preventDefault();
                        return true;
                    }
                } else if (isShortcut(event)
                        && (event.getKeyCode() == KeyCodes.KEY_ENTER
                        || event.getKeyCode() == KeyCodes.KEY_SPACE)) {
                    return true;
                }
                break;
            case Event.ONKEYPRESS: {
                // Button should not react on shortcuts with ENTER and SPACE
                if (isShortcut(event)
                        && (event.getKeyCode() == KeyCodes.KEY_ENTER
                        || event.getKeyCode() == KeyCodes.KEY_SPACE)) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 18
Source File: EventWrapper.java    From incubator-retired-wave with Apache License 2.0 2 votes vote down vote up
/**
 * @return String describing the event's key code, e.g.,
 *    " 64 'a'"
 */
private static String key(Event event) {
  return " " + event.getKeyCode() + " '" + (char) event.getKeyCode() + "'";
}
 
Example 19
Source File: EventWrapper.java    From swellrt with Apache License 2.0 2 votes vote down vote up
/**
 * @return String describing the event's key code, e.g.,
 *    " 64 'a'"
 */
private static String key(Event event) {
  return " " + event.getKeyCode() + " '" + (char) event.getKeyCode() + "'";
}