Last active
August 6, 2022 03:18
-
-
Save marcelocra/930fc699e3b6367df1aad81811668e3c to your computer and use it in GitHub Desktop.
ClojureScript goog keyboard events to create shortcuts (goog.ui.KeyboardShortcutHandler)
This file contains hidden or 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
;; Using goog.ui.KeyboardShortcutHandler with ClojureScript to support keyboard input events (for example, to create shortcuts). | |
;; Documentation about goog.ui.KeyboardShortcutHandler: | |
;; https://github.com/google/closure-library/blob/master/closure/goog/demos/keyboardshortcuts.html | |
(ns example.detect-keyboard-effects | |
(:require [goog.events :as events]) | |
(:import [goog.ui KeyboardShortcutHandler])) | |
(defn setup-keyboard-shortcuts [] | |
(let [shortcuts (KeyboardShortcutHandler. js/document)] | |
(do | |
(.registerShortcut shortcuts "event-id" "ctrl+j") | |
(events/listen | |
shortcuts | |
KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED | |
(fn [event] (js/console.log event.identifier)))))) | |
(defonce activate-keyboard-shortcuts (setup-keyboard-shortcuts)) | |
;; Running this will print 'event-id' to the console whenever the user | |
;; presses 'ctrl+j'. It supports a lot of different keyboard combinarions, | |
;; including shift, meta, function keys, enter, space, etc. Check the doc | |
;; above for details. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment