2019-10-25 19:51:40 -04:00
|
|
|
window["require_setup"](module);
|
|
|
|
|
|
|
|
import {KeyEvent as NKeyEvent} from "teaclient_ppt";
|
|
|
|
namespace _ppt {
|
|
|
|
let key_listener: ((_: ppt.KeyEvent) => any)[] = [];
|
|
|
|
|
|
|
|
let native_ppt;
|
|
|
|
function listener_key(type: ppt.EventType, nevent: NKeyEvent) {
|
|
|
|
if(nevent.key_code === 'VoidSymbol' || nevent.key_code === 'error')
|
|
|
|
nevent.key_code = undefined; /* trigger event for state update */
|
|
|
|
|
|
|
|
let event: ppt.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;
|
2019-11-24 12:38:50 -05:00
|
|
|
//console.debug("Trigger key event %o", type);
|
2019-10-25 19:51:40 -04:00
|
|
|
|
|
|
|
for (const listener of key_listener)
|
2019-11-24 12:38:50 -05:00
|
|
|
listener && listener(event);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function native_keyhook(event: NKeyEvent) {
|
|
|
|
//console.log("Native event!: %o", event);
|
|
|
|
|
|
|
|
if(event.type == 0)
|
|
|
|
listener_key(ppt.EventType.KEY_PRESS, event);
|
|
|
|
else if(event.type == 1)
|
|
|
|
listener_key(ppt.EventType.KEY_RELEASE, event);
|
|
|
|
else if(event.type == 2)
|
|
|
|
listener_key(ppt.EventType.KEY_TYPED, event);
|
2019-11-24 12:38:50 -05:00
|
|
|
else
|
|
|
|
console.warn(tr("Received unknown native event: %o"), event);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function initialize() : Promise<void> {
|
|
|
|
native_ppt = require("teaclient_ppt");
|
|
|
|
|
|
|
|
register_key_listener(listener_hook);
|
|
|
|
native_ppt.RegisterCallback(native_keyhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function finalize() {
|
|
|
|
unregister_key_listener(listener_hook);
|
|
|
|
native_ppt.UnregisterCallback(native_keyhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function register_key_listener(listener: (_: ppt.KeyEvent) => any) {
|
|
|
|
key_listener.push(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unregister_key_listener(listener: (_: ppt.KeyEvent) => any) {
|
|
|
|
key_listener.remove(listener);
|
|
|
|
}
|
|
|
|
|
2019-11-24 09:52:42 -05:00
|
|
|
let key_hooks: ppt.KeyHook[] = [];
|
2019-10-25 19:51:40 -04:00
|
|
|
|
2019-11-24 09:52:42 -05:00
|
|
|
interface CurrentState {
|
|
|
|
keys: {[code: string]:ppt.KeyEvent};
|
2019-10-25 19:51:40 -04:00
|
|
|
special: { [key:number]:boolean };
|
|
|
|
}
|
|
|
|
let current_state: CurrentState = {
|
|
|
|
special: []
|
|
|
|
} as any;
|
|
|
|
|
|
|
|
let key_hooks_active: ppt.KeyHook[] = [];
|
|
|
|
|
|
|
|
function listener_hook(event: ppt.KeyEvent) {
|
|
|
|
if(event.type == ppt.EventType.KEY_TYPED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let old_hooks = [...key_hooks_active];
|
|
|
|
let new_hooks = [];
|
|
|
|
|
|
|
|
current_state.special[ppt.SpecialKey.ALT] = event.key_alt;
|
|
|
|
current_state.special[ppt.SpecialKey.CTRL] = event.key_ctrl;
|
|
|
|
current_state.special[ppt.SpecialKey.SHIFT] = event.key_shift;
|
|
|
|
current_state.special[ppt.SpecialKey.WINDOWS] = event.key_windows;
|
|
|
|
|
2019-11-24 09:52:42 -05:00
|
|
|
current_state[event.key_code] = undefined;
|
2019-10-25 19:51:40 -04:00
|
|
|
if(event.type == ppt.EventType.KEY_PRESS) {
|
2019-11-24 09:52:42 -05:00
|
|
|
current_state[event.key_code] = event;
|
2019-10-25 19:51:40 -04:00
|
|
|
|
|
|
|
for(const hook of key_hooks) {
|
2019-11-24 09:52:42 -05:00
|
|
|
if(hook.key_code !== event.key_code) continue;
|
2019-10-25 19:51:40 -04:00
|
|
|
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);
|
|
|
|
if(!old_hooks.remove(hook) && hook.callback_press) {
|
|
|
|
hook.callback_press();
|
2019-11-24 09:52:42 -05:00
|
|
|
log.trace(LogCategory.GENERAL, tr("Trigger key press for %o!"), hook);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//We have a new situation
|
2019-11-24 09:52:42 -05:00
|
|
|
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);
|
2019-10-25 19:51:40 -04:00
|
|
|
}
|
2019-11-24 09:52:42 -05:00
|
|
|
}
|
2019-10-25 19:51:40 -04:00
|
|
|
key_hooks_active = new_hooks;
|
|
|
|
}
|
|
|
|
|
2019-11-24 09:52:42 -05:00
|
|
|
|
2019-10-25 19:51:40 -04:00
|
|
|
export function register_key_hook(hook: ppt.KeyHook) {
|
|
|
|
key_hooks.push(hook);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unregister_key_hook(hook: ppt.KeyHook) {
|
|
|
|
key_hooks.remove(hook);
|
|
|
|
key_hooks_active.remove(hook);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function key_pressed(code: string | ppt.SpecialKey) : boolean {
|
|
|
|
if(typeof(code) === 'string')
|
2019-11-24 12:38:50 -05:00
|
|
|
return typeof(current_state[code]) === "object";
|
2019-10-25 19:51:40 -04:00
|
|
|
return current_state.special[code];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Object.assign(window["ppt"] || (window["ppt"] = {} as any), _ppt);
|
|
|
|
console.dir(_ppt);
|