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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!