assassin-bug/framework/ui/menu/keyboard-manager.js

41 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2022-11-26 01:22:02 +00:00
export class KeyboardManager {
constructor(menu) {
this.menu = menu;
}
init() {
this.menu
.getContainer()
.addEventListener('keydown', this.handler.bind(this));
// This trick let's us detect the press of the back or forward buttons to exit out of the menu.
window.onpopstate = () => this.menu.clickCancelAction();
}
handler(event) {
switch (event.key) {
case 'ArrowDown':
event.preventDefault();
this.menu.focusNext();
break;
case 'ArrowUp':
event.preventDefault();
this.menu.focusPrevious();
break;
case 'Enter':
event.preventDefault();
this.menu.clickDefaultAction();
break;
case 'Escape':
event.preventDefault();
this.menu.clickCancelAction();
break;
default:
break;
}
}
release() {
this.menu
.getContainer()
.removeEventListener('keydown', this.handler.bind(this));
window.onpopstate = null;
}
}