Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/scottcorgan/keyboard-combo
Handle keyboard key combinations with type safety in Elm
https://github.com/scottcorgan/keyboard-combo
elm keyboard keyboard-combo shortcut
Last synced: about 2 months ago
JSON representation
Handle keyboard key combinations with type safety in Elm
- Host: GitHub
- URL: https://github.com/scottcorgan/keyboard-combo
- Owner: scottcorgan
- License: bsd-3-clause
- Created: 2016-10-27T15:19:44.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-31T09:55:47.000Z (almost 6 years ago)
- Last Synced: 2024-05-09T13:36:56.862Z (8 months ago)
- Topics: elm, keyboard, keyboard-combo, shortcut
- Language: Elm
- Homepage: http://package.elm-lang.org/packages/scottcorgan/keyboard-combo/latest
- Size: 25.4 KB
- Stars: 15
- Watchers: 4
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Keyboard Combo
Handle keyboard key combinations with type safety in Elm
[Check out the demo](https://embed.ellie-app.com/n3mVWz5gzJ/0)
**Note:** Not every key is implemented yet. Please open a PR if one is missing.
## Usage
Usage example taken from [Example in _examples_ directory](https://github.com/scottcorgan/keyboard-combo/tree/master/examples)
```elm
module Main exposing (..)import Html exposing (..)
import Keyboard.Combomain : Program Never Model Msg
main =
Html.program
{ subscriptions = subscriptions
, init = init
, update = update
, view = view
}keyboardCombos : List (Keyboard.Combo.KeyCombo Msg)
keyboardCombos =
[ Keyboard.Combo.combo2 ( Keyboard.Combo.control, Keyboard.Combo.s ) Save
, Keyboard.Combo.combo2 ( Keyboard.Combo.control, Keyboard.Combo.a ) SelectAll
, Keyboard.Combo.combo3 ( Keyboard.Combo.control, Keyboard.Combo.alt, Keyboard.Combo.e ) RandomThing
]-- Init
init : ( Model, Cmd Msg )
init =
{ keys = Keyboard.Combo.init keyboardCombos ComboMsg
, content = "No combo yet"
}
! []subscriptions : Model -> Sub Msg
subscriptions model =
Keyboard.Combo.subscriptions model.keys-- Model
type alias Model =
{ keys : Keyboard.Combo.Model Msg
, content : String
}-- Update
type Msg
= Save
| SelectAll
| RandomThing
| ComboMsg Keyboard.Combo.Msgupdate : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Save ->
{ model | content = "Saved" } ! []SelectAll ->
{ model | content = "Select All" } ! []RandomThing ->
{ model | content = "Random Thing" } ! []ComboMsg msg ->
let
( keys, cmd ) =
Keyboard.Combo.update msg model.keys
in
( { model | keys = keys }, cmd )-- View
view : Model -> Html Msg
view model =
div []
[ h1 [] [ text "Available Key Commands:" ]
, ul []
[ li [] [ text "Save: Ctrl+s" ]
, li [] [ text "Select All: Ctrl+a" ]
, li [] [ text "Random Thing: Ctrl+Alt+e" ]
]
, div []
[ strong [] [ text "Current command: " ]
, span [] [ text model.content ]
]
]
```