parent
ec7523f9b0
commit
f764048f23
@ -1,20 +1,26 @@
|
||||
|
||||
import { Directive, HostListener, Input } from '@angular/core';
|
||||
import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';
|
||||
|
||||
@Directive({
|
||||
selector: '[RecordKeyChord]'
|
||||
})
|
||||
export class RecordKeyChord {
|
||||
@Input('enable') enable: boolean = false;
|
||||
@Output() finishedKeyChord = new EventEmitter<string[]>;
|
||||
|
||||
chordStack: string[] = [];
|
||||
|
||||
@HostListener('window:keydown', ['$event'])
|
||||
handleKeyDown(event: KeyboardEvent) {
|
||||
console.log("keydown:", event);
|
||||
this.chordStack.push(event.key);
|
||||
|
||||
}
|
||||
|
||||
@HostListener('window:keyup', ['$event'])
|
||||
handleKeyUp(event: KeyboardEvent) {
|
||||
console.log("keydown:", event);
|
||||
console.log("keyup:", event);
|
||||
this.finishedKeyChord.next(this.chordStack);
|
||||
this.chordStack = [];
|
||||
}
|
||||
|
||||
}
|
@ -1,19 +1,39 @@
|
||||
import { Injectable, NgZone } from '@angular/core';
|
||||
import { register } from '@tauri-apps/api/globalShortcut';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut';
|
||||
import { EMPTY, Observable, catchError, empty, from } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ShortcutService {
|
||||
bound: Map<ShortcutHandler, string> = new Map<ShortcutHandler, string>();
|
||||
|
||||
constructor(private zone: NgZone) { }
|
||||
register(shortcut: string) {
|
||||
register(shortcut: string, handler: ShortcutHandler) {
|
||||
this.bound.set(handler, shortcut);
|
||||
|
||||
return from(register(shortcut, (s) => {
|
||||
this.zone.run(() => handler(s));
|
||||
}));
|
||||
}
|
||||
|
||||
unregister(handler: ShortcutHandler) {
|
||||
const shortcut = this.bound.get(handler);
|
||||
this.bound.delete(handler);
|
||||
|
||||
return shortcut ? from(unregister(shortcut)) : EMPTY;
|
||||
}
|
||||
|
||||
rebind(shortcut: string, handler: ShortcutHandler) {
|
||||
const prevShortcut = this.bound.get(handler)!;
|
||||
this.register(shortcut, handler).subscribe(
|
||||
{
|
||||
error: (_err) => {
|
||||
this.register(prevShortcut, handler);
|
||||
console.log("Got error during binding, rebinding previous");
|
||||
return EMPTY;
|
||||
|
||||
return new Observable((subscriber) => {
|
||||
register(shortcut, (s) => {
|
||||
this.zone.run(() => subscriber.next(s));
|
||||
})
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue