2013-03-24 01:22:25 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2013-07-29 17:26:53 +00:00
|
|
|
import Signal (Signal)
|
2013-07-29 21:23:04 +00:00
|
|
|
import Native.Keyboard
|
2013-02-22 23:19:40 +00:00
|
|
|
|
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`.
|
|
|
|
-}
|
2013-02-22 23:19:40 +00:00
|
|
|
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.
|
|
|
|
-}
|
2013-07-29 21:23:04 +00:00
|
|
|
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 }
|
2013-07-29 21:23:04 +00:00
|
|
|
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 }
|
2013-07-29 21:23:04 +00:00
|
|
|
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
|
2013-07-29 21:23:04 +00:00
|
|
|
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
|
2013-07-29 21:23:04 +00:00
|
|
|
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
|
2013-07-29 21:23:04 +00:00
|
|
|
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
|
2013-07-29 21:23:04 +00:00
|
|
|
space = isDown 32
|
2013-03-11 17:38:52 +00:00
|
|
|
|
2013-09-10 13:09:21 +00:00
|
|
|
{-| Whether the enter key is pressed. -}
|
2013-03-23 23:52:58 +00:00
|
|
|
enter : Signal Bool
|
2013-07-29 21:23:04 +00:00
|
|
|
enter = isDown 13
|
2013-03-23 23:52:58 +00:00
|
|
|
|
2013-09-10 13:09:21 +00:00
|
|
|
{-| List of keys that are currently down. -}
|
2013-03-24 01:22:25 +00:00
|
|
|
keysDown : Signal [KeyCode]
|
2013-07-29 21:23:04 +00:00
|
|
|
keysDown = Native.Keyboard.keysDown
|
2013-03-24 01:22:25 +00:00
|
|
|
|
2013-09-10 13:09:21 +00:00
|
|
|
{-| The latest key that has been pressed. -}
|
2013-05-16 20:10:50 +00:00
|
|
|
lastPressed : Signal KeyCode
|
2013-09-10 13:09:21 +00:00
|
|
|
lastPressed = Native.Keyboard.lastPressed
|