on 63243ca help sign in
elk/libraries/Keyboard.elm
77 lines 2.0 kB history raw
1module Keyboard where
3{-| Library for working with keyboard input.
5# Representing Keys
6@docs KeyCode
8# Directions
9@docs arrows, wasd, directions
11# Specific Keys
12@docs shift, enter, space, ctrl
14# General Keypresses
15@docs isDown, keysDown, lastPressed
17-}
19import Signal (Signal)
20import Native.Keyboard
22{-| Type alias to make it clearer what integers are supposed to represent
23in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
24[`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
25Use the uppercase character with `toCode`.
26-}
27type KeyCode = Int
29{-| Custom key directions to support different locales. The order is up, down,
30left, right.
31-}
32directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
33directions = Native.Keyboard.directions
35{-| A signal of records indicating which arrow keys are pressed.
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-}
42arrows : Signal { x:Int, y:Int }
43arrows = directions 38 40 37 39
45{-| Just like the arrows signal, but this uses keys w, a, s, and d,
46which are common controls for many computer games.
47-}
48wasd : Signal { x:Int, y:Int }
49wasd = directions 87 83 65 68
51{-| Whether an arbitrary key is pressed. -}
52isDown : KeyCode -> Signal Bool
53isDown = Native.Keyboard.isDown
55{-| Whether the shift key is pressed. -}
56shift : Signal Bool
57shift = isDown 16
59{-| Whether the control key is pressed. -}
60ctrl : Signal Bool
61ctrl = isDown 17
63{-| Whether the space key is pressed. -}
64space : Signal Bool
65space = isDown 32
67{-| Whether the enter key is pressed. -}
68enter : Signal Bool
69enter = isDown 13
71{-| List of keys that are currently down. -}
72keysDown : Signal [KeyCode]
73keysDown = Native.Keyboard.keysDown
75{-| The latest key that has been pressed. -}
76lastPressed : Signal KeyCode
77lastPressed = Native.Keyboard.lastPressed