mirror of
https://github.com/vale981/tridactyl
synced 2025-03-10 12:46:38 -04:00

All the mistakes are Colin's fault - Generic listener - Send messages to command line - ^ doesn't work unless frame is opened in new tab
30 lines
776 B
TypeScript
30 lines
776 B
TypeScript
// Interface: onKeydown.addListener(func)
|
|
//
|
|
import * as Messaging from './messaging'
|
|
|
|
import {MsgSafeKeyboardEvent} from './msgsafe'
|
|
|
|
// Type for messages sent from keydown_content
|
|
export interface KeydownShimMessage extends Message {
|
|
type: "keydown"
|
|
event: MsgSafeKeyboardEvent
|
|
}
|
|
|
|
type KeydownCallback = (keyevent: MsgSafeKeyboardEvent) => void
|
|
|
|
const listeners = new Set<KeydownCallback>()
|
|
function addListener(cb: KeydownCallback) {
|
|
listeners.add(cb)
|
|
return () => { listeners.delete(cb) }
|
|
}
|
|
|
|
export const onKeydown = { addListener }
|
|
|
|
// Receive events from content and pass to listeners
|
|
function handler(message: KeydownShimMessage) {
|
|
for (let listener of listeners) {
|
|
listener(message.event)
|
|
}
|
|
}
|
|
|
|
Messaging.addListener('keydown', handler)
|