2Elm.Native.Keyboard = function(elm) {
4 elm.Native = elm.Native || {};
5 if (elm.Native.Keyboard) return elm.Native.Keyboard;
7 // Duplicated from Native.Signal
8 function send(node, timestep, changed) {
10 for (var i = kids.length; i--; ) {
11 kids[i].recv(timestep, changed, node.id);
15 var Signal = Elm.Signal(elm);
16 var NList = Elm.Native.List(elm);
17 var Utils = Elm.Native.Utils(elm);
19 var downEvents = Signal.constant(0);
20 var upEvents = Signal.constant(0);
21 var blurEvents = Signal.constant(0);
23 elm.addListener([downEvents.id], document, 'keydown', function down(e) {
24 elm.notify(downEvents.id, e.keyCode);
27 elm.addListener([upEvents.id], document, 'keyup', function up(e) {
28 elm.notify(upEvents.id, e.keyCode);
31 elm.addListener([blurEvents.id], document, 'blur', function blur(e) {
32 elm.notify(blurEvents.id, NList.Nil);
35 function KeyMerge(down, up, blur) {
36 var args = [down,up,blur];
37 this.id = Utils.guid();
38 // Ignore starting values here
39 this.value = NList.Nil
44 var isChanged = false;
46 this.recv = function(timestep, changed, parentID) {
49 // We know this a change must only be one of the following cases
50 if (parentID === down.id && !(NList.member(down.value)(this.value))) {
52 this.value = NList.Cons(down.value, this.value);
54 if (parentID === up.id) {
56 var notEq = function(kc) { return kc !== up.value };
57 this.value = NList.filter(notEq)(this.value);
59 if (parentID === blur.id) {
61 this.value = NList.Nil;
65 send(this, timestep, isChanged);
71 for (var i = n; i--; ) { args[i].kids.push(this); }
75 var keysDown = Signal.dropRepeats(new KeyMerge(downEvents,upEvents,blurEvents));
77 function keySignal(f) {
78 var signal = A2(Signal.lift, f, keysDown);
79 // what's the significance of these two following lines? -jpm
80 keysDown.defaultNumberOfKids += 1;
81 signal.defaultNumberOfKids = 0;
85 function dir(up, down, left, right) {
88 while (ks.ctor === "::") {
90 case left : --x; break;
91 case right: ++x; break;
93 case down : --y; break;
97 return { _:{}, x:x, y:y };
102 function is(key) { return keySignal(NList.member(key)); }
104 var lastPressed = Signal.dropRepeats(downEvents);
106 return elm.Native.Keyboard = {
110 lastPressed:lastPressed
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list