elm/libraries/Keyboard.elm

53 lines
1.6 KiB
Elm
Raw Normal View History

2013-02-21 08:40:41 +00:00
-- These are nicely curated inputs from the keyboard. See the
-- [Keyboard.Raw library](/docs/Signal/KeyboardRaw.elm) for a
-- lower-level interface that will let you define more complicated behavior.
module Keyboard where
2013-02-21 08:40:41 +00:00
import Native.Keyboard as N
type KeyCode = Int
2013-02-21 08:40:41 +00:00
-- A signal of records indicating which arrow keys are pressed.
--
-- `{ x = 0, y = 0 }` when pressing no arrows.
-- `{ x =-1, y = 0 }` when pressing the left arrow.
-- `{ x = 1, y = 1 }` when pressing the up and right arrows.
-- `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
arrows : Signal { x:Int, y:Int }
arrows = N.directions 37 39 38 40
2013-02-21 08:40:41 +00:00
-- Just like the arrows signal, but this uses keys w, a, s, and d,
-- which are common controls for many computer games.
wasd : Signal { x:Int, y:Int }
wasd = N.directions 65 68 87 83
2013-02-21 08:40:41 +00:00
-- Custom key directions so that you can support different locales.
-- The plan is to have a locale independent version of this function
-- that uses the physical location of keys, but I don't know how to do it.
directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
2013-02-21 08:40:41 +00:00
-- Whether an arbitrary key is pressed.
2013-02-21 08:40:41 +00:00
isDown : KeyCode -> Signal Bool
-- Whether the shift key is pressed.
shift : Signal Bool
shift = N.isDown 16
2013-02-21 08:40:41 +00:00
-- Whether the control key is pressed.
ctrl : Signal Bool
ctrl = N.isDown 17
2013-02-21 08:40:41 +00:00
-- Whether the space key is pressed.
space : Signal Bool
space = N.isDown 32
-- Whether the enter key is pressed.
enter : Signal Bool
enter = N.isDown 13
-- List of keys that are currently down.
keysDown : Signal [KeyCode]
-- The latest key that has been pressed.
lastPressed : Signal Char