Class KeyConvert
java.lang.Object
com.vaadin.swingbridge.surrogates.util.KeyConvert
Translation between Vaadin's
Key / KeyModifier /
KeyLocation world and AWT's VK_* keycodes, modifier masks,
and key-location constants. Two directions with distinct purposes:
- Inbound (Vaadin → AWT) —
vaadinKeyToVK(Key),keyCharFrom(Key),keyModifiersToMask(Set),keyLocationToAwt(KeyLocation). Used byComponentMixin.addKeyListenerto bridge Vaadin'sKeyDown/Press/Upevents intoSKeyEvent. Matches againstevent.key(letters as"a"/"A", named keys as"Enter"/"ArrowLeft") — the shift-resolved identifierKeyboardEvent.getKey()surfaces. This direction is surrogate-only; the emulator has no inbound counterpart. - Outbound (AWT → Vaadin) —
vkToVaadinKey(int),toVaadinKeyBinding(KeyStroke),KeyConvert.VaadinKeyBinding. Used for accelerator/mnemonic install (registerKeyboardAction, menu accelerators, button mnemonics). Producesevent.code-shapedKeys ("KeyA"/"Digit5") because VaadinShortcutsmatch physical keys. Shared by both modules —:emulatorscalls straight through here for its ownregisterKeyboardAction— a single shared copy in the SD_shelper_statics-allowed direction (:emulators → :surrogates), not a per-module twin.
See SD_shelper_statics (module direction) and SD_key_events (the intentional event.code/
event.key split on the inbound side, which is not
deduplicated because the emulator has no inbound table).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordPair of VaadinKey+ modifier set resolved from aKeyStroke. -
Method Summary
Modifier and TypeMethodDescriptionstatic charkeyCharFrom(com.vaadin.flow.component.Key key) Return the effectivekeyCharfor a VaadinKey: the first character when the key's primary identifier is single-char (letters, digits, punctuation), orSKeyEvent.CHAR_UNDEFINEDfor named keys (Enter, Shift, F1, …).static intkeyLocationToAwt(com.vaadin.flow.component.KeyLocation location) VaadinKeyLocation→ AWTKEY_LOCATION_*.static intkeyModifiersToMask(Set<com.vaadin.flow.component.KeyModifier> modifiers) VaadinKeyModifierset → AWT*_DOWN_MASKbitmask.static KeyConvert.VaadinKeyBindingtoVaadinKeyBinding(KeyStroke stroke) TranslateKeyStroke→ VaadinShortcuts-shaped pair.static intvaadinKeyToVK(com.vaadin.flow.component.Key key) VaadinKey(as received onKeyboardEvent.getKey()) → AWTVK_*keycode.static com.vaadin.flow.component.KeyvkToVaadinKey(int vk)
-
Method Details
-
vaadinKeyToVK
public static int vaadinKeyToVK(com.vaadin.flow.component.Key key) VaadinKey(as received onKeyboardEvent.getKey()) → AWTVK_*keycode. ReturnsSKeyEvent.VK_UNDEFINEDwhen the incoming key doesn't map to any knownVK_*; per Swing's convention that's the right signal for "unknown key", not a failure.Matches against
key.getKeys().get(0)— the first DOM-level identifier carried by theKey. For incoming events Vaadin populates this from the browser'sevent.key, so letters come through as"a"/"A"(the shift state is reflected in the modifier mask, not the keycode — VK_A is the same either way). -
keyCharFrom
public static char keyCharFrom(com.vaadin.flow.component.Key key) Return the effectivekeyCharfor a VaadinKey: the first character when the key's primary identifier is single-char (letters, digits, punctuation), orSKeyEvent.CHAR_UNDEFINEDfor named keys (Enter, Shift, F1, …). Browserkeypressevents only fire for character-producing keys, so KEY_TYPED dispatches will always see a defined char here; KEY_PRESSED / KEY_RELEASED see CHAR_UNDEFINED for action keys per AWT's convention. -
keyModifiersToMask
VaadinKeyModifierset → AWT*_DOWN_MASKbitmask. Covers the five modifiers the browser exposes — Shift / Control / Alt / AltGraph / Meta. Any unknown modifier in the incoming set is ignored silently (Vaadin's enum is closed, so there's nothing to warn about). -
keyLocationToAwt
public static int keyLocationToAwt(com.vaadin.flow.component.KeyLocation location) VaadinKeyLocation→ AWTKEY_LOCATION_*. The two enums line up one-for-one on meaning but use different integer values — Vaadin's STANDARD=0 aligns with AWT's KEY_LOCATION_STANDARD=1, etc. Anullinput falls back toKEY_LOCATION_UNKNOWNso call sites constructing syntheticSKeyEvents without location info still get a sensible default. -
vkToVaadinKey
public static com.vaadin.flow.component.Key vkToVaadinKey(int vk) Inverse ofvaadinKeyToVK(Key): map an AWTVK_*code to a VaadinKey. Returnsnullwhen no stable mapping exists — callers treat null as "can't install this binding" rather than synthesising a bogus Key. Covers the letter / digit ranges (VK_A..VK_Z →"KeyA".."KeyZ", VK_0..VK_9 →"Digit0".."Digit9"— the codes browsers emit onevent.code) plus the named-key switch used for accelerator install (Enter, Escape, arrows, F-keys, …). Intended for outbound wiring (mnemonic install, registerKeyboardAction); the inverse directionvaadinKeyToVK(Key)matches againstevent.keyinstead because that's what Vaadin's inbound KeyNotifier surfaces. -
toVaadinKeyBinding
TranslateKeyStroke→ VaadinShortcuts-shaped pair. Returnsnullwhen the keystroke's keyCode isn't covered byvkToVaadinKey(int)— caller shouldonUnimplementedand skip browser-side install. Both modern (*_DOWN_MASK) and legacy (SHIFT_MASK, …) modifier bits accepted; AWT's KeyStroke factories produced either over the years and migrated code may mix them.
-