tulskiy / jkeymaster

A library for registering global hotkeys in java with JNA. The goal is to support X11-based platforms, Windows and MacOSX
GNU Lesser General Public License v3.0
234 stars 45 forks source link

Clojure example #12

Closed houshuang closed 11 years ago

houshuang commented 11 years ago

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

:dependencies [[com.github.tulskiy/jkeymaster "1.1"]]

And this is the main code:

(ns keys.core
  (:gen-class)
  (:use (com.tulskiy.keymaster.common)))

(defn register []
  (let [provider (com.tulskiy.keymaster.common.Provider/getCurrentProvider true)
        listener (proxy [com.tulskiy.keymaster.common.HotKeyListener] [] (onHotKey [hotKey] (println hotKey)))
        keystroke (javax.swing.KeyStroke/getKeyStroke "control shift 1")]
    (.init provider)
    (.register provider keystroke listener)))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (register))

I'll keep playing with this and maybe even write a tiny wrapper library for Clojure (which I'm just learning).

houshuang commented 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)))
tulskiy commented 11 years ago

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 :)

houshuang commented 11 years ago

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

houshuang commented 11 years ago

Put a library here: http://github.com/houshuang/keymaster-clj