miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Subscription.Keyboard

Description

Overview

Miso.Subscription.Keyboard provides global keyboard subscriptions that track which keys are currently held down. All four subscriptions register keydown, keyup, and blur listeners on window; the blur handler clears the pressed-key set so keys cannot get stuck when the window loses focus.

Quick start

import Miso
import Miso.Subscription.Keyboard

-- Fire action with arrow-key state on every key change
subs :: [Sub Action]
subs = [ arrowsSub ArrowsChanged ]

update :: Action -> Effect p props Model Action
update (ArrowsChanged (Arrows x y)) = do
  -- x ∈ {-1, 0, 1}, y ∈ {-1, 0, 1}
  io_ (move x y)

Subscription variants

  • keyboardSub — delivers the raw IntSet of all currently pressed keyCodes. Use this when you need to handle arbitrary key combinations.
  • arrowsSub — maps the four arrow keys (37–40) to an Arrows value with arrowX ∈ {-1, 0, 1} and arrowY ∈ {-1, 0, 1}.
  • wasdSub — same as arrowsSub but for W/A/S/D (keyCodes 87/83/65/68).
  • directionSub — fully configurable: supply your own (up, down, left, right) keyCode lists and get the same Arrows mapping.

See also

Synopsis

Types

data Arrows Source #

Type for arrow keys currently pressed.

  • 37 left arrow ( x = -1 )
  • 38 up arrow ( y = 1 )
  • 39 right arrow ( x = 1 )
  • 40 down arrow ( y = -1 )

Constructors

Arrows 

Fields

  • arrowX :: !Int

    Horizontal direction: -1 (left), 0 (neutral), 1 (right)

  • arrowY :: !Int

    Vertical direction: -1 (down), 0 (neutral), 1 (up)

Instances

Instances details
Show Arrows Source # 
Instance details

Defined in Miso.Subscription.Keyboard

Eq Arrows Source # 
Instance details

Defined in Miso.Subscription.Keyboard

Methods

(==) :: Arrows -> Arrows -> Bool #

(/=) :: Arrows -> Arrows -> Bool #

Subscriptions

arrowsSub :: (Arrows -> action) -> Sub action Source #

Maps Arrows onto a Keyboard subscription.

directionSub Source #

Arguments

:: ([Int], [Int], [Int], [Int])

(up, down, left, right) keyCode lists for each direction

-> (Arrows -> action)

Callback fired with the current Arrows state on every key change

-> Sub action 

Maps a specified list of keys to directions (up, down, left, right). The Ints represent keyCodes for each direction.

keyboardSub :: (IntSet -> action) -> Sub action Source #

Returns Sub for keyboard events. The callback will be called with the Set of currently pressed keyCodes.

wasdSub :: (Arrows -> action) -> Sub action Source #

Maps Arrows onto a Keyboard subscription for directions (W+A+S+D keys).