Elm: signal for two keyboard keys together?

三世轮回 提交于 2019-12-05 00:16:25

问题


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.


回答1:


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.




回答2:


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')


来源:https://stackoverflow.com/questions/33832720/elm-signal-for-two-keyboard-keys-together

标签

工具导航Map