139 lines
4.3 KiB
TypeScript
139 lines
4.3 KiB
TypeScript
import {KeyEvent as NKeyEvent, RegisterCallback, UnregisterCallback} from "tc-native/ppt";
|
|
import {EventType, KeyEvent, KeyHook, SpecialKey} from "tc-shared/PPTListener";
|
|
import {tr} from "tc-shared/i18n/localize";
|
|
import {LogCategory} from "tc-shared/log";
|
|
import * as log from "tc-shared/log";
|
|
|
|
let key_listener: ((_: KeyEvent) => any)[] = [];
|
|
|
|
function listener_key(type: EventType, nevent: NKeyEvent) {
|
|
if(nevent.key_code === 'VoidSymbol' || nevent.key_code === 'error')
|
|
nevent.key_code = undefined; /* trigger event for state update */
|
|
|
|
let event: KeyEvent = {
|
|
type: type,
|
|
|
|
key: nevent.key_code,
|
|
key_code: nevent.key_code,
|
|
|
|
key_ctrl: nevent.key_ctrl,
|
|
key_shift: nevent.key_shift,
|
|
key_alt: nevent.key_alt,
|
|
key_windows: nevent.key_windows
|
|
} as any;
|
|
|
|
for (const listener of key_listener)
|
|
listener && listener(event);
|
|
}
|
|
|
|
function native_keyhook(event: NKeyEvent) {
|
|
//console.log("Native event!: %o", event);
|
|
|
|
if(event.type == 0)
|
|
listener_key(EventType.KEY_PRESS, event);
|
|
else if(event.type == 1)
|
|
listener_key(EventType.KEY_RELEASE, event);
|
|
else if(event.type == 2)
|
|
listener_key(EventType.KEY_TYPED, event);
|
|
else
|
|
console.warn(tr("Received unknown native event: %o"), event);
|
|
}
|
|
|
|
export async function initialize() : Promise<void> {
|
|
register_key_listener(listener_hook);
|
|
RegisterCallback(native_keyhook);
|
|
}
|
|
|
|
export function finalize() {
|
|
unregister_key_listener(listener_hook);
|
|
UnregisterCallback(native_keyhook);
|
|
}
|
|
|
|
export function register_key_listener(listener: (_: KeyEvent) => any) {
|
|
key_listener.push(listener);
|
|
}
|
|
|
|
export function unregister_key_listener(listener: (_: KeyEvent) => any) {
|
|
const index = key_listener.findIndex(e => e === listener);
|
|
if(index !== -1) key_listener.splice(index, 1);
|
|
}
|
|
|
|
let key_hooks: KeyHook[] = [];
|
|
|
|
interface CurrentState {
|
|
keys: {[code: string]:KeyEvent};
|
|
special: { [key:number]:boolean };
|
|
}
|
|
let current_state: CurrentState = {
|
|
special: []
|
|
} as any;
|
|
|
|
let key_hooks_active: KeyHook[] = [];
|
|
|
|
function listener_hook(event: KeyEvent) {
|
|
if(event.type == EventType.KEY_TYPED)
|
|
return;
|
|
|
|
let old_hooks = [...key_hooks_active];
|
|
let new_hooks = [];
|
|
|
|
current_state.special[SpecialKey.ALT] = event.key_alt;
|
|
current_state.special[SpecialKey.CTRL] = event.key_ctrl;
|
|
current_state.special[SpecialKey.SHIFT] = event.key_shift;
|
|
current_state.special[SpecialKey.WINDOWS] = event.key_windows;
|
|
|
|
current_state[event.key_code] = undefined;
|
|
if(event.type == EventType.KEY_PRESS) {
|
|
current_state[event.key_code] = event;
|
|
|
|
for(const hook of key_hooks) {
|
|
if(hook.key_code !== event.key_code) continue;
|
|
if(hook.key_alt != event.key_alt) continue;
|
|
if(hook.key_ctrl != event.key_ctrl) continue;
|
|
if(hook.key_shift != event.key_shift) continue;
|
|
if(hook.key_windows != event.key_windows) continue;
|
|
|
|
new_hooks.push(hook);
|
|
const index = old_hooks.findIndex(e => e === hook);
|
|
if(index === -1 && hook.callback_press) {
|
|
hook.callback_press();
|
|
log.trace(LogCategory.GENERAL, tr("Trigger key press for %o!"), hook);
|
|
}
|
|
}
|
|
}
|
|
|
|
//We have a new situation
|
|
for(const hook of old_hooks) {
|
|
//Do not test for meta key states because they could differ in a key release event
|
|
if(hook.key_code === event.key_code) {
|
|
if(hook.callback_release) {
|
|
hook.callback_release();
|
|
log.trace(LogCategory.GENERAL, tr("Trigger key release for %o!"), hook);
|
|
}
|
|
} else {
|
|
new_hooks.push(hook);
|
|
}
|
|
}
|
|
key_hooks_active = new_hooks;
|
|
}
|
|
|
|
|
|
export function register_key_hook(hook: KeyHook) {
|
|
key_hooks.push(hook);
|
|
}
|
|
|
|
export function unregister_key_hook(hook: KeyHook) {
|
|
let index;
|
|
|
|
index = key_hooks.findIndex(e => e === hook);
|
|
if(index !== -1) key_hooks.splice(index, 1);
|
|
|
|
index = key_hooks_active.findIndex(e => e === hook);
|
|
if(index !== -1) key_hooks_active.splice(index, 1);
|
|
}
|
|
|
|
export function key_pressed(code: string | SpecialKey) : boolean {
|
|
if(typeof(code) === 'string')
|
|
return typeof(current_state[code]) === "object";
|
|
return current_state.special[code];
|
|
} |