MCPcopy Create free account
hub / github.com/TanStack/hotkeys / dispatchKeyFromElement

Function dispatchKeyFromElement

packages/hotkeys/tests/sequence-manager.test.ts:26–51  ·  view source on GitHub ↗

* Helper to dispatch a keyboard event from a specific element. * Focuses the element first to set document.activeElement, then dispatches * on the element so listeners attached to it receive the event.

(
  element: HTMLElement,
  key: string,
  options: {
    eventType?: 'keydown' | 'keyup'
    ctrlKey?: boolean
    shiftKey?: boolean
    altKey?: boolean
    metaKey?: boolean
  } = {},
)

Source from the content-addressed store, hash-verified

24 * on the element so listeners attached to it receive the event.
25 */
26function dispatchKeyFromElement(
27 element: HTMLElement,
28 key: string,
29 options: {
30 eventType?: 'keydown' | 'keyup'
31 ctrlKey?: boolean
32 shiftKey?: boolean
33 altKey?: boolean
34 metaKey?: boolean
35 } = {},
36): KeyboardEvent {
37 // Focus the element so document.activeElement is set
38 element.focus()
39
40 const eventType = options.eventType ?? 'keydown'
41 const event = new KeyboardEvent(eventType, {
42 key,
43 ctrlKey: options.ctrlKey ?? false,
44 shiftKey: options.shiftKey ?? false,
45 altKey: options.altKey ?? false,
46 metaKey: options.metaKey ?? false,
47 bubbles: true,
48 })
49 element.dispatchEvent(event)
50 return event
51}
52
53describe('SequenceManager', () => {
54 beforeEach(() => {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…