2013-03-24 01:22:25 +00:00
|
|
|
module Keyboard where
|
2013-02-21 08:40:41 +00:00
|
|
|
|
2013-07-26 14:38:11 +00:00
|
|
|
import Native.Keyboard as Native
|
2013-02-22 23:19:40 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Type alias to make it clearer what integers are supposed to represent
|
|
|
|
-- in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
|
|
|
|
-- [`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
|
2013-02-22 23:19:40 +00:00
|
|
|
type KeyCode = Int
|
|
|
|
|
2013-02-21 08:40:41 +00:00
|
|
|
-- A signal of records indicating which arrow keys are pressed.
|
|
|
|
--
|
2013-05-17 20:46:08 +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>
|
2013-02-21 08:40:41 +00:00
|
|
|
-- `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
|
|
|
|
arrows : Signal { x:Int, y:Int }
|
2013-07-26 14:38:11 +00:00
|
|
|
arrows = Native.directions 38 40 37 39
|
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 }
|
2013-07-26 14:38:11 +00:00
|
|
|
wasd = Native.directions 87 83 65 68
|
2013-02-21 08:40:41 +00:00
|
|
|
|
2013-03-23 23:52:58 +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.
|
2013-03-10 09:07:17 +00:00
|
|
|
directions : KeyCode -> KeyCode -> KeyCode -> KeyCode -> Signal { x:Int, y:Int }
|
2013-07-26 14:38:11 +00:00
|
|
|
directions = Native.directions
|
2013-02-21 08:40:41 +00:00
|
|
|
|
2013-03-23 23:52:58 +00:00
|
|
|
-- Whether an arbitrary key is pressed.
|
2013-02-21 08:40:41 +00:00
|
|
|
isDown : KeyCode -> Signal Bool
|
2013-07-26 14:38:11 +00:00
|
|
|
isDown = Native.isDown
|
2013-02-21 08:40:41 +00:00
|
|
|
|
|
|
|
-- Whether the shift key is pressed.
|
|
|
|
shift : Signal Bool
|
2013-07-26 14:38:11 +00:00
|
|
|
shift = Native.isDown 16
|
2013-02-21 08:40:41 +00:00
|
|
|
|
|
|
|
-- Whether the control key is pressed.
|
|
|
|
ctrl : Signal Bool
|
2013-07-26 14:38:11 +00:00
|
|
|
ctrl = Native.isDown 17
|
2013-02-21 08:40:41 +00:00
|
|
|
|
|
|
|
-- Whether the space key is pressed.
|
|
|
|
space : Signal Bool
|
2013-07-26 14:38:11 +00:00
|
|
|
space = Native.isDown 32
|
2013-03-11 17:38:52 +00:00
|
|
|
|
2013-03-23 23:52:58 +00:00
|
|
|
-- Whether the enter key is pressed.
|
|
|
|
enter : Signal Bool
|
2013-07-26 14:38:11 +00:00
|
|
|
enter = Native.isDown 13
|
2013-03-23 23:52:58 +00:00
|
|
|
|
2013-03-24 01:22:25 +00:00
|
|
|
-- List of keys that are currently down.
|
|
|
|
keysDown : Signal [KeyCode]
|
2013-07-26 14:38:11 +00:00
|
|
|
keysDown = Native.keysDown
|
2013-03-24 01:22:25 +00:00
|
|
|
|
|
|
|
-- The latest key that has been pressed.
|
2013-05-16 20:10:50 +00:00
|
|
|
lastPressed : Signal KeyCode
|
2013-07-26 14:38:11 +00:00
|
|
|
lastPressed = Native.lastPressed
|