Skip to content

Instantly share code, notes, and snippets.

@albertzak
Created September 20, 2021 10:36
Show Gist options
  • Save albertzak/b8be121ac618bc192322c9dfe8bacf84 to your computer and use it in GitHub Desktop.
Save albertzak/b8be121ac618bc192322c9dfe8bacf84 to your computer and use it in GitHub Desktop.
(defn on-konami-code []
(prn :konami-code-detected))
(defn setup-konami-code-listener! []
(let
[key
{37 :left
38 :up
39 :right
40 :down
65 :a
66 :b}
; note: intentionally missing first up because last-at will block first valid key
code [:up :down :down :left :right :left :right :b :a]
max-delay 1500
valid-keys (set (vals key))
buffer (atom [])
last-at (atom 0)
tick #(reset! last-at (js/Date.))
reset
(fn []
(reset! buffer [])
(tick))]
(js/document.addEventListener
"keyup"
(fn [^js e]
(let [k (key (.-keyCode e))]
(if (and (valid-keys k)
(<= (- (js/Date.) @last-at) max-delay))
(do
(tick)
(swap! buffer conj k)
(when (= (take-last (count code) @buffer)
code)
(#'on-konami-code)))
(reset)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment