Closed houshuang closed 11 years ago
Here's a much nicer wrapper:
; Provides a bridge to the jkeymaster library
; First acquire a provider, and then register a keyboard shortcut
; Example code:
; (let [provider (keys.keymaster/provider)]
; (keys.keymaster/register (provider) "control shift 1" #(println)))
(ns keymaster.keymaster
(:gen-class)
(:use (com.tulskiy.keymaster.common)))
(defn provider []
"Gets and initiates a keymaster provider, which must be passed to register to register shortcuts"
(let [provider (com.tulskiy.keymaster.common.Provider/getCurrentProvider true)]
(.init provider)
provider))
(defn conv-keystroke [x]
"Takes keystroke in the form \"control shift 1\" and returns a Keystroke class"
(javax.swing.KeyStroke/getKeyStroke x))
(defn conv-listener [f]
"Takes a function with one argument, which will get passed the keycode, and creates a listener
Todo: How to accept a function with or without a parameter to accept hotKey?"
(proxy [com.tulskiy.keymaster.common.HotKeyListener] [] (onHotKey [hotKey] (f))))
(defn register
[provider shortcut listener]
"Registers a shortcut on provider, which will trigger listener (with one argument)"
(let [k (conv-keystroke shortcut)
l (conv-listener listener)]
(.register provider k l)))
Cool, thank you! I guess I'll set up a proper wiki later on and add this code. Or, if there's a way to include this code in the library without adding dependency on clojure, I'd be happy to merge a pull request :)
I'm trying to get some more eyes on it first ( http://codereview.stackexchange.com/questions/25575/api-wrapper-for-clojure-idiomatic-style-for-libraries), but then I'll upload it to clojars, so people can include it in their code very simply (clojure has very nice dependency/package management etc). But feel free to include it as well.
Stian
On Sun, Apr 28, 2013 at 4:03 AM, Denis Tulskiy notifications@github.comwrote:
Cool, thank you! I guess I'll set up a proper wiki later on and add this code. Or, if there's a way to include this code in the library without adding dependency on clojure, I'd be happy to merge a pull request :)
— Reply to this email directly or view it on GitHubhttps://github.com/tulskiy/jkeymaster/issues/12#issuecomment-17130358 .
http://reganmian.net/blog -- Random Stuff that Matters
Put a library here: http://github.com/houshuang/keymaster-clj
I'm just leaving it here because it might be useful to others, here's a minimal (and ugly, but functional) example using jkeymaster from Clojure:
add this to project.clj
And this is the main code:
I'll keep playing with this and maybe even write a tiny wrapper library for Clojure (which I'm just learning).