Skip to content

Instantly share code, notes, and snippets.

@rauhs
Created March 3, 2016 08:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rauhs/ec1a7b94a6481ae4cf1d to your computer and use it in GitHub Desktop.
Save rauhs/ec1a7b94a6481ae4cf1d to your computer and use it in GitHub Desktop.
(ns srs-c.utils.keyboard
(:require-macros
[klang.macros :refer [log! debg! trac! info! warn! erro! crit! fata! env!]]
[srs-c.macros :refer [onlydev onlyprod]])
(:require [goog.events])
(:import [goog.ui KeyboardShortcutHandler]))
(defn install-shortcut!
"Installs a Keyboard Shortcut handler.
The key is a string the trigger is a function that will receive the keyboard event as the
first argument. If once? is true the keyboard shortcut is only fired once.
The unregister handler is returned and can be called to unregister the listener."
([key trigger] (install-shortcut! key trigger true))
([key trigger once?]
(let [handler (new KeyboardShortcutHandler js/window)]
(.registerShortcut handler (str key once?) key)
(.listen goog.events
handler
KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED
(fn [e]
(trigger e)
(when once?
(trac! "Keyboard shortcut triggered for key" key "and unregistered.")
(.unregisterShortcut handler keys))))
(trac! "Keyboard shortcut installed " key)
(fn [] (.unregisterShortcut handler keys)))))
(defui
Login-Popup
;; Shows the login screen
Object
(componentDidMount [this]
(om/set-state! this {:esc-handler (keyboard/install-shortcut! "esc" #(goto % (route-home)))}))
(componentWillUnmount [this]
((om/get-state this :esc-handler)))
(render [this]
nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment