on master help sign in
Merge pull request #250 from mgold/dev
01c303bEvan Czaplicki
libraries/Keyboard.elm+39−20
@@ -1,57 +1,76 @@
1module Keyboard where
1module Keyboard where
2
2
3+{-| Library for working with keyboard input.
4+
5+# Representing Keys
6+@docs KeyCode
7+
8+# Directions
9+@docs arrows, wasd, directions
10+
11+# Specific Keys
12+@docs shift, enter, space, ctrl
13+
14+# General Keypresses
15+@docs isDown, keysDown, lastPressed
16+
17+-}
18+
3import Signal (Signal)
19import Signal (Signal)
4import Native.Keyboard
20import Native.Keyboard
5
21
6−-- Type alias to make it clearer what integers are supposed to represent
22+{-| Type alias to make it clearer what integers are supposed to represent
7−-- in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
23+ in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
8−-- [`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
24+ [`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
25+ Use the uppercase character with `toCode`. -}
9type KeyCode = Int
26type KeyCode = Int
10
27
11−-- Custom key directions so that you can support different locales.
28+{-| Custom key directions to support different locales. The order is up, down,
29+left, right. -}
12-- The plan is to have a locale independent version of this function
30-- The plan is to have a locale independent version of this function
13-- that uses the physical location of keys, but I don't know how to do it.
31-- that uses the physical location of keys, but I don't know how to do it.
14directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
32directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
15directions = Native.Keyboard.directions
33directions = Native.Keyboard.directions
16
34
17−-- A signal of records indicating which arrow keys are pressed.
35+{-| A signal of records indicating which arrow keys are pressed.
18−--
36+
19−-- `{ x = 0, y = 0 }` when pressing no arrows.<br>
37+ `{ x = 0, y = 0 }` when pressing no arrows.<br>
20−-- `{ x =-1, y = 0 }` when pressing the left arrow.<br>
38+ `{ x =-1, y = 0 }` when pressing the left arrow.<br>
21−-- `{ x = 1, y = 1 }` when pressing the up and right arrows.<br>
39+ `{ x = 1, y = 1 }` when pressing the up and right arrows.<br>
22−-- `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
40+ `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
41+-}
23arrows : Signal { x:Int, y:Int }
42arrows : Signal { x:Int, y:Int }
24arrows = directions 38 40 37 39
43arrows = directions 38 40 37 39
25
44
26−-- Just like the arrows signal, but this uses keys w, a, s, and d,
45+{-| Just like the arrows signal, but this uses keys w, a, s, and d,
27−-- which are common controls for many computer games.
46+which are common controls for many computer games. -}
28wasd : Signal { x:Int, y:Int }
47wasd : Signal { x:Int, y:Int }
29wasd = directions 87 83 65 68
48wasd = directions 87 83 65 68
30
49
31−-- Whether an arbitrary key is pressed.
50+{-| Whether an arbitrary key is pressed. -}
32isDown : KeyCode -> Signal Bool
51isDown : KeyCode -> Signal Bool
33isDown = Native.Keyboard.isDown
52isDown = Native.Keyboard.isDown
34
53
35−-- Whether the shift key is pressed.
54+{-| Whether the shift key is pressed. -}
36shift : Signal Bool
55shift : Signal Bool
37shift = isDown 16
56shift = isDown 16
38
57
39−-- Whether the control key is pressed.
58+{-| Whether the control key is pressed. -}
40ctrl : Signal Bool
59ctrl : Signal Bool
41ctrl = isDown 17
60ctrl = isDown 17
42
61
43−-- Whether the space key is pressed.
62+{-| Whether the space key is pressed. -}
44space : Signal Bool
63space : Signal Bool
45space = isDown 32
64space = isDown 32
46
65
47−-- Whether the enter key is pressed.
66+{-| Whether the enter key is pressed. -}
48enter : Signal Bool
67enter : Signal Bool
49enter = isDown 13
68enter = isDown 13
50
69
51−-- List of keys that are currently down.
70+{-| List of keys that are currently down. -}
52keysDown : Signal [KeyCode]
71keysDown : Signal [KeyCode]
53keysDown = Native.Keyboard.keysDown
72keysDown = Native.Keyboard.keysDown
54
73
55−-- The latest key that has been pressed.
74+{-| The latest key that has been pressed. -}
56lastPressed : Signal KeyCode
75lastPressed : Signal KeyCode
57−lastPressed = Native.Keyboard.lastPressed
76+lastPressed = Native.Keyboard.lastPressed