Keyboard 创建自定义键盘控件[Elm]

Keyboard 创建自定义键盘控件[Elm],keyboard,signals,elm,Keyboard,Signals,Elm,我正在尝试为4人游戏创建自定义键盘控件。现在,按键是这样预先确定的: type Orient = { x:Int, y:Int } type GameInput = { space:Bool, delta:Time, so1:Orient, so2:Orient, so3:Orient, so4:Orient, amount:Int } gameInput : Signal GameInput gameInput = let

我正在尝试为4人游戏创建自定义键盘控件。现在,按键是这样预先确定的:

type Orient = { x:Int, y:Int }
type GameInput = { space:Bool, delta:Time, so1:Orient, so2:Orient, so3:Orient, 
                   so4:Orient, amount:Int }


gameInput : Signal GameInput
gameInput = 
             let sampledInput = sampleOn delta 
                                <| GameInput <~ Keyboard.space
                                              ~ delta
                                              ~ Keyboard.arrows
                                              ~ Keyboard.wasd
                                              ~ Keyboard.directions (toCode 'O')
                                                                    (toCode 'L')
                                                                    (toCode 'K')
                                                                    (toCode 'M')
                                              ~ Keyboard.directions (toCode 'Y')
                                                                    (toCode 'H')
                                                                    (toCode 'G')
                                                                    (toCode 'J')
                                              ~ amountPlayers.signal
             in  lift (Debug.watch "input") sampledInput
任何结果都将成为一个信号的信号,而该信号不能被提升到
GameInput
(正确)上

我已尝试将字符作为参数传递到
gameInput

gameInput2 : Signal GameInput
gameInput2 = gameInput <~ customKeys2

gameInput : CustomKeys -> Signal GameInput
gameInput { up,down,left,right } = GameInput <~ Keyboard.directions (toCode up)
                                                                    (toCode down)
                                                                    (toCode left)
                                                                    (toCode right)
gameInput2:信号GameInput
gameInput2=gameInput信号gameInput

gameInput{up,down,left,right}=gameInput直接使用
键盘。方向
不起作用。问题是,在游戏开始时,可以更改按键,这样您就有了一些
信号字符。方向从“普通类型”变为“信号类型”。所以你举不起来

但您也可以访问当前按下的键,
Keyboard.keysDown
。我查找了,我认为您可以在Elm中重新创建它,使它采用
Signal Int
参数

以下是普通
键盘的Elm实现。说明

directions : Int -> Int -> Int -> Int -> Signal { x : Int, y : Int }
directions up down left right = 
  (\kd -> 
    List.filter (\ky -> List.member ky [up,down,left,right]) kd |>
      List.foldl (\ky st -> if | ky == up    -> { st | y <- st.y + 1 }
                               | ky == down  -> { st | y <- st.y - 1 }
                               | ky == left  -> { st | x <- st.x - 1 }
                               | ky == right -> { st | x <- st.x + 1 }
      ) {x=0,y=0}
  ) <~ Keyboard.keysDown
方向:Int->Int->Int->Int->Int->Signal{x:Int,y:Int}
上下左右方向=
(\kd->
List.filter(\ky->List.member ky[上、下、左、右])kd>
List.foldl(\ky st->if | ky==up->{st | y{st | y{st | x{st | x信号Int->Signal Int->Signal{x:Int,y:Int}
上下左右方向=
(\u d l r kd->
List.filter(\ky->List.member ky[u,d,l,r])kd>
List.foldl(\ky st->if | ky==u->{st | y{st | y{st | x{st | x
gameInput2 : Signal GameInput
gameInput2 = gameInput <~ customKeys2

gameInput : CustomKeys -> Signal GameInput
gameInput { up,down,left,right } = GameInput <~ Keyboard.directions (toCode up)
                                                                    (toCode down)
                                                                    (toCode left)
                                                                    (toCode right)
directions : Int -> Int -> Int -> Int -> Signal { x : Int, y : Int }
directions up down left right = 
  (\kd -> 
    List.filter (\ky -> List.member ky [up,down,left,right]) kd |>
      List.foldl (\ky st -> if | ky == up    -> { st | y <- st.y + 1 }
                               | ky == down  -> { st | y <- st.y - 1 }
                               | ky == left  -> { st | x <- st.x - 1 }
                               | ky == right -> { st | x <- st.x + 1 }
      ) {x=0,y=0}
  ) <~ Keyboard.keysDown
directions : Signal Int -> Signal Int -> Signal Int -> Signal Int -> Signal { x : Int, y : Int }
directions up down left right = 
  (\u d l r kd -> 
    List.filter (\ky -> List.member ky [u,d,l,r]) kd |>
      List.foldl (\ky st -> if | ky == u -> { st | y <- st.y + 1 }
                               | ky == d -> { st | y <- st.y - 1 }
                               | ky == l -> { st | x <- st.x - 1 }
                               | ky == r -> { st | x <- st.x + 1 }
      ) {x=0,y=0}
  ) <~ up ~ down ~ left ~ right ~ Keyboard.keysDown