The Oddler
The Oddler

Reputation: 6708

Elm: signal for two keyboard keys together?

How would one create a signal for alt+o or any other pair of keys in Elm?

Is there a built-in way for doing this, or do I have to create something myself?

I'm very new to Elm, so any additional explanation is always welcome.

Upvotes: 5

Views: 304

Answers (2)

The Oddler
The Oddler

Reputation: 6708

I figured it out myself:

Signal.map2 (&&) Keyboard.alt (Keyboard.isDown <| Char.toCode 'O')

This creates a single Signal Bool that's true when both are down, otherwise false.

Upvotes: 5

Dylan Mccomas
Dylan Mccomas

Reputation: 29

Yes there is a built-in way in elm to handle keyboard inputs

The module is keyboard.elm

From my understanding to be able to use this you have to

import keyboard
import Signal exposing ((<~))

The keysDown function creates a signal which informs what keys are currently being pressed

import Keyboard
import Signal exposing ((<~))
import Graphics.Element exposing (show)


main = show <~ Keyboard.keysDown

The isDown function takes a key code as its argument and returns a boolean signal indicating whether the given key is currently being pressed. There are also helper functions defined in terms of isDown for certain special keys: shift, ctrl, space and enter.

import Char
import Graphics.Element exposing (show)
import Keyboard
import Signal exposing ((<~))


main = show <~ Keyboard.isDown (Char.toCode 'A')

Upvotes: 2

Related Questions