Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: candera/kchordr
base: 53b886fbd7
...
head fork: candera/kchordr
compare: d596f0c18a
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
3  project.clj
@@ -14,5 +14,4 @@
:repositories {"local" "file:repo"
"sonatype" "http://oss.sonatype.org/content/repositories/releases"
"nativelibs4java-repo" "http://nativelibs4java.sourceforge.net/maven"}
- ;; :jvm-opts "-Djna.library.path=ext"
- )
+ :jvm-opts ["-Djna.library.path=ext"])
View
2  scratch-jvm.clj
@@ -68,6 +68,8 @@
(def jdn (process (state default-key-behaviors) (->event :j :dn)))
+(handle-deciding-regular-press jdn :q)
+
(process jdn (->event :j :up))
(process jdn (->event :q :dn))
jdn
View
46 src/clj/kchordr/core.clj
@@ -1,5 +1,5 @@
(ns kchordr.core
- (:refer-clojure :exclude [key])
+ (:refer-clojure :exclude [key send])
(:use [clojure.core.match :only (match)]
kchordr.keycodes)
(:import interception.InterceptionLibrary))
@@ -13,8 +13,13 @@
(def ^{:doc "Map of keys to behaviors. Absence from this list means it's a regular key."}
default-key-behaviors
- {:j [:modifier-alias :rshift]})
+ {:j {:keyclass :modifier-alias :alias :rshift}})
+;; {:keystate {:j :undecided :k :rcontrol}
+;; :to-send ({:key :q :direction :dn}
+;; {:key :r :direction :up})
+;; :behaviors {:j {:keyclass :modifier-alias :alias :rshift]
+;; :k {:keyclass :modifier-alias :alias :rcontrol]}}
(defn state
"Returns a new key-state object."
[behaviors]
@@ -59,7 +64,6 @@
(defn undecided-modifier-downs
"Return a seq of events for the undecided modifiers."
[state]
- {:keystate {:j :undecided}}
(->> (:keystate state)
(filter (fn [[k v]] (= v :undecided)))
(map first)
@@ -105,6 +109,36 @@
[(->event key :dn)])
:keystate (decide-modifiers state)))
+(defn alias
+ "Return the alias of key, or nil if none."
+ [state key]
+ (get-in state [:keystate key]))
+
+(defn aliasing?
+ "Return true if key is currently being aliased to something else."
+ [state key]
+ (let [alias (get-in state [:keystate key])]
+ (and alias (not= :undecided alias))))
+
+(defn send
+ "Return a state object with the additional key events added to the
+ :to-send seq."
+ [state & events]
+ (->> events
+ (partition 2)
+ (map #(apply ->event %))
+ (update-in state [:to-send] concat)))
+
+(defn release-modifier-alias
+ "When a modifier key goes up, we need to send a key down & up for the
+ key itself (if it is not yet aliasing) or a key up for the unaliased
+ key."
+ [state key]
+ (let [state (if (aliasing? state key)
+ (send state (alias state key) :up)
+ (send state key :dn key :up))]
+ (update-in state [:keystate] #(dissoc % key))))
+
(defn handle-default
"Handle a key event by simply appending it to the list of events to
transmit."
@@ -116,7 +150,10 @@
"Given the current state and a key event, return an updated state."
[state event]
(let [{:keys [key direction]} event
- [keyclass alias] (get-in state [:behaviors key] [:regular])
+ {:keys [keyclass alias]} (get-in
+ state
+ [:behaviors key]
+ {:keyclass :regular})
keystate (:keystate state)]
(log "Beginning state is" state)
(log "Processing event" key direction)
@@ -124,6 +161,7 @@
(log "Keystate is" keystate)
(match [keyclass direction (undecided-modifier? keystate)]
[:modifier-alias :dn _] (handle-modifier-press state key)
+ [:modifier-alias :up _] (release-modifier-alias state key)
[:regular :dn true] (handle-deciding-regular-press state key)
[_ _ _] (handle-default state key direction))))
View
12 test/kchordr/test/core.clj
@@ -1,5 +1,6 @@
(ns kchordr.test.core
- (:use [kchordr.core])
+ (:refer-clojure :exclude (send))
+ (:require [kchordr.core :refer (state process default-key-behaviors ->event)])
(:use [clojure.test]))
(defn- sent
@@ -8,12 +9,18 @@
[events]
(:to-send (reduce #(process %1 %2) (state default-key-behaviors) events)))
+(defn- press
+ "Given a seq of pairs of [key direction], return the sent keys as a
+ similar seq."
+ [pressed]
+ (map (juxt :key :direction) (sent (map ->event pressed))))
+
(deftest key-tests
(are [pressed anticipated]
;; We use vectors as the test format because they're easier to
;; read, but we still want to use maps as the underlying
;; construct for their flexibility. Yay juxt!
- (= anticipated (map (juxt :key :direction) (sent (map ->event pressed))))
+ (= anticipated (press pressed))
;; Single regular key press
[[:b :dn]]
[[:b :dn]]
@@ -46,5 +53,6 @@
;; by modifier alias press and release
[[:j :dn] [:x :dn] [:x :up] [:j :up] [:j :dn] [:j :up]]
[[:rshift :dn] [:x :dn] [:x :up] [:rshift :up] [:j :dn] [:j :up]]
+
))
View
8 test/kchordr/test/keycodes.clj
@@ -2,8 +2,8 @@
(:use kchordr.keycodes
clojure.test))
-(deftest keycode-tests
- (are [original expected-translation]
- (= expected-translation (translate original))
+;; (deftest keycode-tests
+;; (are [original expected-translation]
+;; (= expected-translation (translate original))
- 30 :a))
+;; 30 :a))

No commit comments for this range

Something went wrong with that request. Please try again.