on c19ccfe help sign in
elk/libraries/Keyboard.elm
52 lines 1.7 kB history raw
1module Keyboard where
3import Native.Keyboard as N
5-- Type alias to make it clearer what integers are supposed to represent
6-- in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
7-- [`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
8type KeyCode = Int
10-- A signal of records indicating which arrow keys are pressed.
11--
12-- `{ x = 0, y = 0 }` when pressing no arrows.<br>
13-- `{ x =-1, y = 0 }` when pressing the left arrow.<br>
14-- `{ x = 1, y = 1 }` when pressing the up and right arrows.<br>
15-- `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
16arrows : Signal { x:Int, y:Int }
17arrows = N.directions 38 40 37 39
19-- Just like the arrows signal, but this uses keys w, a, s, and d,
20-- which are common controls for many computer games.
21wasd : Signal { x:Int, y:Int }
22wasd = N.directions 87 83 65 68
24-- Custom key directions so that you can support different locales.
25-- The plan is to have a locale independent version of this function
26-- that uses the physical location of keys, but I don't know how to do it.
27directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
29-- Whether an arbitrary key is pressed.
30isDown : KeyCode -> Signal Bool
32-- Whether the shift key is pressed.
33shift : Signal Bool
34shift = N.isDown 16
36-- Whether the control key is pressed.
37ctrl : Signal Bool
38ctrl = N.isDown 17
40-- Whether the space key is pressed.
41space : Signal Bool
42space = N.isDown 32
44-- Whether the enter key is pressed.
45enter : Signal Bool
46enter = N.isDown 13
48-- List of keys that are currently down.
49keysDown : Signal [KeyCode]
51-- The latest key that has been pressed.
52lastPressed : Signal KeyCode