From cf2a5c2ffbea489aa42dbe0b906968e29cd1917c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 10 Aug 2023 22:47:28 +0100 Subject: [PATCH] feat: add kb shortcut detection --- OliverBooth.sln | 1 + src/ts/Input.ts | 33 +++++++++++++++++++++++++++++++++ src/ts/app.ts | 7 ++++++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/ts/Input.ts diff --git a/OliverBooth.sln b/OliverBooth.sln index 4838019..4484d78 100644 --- a/OliverBooth.sln +++ b/OliverBooth.sln @@ -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 diff --git a/src/ts/Input.ts b/src/ts/Input.ts new file mode 100644 index 0000000..f584c21 --- /dev/null +++ b/src/ts/Input.ts @@ -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; \ No newline at end of file diff --git a/src/ts/app.ts b/src/ts/app.ts index 99346ba..60e8165 100644 --- a/src/ts/app.ts +++ b/src/ts/app.ts @@ -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);