41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
|
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;
|
||
|
}
|
||
|
}
|