on 294fdfd help sign in
elk/libraries/Keyboard.elm
52 lines 1.6 kB history raw
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.
4module Keyboard where
6import Native.Keyboard as N
8type KeyCode = Int
10-- A signal of records indicating which arrow keys are pressed.
11--
12-- `{ x = 0, y = 0 }` when pressing no arrows.
13-- `{ x =-1, y = 0 }` when pressing the left arrow.
14-- `{ x = 1, y = 1 }` when pressing the up and right arrows.
15-- `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
16arrows : Signal { x:Int, y:Int }
17arrows = N.directions 37 39 38 40
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 65 68 87 83
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 Char