on master help sign in
Add and fix up the Keyboard inputs.
8d26a1devancz
core/Keyboard.elmnew file+36
@@ -0,0 +1,36 @@
1+-- These are nicely curated inputs from the keyboard. See the
2+-- [Keyboard.Raw library](/docs/Signal/KeyboardRaw.elm) for a
3+-- lower-level interface that will let you define more complicated behavior.
4+
5+module Keyboard where
6+
7+-- A signal of records indicating which arrow keys are pressed.
8+--
9+-- `{ x = 0, y = 0 }` when pressing no arrows.
10+-- `{ x =-1, y = 0 }` when pressing the left arrow.
11+-- `{ x = 1, y = 1 }` when pressing the up and right arrows.
12+-- `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
13+arrows : Signal { x:Int, y:Int }
14+arrows = Native.Keyboard.dir 37 39 38 40
15+
16+-- Just like the arrows signal, but this uses keys w, a, s, and d,
17+-- which are common controls for many computer games.
18+wasd : Signal { x:Int, y:Int }
19+wasd = Native.Keyboard.dir 65 68 87 83
20+
21+
22+
23+isDown : KeyCode -> Signal Bool
24+isDown = Native.Keyboard.isDown
25+
26+-- Whether the shift key is pressed.
27+shift : Signal Bool
28+shift = Native.Keyboard.isDown 16
29+
30+-- Whether the control key is pressed.
31+ctrl : Signal Bool
32+ctrl = Native.Keyboard.isDown 17
33+
34+-- Whether the space key is pressed.
35+space : Signal Bool
36+space = Native.Keyboard.isDown 32
core/Keyboard/Raw.elmnew file+5
@@ -0,0 +1,5 @@
1+
2+module Keyboard.Raw where
3+
4+keysDown = Native.Keyboard.Raw.keysDown
5+charPressed = Native.Keyboard.Raw.charPressed
core/Native/Keyboard.js+15−44
@@ -49,7 +49,9 @@ These are nicely curated inputs from the keyboard. See the
49lower-level interface that will let you define more complicated behavior.
49lower-level interface that will let you define more complicated behavior.
50!*/
50!*/
51
51
52−(function() {
52+function nativeKeyboard(elm) {
53+ 'use strict';
54+
53 function keySignal(f) {
55 function keySignal(f) {
54 var signal = Elm.Signal.lift(f)(Elm.Keyboard.Raw.keysDown);
56 var signal = Elm.Signal.lift(f)(Elm.Keyboard.Raw.keysDown);
55 Elm.Keyboard.Raw.keysDown.defaultNumberOfKids += 1;
57 Elm.Keyboard.Raw.keysDown.defaultNumberOfKids += 1;
@@ -57,67 +59,36 @@ lower-level interface that will let you define more complicated behavior.
57 return signal;
59 return signal;
58 }
60 }
59
61
60− function dir(left,right,up,down) {
62+ function dir(up) { return function(down) {
63+ return function(left) { return function(right) {
61 function f(ks) {
64 function f(ks) {
62 var x = 0, y = 0;
65 var x = 0, y = 0;
63− while (ks[0] == "Cons") {
66+ while (ks.ctor == "Cons") {
64− switch (ks[1]) {
67+ switch (ks._0) {
65 case left : --x; break;
68 case left : --x; break;
66 case right: ++x; break;
69 case right: ++x; break;
67 case up : ++y; break;
70 case up : ++y; break;
68 case down : --y; break;
71 case down : --y; break;
69 }
72 }
70− ks = ks[2];
73+ ks = ks._1;
71 }
74 }
72− return { _:[true], x:[x], y:[y] };
75+ return { _:{}, x:x, y:y };
73 }
76 }
74 return keySignal(f);
77 return keySignal(f);
78+ }}}
75 }
79 }
76
80
77 function is(key) {
81 function is(key) {
78 function f(ks) {
82 function f(ks) {
79− while (ks[0] == "Cons") {
83+ while (ks.ctor == "Cons") {
80− if (key == ks[1]) return true;
84+ if (key == ks._0) return true;
81− ks = ks[2];
85+ ks = ks._1;
82 }
86 }
83 return false;
87 return false;
84 }
88 }
85 return keySignal(f);
89 return keySignal(f);
86 }
90 }
87
91
88− /*[Directions]*/
92+ elm.Native.Keyboard = { isDown:is, dir:dir };
89−
90− /** arrows : Signal { x:Int, y:Int }
91− A signal of records indicating which arrow keys are pressed.
92−
93− `{ x = 0, y = 0 }` when pressing no arrows.
94− `{ x =-1, y = 0 }` when pressing the left arrow.
95− `{ x = 1, y = 1 }` when pressing the up and right arrows.
96− `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
97− **/
98− Elm.Keyboard.arrows = dir(37,39,38,40);
99−
100− /** wasd : Signal { x:Int, y:Int }
101− Just like the arrows signal, but this uses keys w, a, s, and d,
102− which are common controls for many computer games.
103− **/
104− Elm.Keyboard.wasd = dir(65,68,87,83);
105−
106− /*[Modifiers]*/
107−
108− /** shift : Signal Bool
109− Whether the shift key is pressed.
110− **/
111− Elm.Keyboard.shift = is(16);
112−
113− /** ctrl : Signal Bool
114− Whether the control key is pressed.
115− **/
116− Elm.Keyboard.ctrl = is(17);
117−
118− /** space : Signal Bool
119− Whether the space key is pressed.
120− **/
121− Elm.Keyboard.space = is(32);
122
93
123−}());
94+}
core/Native/Keyboard/Raw.jsnew file+33
@@ -0,0 +1,33 @@
1+function nativeKeyboardRaw(elm) {
2+ var keysDown = Elm.Signal.constant(Elm.Native.List.Nil);
3+ var charPressed = Elm.Signal.constant(Elm.Maybe.Nothing);
4+
5+ Value.addListener(document, 'keydown', function(e) {
6+ if (Elm.List.member(e.keyCode)(keysDown.value)) return;
7+ var list = Elm.Native.List.Cons(e.keyCode, keysDown.value);
8+ var hasListener = Dispatcher.notify(keysDown.id, list);
9+ if (!hasListener)
10+ this.removeEventListener('keydown',arguments.callee,false);
11+ });
12+ Value.addListener(document, 'keyup', function(e) {
13+ function notEq(kc) { return kc !== e.keyCode; }
14+ var codes = Elm.List.filter(notEq)(keysDown.value);
15+ var hasListener = Dispatcher.notify(keysDown.id, codes);
16+ if (!hasListener)
17+ this.removeEventListener('keyup',arguments.callee,false);
18+ });
19+ Value.addListener(window, 'blur', function(e) {
20+ var hasListener = Dispatcher.notify(keysDown.id, ["Nil"]);
21+ if (!hasListener)
22+ this.removeEventListener('blur',arguments.callee,false);
23+ });
24+ Value.addListener(document, 'keypress', function(e) {
25+ var next = Elm.Maybe.Just(e.charCode || e.keyCode);
26+ var hasListener = Dispatcher.notify(charPressed.id, next);
27+ Dispatcher.notify(charPressed.id, Elm.Maybe.Nothing);
28+ if (!hasListener)
29+ this.removeEventListener('keypress',arguments.callee,false);
30+ });
31+
32+ elm.Native.Keyboard.Raw = {keysDown:keysDown, charPressed:charPressed};
33+}
core/WebSocket.elm+2−2
@@ -1,5 +1,5 @@
1−-- A library for low latency HTTP communication. See the HTTP library for standard
1+-- A library for low latency HTTP communication. See the HTTP library for
2−-- requests like GET, POST, etc.
2+-- standard requests like GET, POST, etc.
3
3
4module WebSocket where
4module WebSocket where
5
5