@@ -1,57 +1,76 @@
11module Keyboard where
22
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+
319import Signal (Signal)
420import Native.Keyboard
521
6−-- Type alias to make it clearer what integers are supposed to represent
7−-- in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
8−-- [`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
22+{-| Type alias to make it clearer what integers are supposed to represent
23+ in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
24+ [`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
25+ Use the uppercase character with `toCode`. -}
926type KeyCode = Int
1027
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. -}
1230-- The plan is to have a locale independent version of this function
1331-- that uses the physical location of keys, but I don't know how to do it.
1432directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
1533directions = Native.Keyboard.directions
1634
17−-- A signal of records indicating which arrow keys are pressed.
18−--
19−-- `{ x = 0, y = 0 }` when pressing no arrows.<br>
20−-- `{ x =-1, y = 0 }` when pressing the left arrow.<br>
21−-- `{ 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.
35+{-| A signal of records indicating which arrow keys are pressed.
36+
37+ `{ x = 0, y = 0 }` when pressing no arrows.<br>
38+ `{ x =-1, y = 0 }` when pressing the left arrow.<br>
39+ `{ x = 1, y = 1 }` when pressing the up and right arrows.<br>
40+ `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
41+-}
2342arrows : Signal { x:Int, y:Int }
2443arrows = directions 38 40 37 39
2544
26−-- Just like the arrows signal, but this uses keys w, a, s, and d,
27−-- which are common controls for many computer games.
45+{-| Just like the arrows signal, but this uses keys w, a, s, and d,
46+which are common controls for many computer games. -}
2847wasd : Signal { x:Int, y:Int }
2948wasd = directions 87 83 65 68
3049
31−-- Whether an arbitrary key is pressed.
50+{-| Whether an arbitrary key is pressed. -}
3251isDown : KeyCode -> Signal Bool
3352isDown = Native.Keyboard.isDown
3453
35−-- Whether the shift key is pressed.
54+{-| Whether the shift key is pressed. -}
3655shift : Signal Bool
3756shift = isDown 16
3857
39−-- Whether the control key is pressed.
58+{-| Whether the control key is pressed. -}
4059ctrl : Signal Bool
4160ctrl = isDown 17
4261
43−-- Whether the space key is pressed.
62+{-| Whether the space key is pressed. -}
4463space : Signal Bool
4564space = isDown 32
4665
47−-- Whether the enter key is pressed.
66+{-| Whether the enter key is pressed. -}
4867enter : Signal Bool
4968enter = isDown 13
5069
51−-- List of keys that are currently down.
70+{-| List of keys that are currently down. -}
5271keysDown : Signal [KeyCode]
5372keysDown = Native.Keyboard.keysDown
5473
55−-- The latest key that has been pressed.
74+{-| The latest key that has been pressed. -}
5675lastPressed : Signal KeyCode
57−lastPressed = Native.Keyboard.lastPressed
76+lastPressed = Native.Keyboard.lastPressed