on master help sign in
Change the name of `Keyboard` to `Keys`. Change some function names and change type of `charPressed` to be the latest char pressed (default is '\0')
79093ffevancz
libraries/Keyboard.elm → libraries/Keys.elmrenamed+12−5
@@ -2,9 +2,9 @@
2-- [Keyboard.Raw library](/docs/Signal/KeyboardRaw.elm) for a
2-- [Keyboard.Raw library](/docs/Signal/KeyboardRaw.elm) for a
3-- lower-level interface that will let you define more complicated behavior.
3-- lower-level interface that will let you define more complicated behavior.
4
4
5−module Keyboard where
5+module Keys where
6
6
7−import Native.Keyboard as N
7+import Native.Keys as N
8
8
9type KeyCode = Int
9type KeyCode = Int
10
10
@@ -22,9 +22,12 @@ arrows = N.directions 37 39 38 40
22wasd : Signal { x:Int, y:Int }
22wasd : Signal { x:Int, y:Int }
23wasd = N.directions 65 68 87 83
23wasd = N.directions 65 68 87 83
24
24
25+-- Custom key directions so that you can support different locales.
26+-- The plan is to have a locale independent version of this function
27+-- that uses the physical location of keys, but I don't know how to do it.
25directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
28directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
26
29
27−
30+-- Whether an arbitrary key is pressed.
28isDown : KeyCode -> Signal Bool
31isDown : KeyCode -> Signal Bool
29
32
30-- Whether the shift key is pressed.
33-- Whether the shift key is pressed.
@@ -39,5 +42,9 @@ ctrl = N.isDown 17
39space : Signal Bool
42space : Signal Bool
40space = N.isDown 32
43space = N.isDown 32
41
44
45+-- Whether the enter key is pressed.
46+enter : Signal Bool
47+enter = N.isDown 13
48+
42−keysDown : Signal [KeyCode]
49+down : Signal [KeyCode]
43−charPressed : Signal (Maybe Char)
50+presses : Signal Char
libraries/Native/Signal/Keyboard.js → libraries/Native/Signal/Keys.jsrenamed+8−11
@@ -1,15 +1,14 @@
1
1
2−Elm.Native.Keyboard = function(elm) {
2+Elm.Native.Keys = function(elm) {
3 'use strict';
3 'use strict';
4 elm.Native = elm.Native || {};
4 elm.Native = elm.Native || {};
5− if (elm.Native.Keyboard) return elm.Native.Keyboard;
5+ if (elm.Native.Keys) return elm.Native.Keys;
6
6
7 var Signal = Elm.Signal(elm);
7 var Signal = Elm.Signal(elm);
8 var NList = Elm.Native.List(elm);
8 var NList = Elm.Native.List(elm);
9− var Maybe = Elm.Maybe(elm);
10
9
11 var keysDown = Signal.constant(NList.Nil);
10 var keysDown = Signal.constant(NList.Nil);
12− var charPressed = Signal.constant(Maybe.Nothing);
11+ var charPressed = Signal.constant('\0');
13
12
14 function down(e) {
13 function down(e) {
15 if (NList.member(e.keyCode)(keysDown.value)) return;
14 if (NList.member(e.keyCode)(keysDown.value)) return;
@@ -28,9 +27,7 @@ Elm.Native.Keyboard = function(elm) {
28 if (!hasListener) elm.node.removeEventListener('blur', blur);
27 if (!hasListener) elm.node.removeEventListener('blur', blur);
29 }
28 }
30 function press(e) {
29 function press(e) {
31− var next = Maybe.Just(e.charCode || e.keyCode);
30+ var hasListener = elm.notify(charPressed.id, e.charCode || e.keyCode);
32− var hasListener = elm.notify(charPressed.id, next);
33− elm.notify(charPressed.id, Maybe.Nothing);
34 if (!hasListener) elm.node.removeEventListener('keypress', press);
31 if (!hasListener) elm.node.removeEventListener('keypress', press);
35 }
32 }
36
33
@@ -40,7 +37,7 @@ Elm.Native.Keyboard = function(elm) {
40 elm.node.addEventListener('keypress', press);
37 elm.node.addEventListener('keypress', press);
41
38
42 function keySignal(f) {
39 function keySignal(f) {
43− var signal = A2( Signal.lift, f, elm.Keyboard.Raw.keysDown );
40+ var signal = A2( Signal.lift, f, keysDown );
44 keysDown.defaultNumberOfKids += 1;
41 keysDown.defaultNumberOfKids += 1;
45 signal.defaultNumberOfKids = 0;
42 signal.defaultNumberOfKids = 0;
46 return signal;
43 return signal;
@@ -65,11 +62,11 @@ Elm.Native.Keyboard = function(elm) {
65
62
66 function is(key) { return keySignal(NList.member(key)); }
63 function is(key) { return keySignal(NList.member(key)); }
67
64
68− return elm.Native.Keyboard = {
65+ return elm.Native.Keys = {
69 isDown:is,
66 isDown:is,
70 directions:F4(dir),
67 directions:F4(dir),
71− keysDown:keysDown,
68+ down:keysDown,
72− charPressed:charPressed
69+ presses:charPressed
73 };
70 };
74
71
75};
72};