elm/libraries/Keyboard.elm

78 lines
2 KiB
Elm
Raw Normal View History

module Keyboard where
2013-02-21 08:40:41 +00:00
2013-09-10 13:09:21 +00:00
{-| Library for working with keyboard input.
# Representing Keys
@docs KeyCode
# Directions
@docs arrows, wasd, directions
# Specific Keys
@docs shift, enter, space, ctrl
# General Keypresses
@docs isDown, keysDown, lastPressed
-}
import Signal (Signal)
import Native.Keyboard
2013-09-10 13:09:21 +00:00
{-| Type alias to make it clearer what integers are supposed to represent
2013-09-11 01:21:30 +00:00
in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
[`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
Use the uppercase character with `toCode`.
-}
type KeyCode = Int
2013-09-10 13:09:21 +00:00
{-| Custom key directions to support different locales. The order is up, down,
2013-09-11 01:21:30 +00:00
left, right.
-}
directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
directions = Native.Keyboard.directions
2013-09-10 13:09:21 +00:00
{-| A signal of records indicating which arrow keys are pressed.
2013-09-11 01:21:30 +00:00
`{ x = 0, y = 0 }` when pressing no arrows.<br>
`{ x =-1, y = 0 }` when pressing the left arrow.<br>
`{ x = 1, y = 1 }` when pressing the up and right arrows.<br>
`{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
2013-09-10 13:09:21 +00:00
-}
2013-02-21 08:40:41 +00:00
arrows : Signal { x:Int, y:Int }
arrows = directions 38 40 37 39
2013-02-21 08:40:41 +00:00
2013-09-10 13:09:21 +00:00
{-| Just like the arrows signal, but this uses keys w, a, s, and d,
2013-09-11 01:21:30 +00:00
which are common controls for many computer games.
-}
2013-02-21 08:40:41 +00:00
wasd : Signal { x:Int, y:Int }
wasd = directions 87 83 65 68
2013-02-21 08:40:41 +00:00
2013-09-10 13:09:21 +00:00
{-| Whether an arbitrary key is pressed. -}
2013-02-21 08:40:41 +00:00
isDown : KeyCode -> Signal Bool
isDown = Native.Keyboard.isDown
2013-02-21 08:40:41 +00:00
2013-09-10 13:09:21 +00:00
{-| Whether the shift key is pressed. -}
2013-02-21 08:40:41 +00:00
shift : Signal Bool
shift = isDown 16
2013-02-21 08:40:41 +00:00
2013-09-10 13:09:21 +00:00
{-| Whether the control key is pressed. -}
2013-02-21 08:40:41 +00:00
ctrl : Signal Bool
ctrl = isDown 17
2013-02-21 08:40:41 +00:00
2013-09-10 13:09:21 +00:00
{-| Whether the space key is pressed. -}
2013-02-21 08:40:41 +00:00
space : Signal Bool
space = isDown 32
2013-09-10 13:09:21 +00:00
{-| Whether the enter key is pressed. -}
enter : Signal Bool
enter = isDown 13
2013-09-10 13:09:21 +00:00
{-| List of keys that are currently down. -}
keysDown : Signal [KeyCode]
keysDown = Native.Keyboard.keysDown
2013-09-10 13:09:21 +00:00
{-| The latest key that has been pressed. -}
lastPressed : Signal KeyCode
2013-09-10 13:09:21 +00:00
lastPressed = Native.Keyboard.lastPressed