Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm: signal for two keyboard keys together?

Tags:

elm

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.

like image 702
The Oddler Avatar asked Nov 20 '15 17:11

The Oddler


2 Answers

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.

like image 200
The Oddler Avatar answered Nov 14 '22 21:11

The Oddler


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')
like image 26
Dylan Mccomas Avatar answered Nov 14 '22 21:11

Dylan Mccomas