Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Support emitting key on KeyboardEvent #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/FRP/Event/Keyboard.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import FRP.Event (Event, makeEvent, subscribe)
import Web.Event.EventTarget (addEventListener, eventListener, removeEventListener)
import Web.HTML (window)
import Web.HTML.Window (toEventTarget)
import Web.UIEvent.KeyboardEvent (code, fromEvent)
import Web.UIEvent.KeyboardEvent (code, fromEvent, key)

-- | A handle for creating events from the keyboard.
newtype Keyboard = Keyboard
{ keys :: Ref.Ref (Set.Set String)
, dispose :: Effect Unit
}

-- | Get a handle for working with the keyboard.
getKeyboard :: Effect Keyboard
getKeyboard = do
Expand All @@ -48,22 +48,22 @@ disposeKeyboard :: Keyboard -> Effect Unit
disposeKeyboard (Keyboard { dispose }) = dispose

-- | Create an `Event` which fires when a key is pressed
down :: Event String
down :: Event { key :: String, code :: String }
down = makeEvent \k -> do
target <- toEventTarget <$> window
keyDownListener <- eventListener \e -> do
fromEvent e # traverse_ \ke ->
k (code ke)
k { key: key ke, code: code ke }
addEventListener (wrap "keydown") keyDownListener false target
pure (removeEventListener (wrap "keydown") keyDownListener false target)

-- | Create an `Event` which fires when a key is released
up :: Event String
up :: Event { key :: String, code :: String }
up = makeEvent \k -> do
target <- toEventTarget <$> window
keyUpListener <- eventListener \e -> do
fromEvent e # traverse_ \ke ->
k (code ke)
k { key: key ke, code: code ke }
addEventListener (wrap "keyup") keyUpListener false target
pure (removeEventListener (wrap "keyup") keyUpListener false target)

Expand Down