MCPcopy Create free account
hub / github.com/adobe/react-spectrum / onKeyDown

Function onKeyDown

packages/react-aria/src/interactions/usePress.ts:389–448  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

387 let state = ref.current;
388 let pressProps: DOMAttributes = {
389 onKeyDown(e) {
390 if (
391 isValidKeyboardEvent(e.nativeEvent, e.currentTarget as Element) &&
392 nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)
393 ) {
394 if (shouldPreventDefaultKeyboard(getEventTarget(e) as Element, e.key)) {
395 e.preventDefault();
396 }
397
398 // If the event is repeating, it may have started on a different element
399 // after which focus moved to the current element. Ignore these events and
400 // only handle the first key down event.
401 let shouldStopPropagation = true;
402 if (!state.isPressed && !e.repeat) {
403 state.target = e.currentTarget;
404 state.isPressed = true;
405 state.pointerType = 'keyboard';
406 shouldStopPropagation = triggerPressStart(e, 'keyboard');
407 }
408
409 // Focus may move before the key up event, so register the event on the document
410 // instead of the same element where the key down event occurred. Make it capturing so that it will trigger
411 // before stopPropagation from useKeyboard on a child element may happen and thus we can still call triggerPress for the parent element.
412 let originalTarget = e.currentTarget;
413 let pressUp = e => {
414 if (
415 isValidKeyboardEvent(e, originalTarget) &&
416 !e.repeat &&
417 nodeContains(originalTarget, getEventTarget(e) as Element) &&
418 state.target
419 ) {
420 triggerPressUpEvent(createEvent(state.target, e), 'keyboard');
421 }
422 };
423
424 addGlobalListener(
425 getOwnerDocument(e.currentTarget),
426 'keyup',
427 chain(pressUp, onKeyUp),
428 true
429 );
430
431 if (shouldStopPropagation) {
432 e.stopPropagation();
433 }
434
435 // Keep track of the keydown events that occur while the Meta (e.g. Command) key is held.
436 // macOS has a bug where keyup events are not fired while the Meta key is down.
437 // When the Meta key itself is released we will get an event for that, and we'll act as if
438 // all of these other keys were released as well.
439 // https://bugs.chromium.org/p/chromium/issues/detail?id=1393524
440 // https://bugs.webkit.org/show_bug.cgi?id=55291
441 // https://bugzilla.mozilla.org/show_bug.cgi?id=1299553
442 if (e.metaKey && isMac()) {
443 state.metaKeyEvents?.set(e.key, e.nativeEvent);
444 }
445 } else if (e.key === 'Meta') {
446 state.metaKeyEvents = new Map();

Callers

nothing calls this directly

Calls 7

nodeContainsFunction · 0.90
getEventTargetFunction · 0.90
getOwnerDocumentFunction · 0.90
chainFunction · 0.90
isValidKeyboardEventFunction · 0.85
setMethod · 0.45

Tested by

no test coverage detected