feat: add kb shortcut detection

This commit is contained in:
Oliver Booth 2023-08-10 22:47:28 +01:00
parent e939174040
commit cf2a5c2ffb
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
3 changed files with 40 additions and 1 deletions

View File

@ -28,6 +28,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ts", "ts", "{BB9F76AC-292A-
src\ts\Author.ts = src\ts\Author.ts
src\ts\TimeUtility.ts = src\ts\TimeUtility.ts
src\ts\UI.ts = src\ts\UI.ts
src\ts\Input.ts = src\ts\Input.ts
EndProjectSection
EndProject
Global

33
src/ts/Input.ts Normal file
View File

@ -0,0 +1,33 @@
class Input {
public static readonly KONAMI_CODE = [
"ArrowUp",
"ArrowUp",
"ArrowDown",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
"ArrowLeft",
"ArrowRight",
"b",
"a",
"Enter"
];
public static registerShortcut(shortcut: string | string[], callback: Function) {
let keys: string[];
if (typeof shortcut === 'string') keys = shortcut.split(' ');
else keys = shortcut;
let sequence: string[] = [];
document.addEventListener('keydown', e => {
sequence.push(e.key);
if (sequence.slice(-keys.length).join(' ') === keys.join(' ')) {
callback();
sequence = [];
}
});
}
}
export default Input;

View File

@ -1,9 +1,14 @@
import API from "./API";
import API from "./API";
import UI from "./UI";
import Input from "./Input";
declare const Handlebars: any;
(() => {
Input.registerShortcut(Input.KONAMI_CODE, () => {
window.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "_blank");
});
const blogPostContainer = UI.blogPostContainer;
if (blogPostContainer) {
const template = Handlebars.compile(UI.blogPostTemplate.innerHTML);