Created
March 3, 2016 08:43
-
-
Save rauhs/ec1a7b94a6481ae4cf1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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