Skip to content

Instantly share code, notes, and snippets.

@marcelocra
Last active August 6, 2022 03:18
Show Gist options
  • Save marcelocra/930fc699e3b6367df1aad81811668e3c to your computer and use it in GitHub Desktop.
Save marcelocra/930fc699e3b6367df1aad81811668e3c to your computer and use it in GitHub Desktop.
ClojureScript goog keyboard events to create shortcuts (goog.ui.KeyboardShortcutHandler)
;; 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