2

这是该主题的后续问题:

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

我正在添加一个允许玩家自定义控件的功能。但是箭头键代表自己的方式真的很奇怪。稍后我会谈到这一点。我所拥有的看起来像这样:

ck2up : Input [KeyCode]
ck2up = input [toCode 'W']

ck2down : Input [KeyCode]
ck2down = input [toCode 'S']

ck2left : Input [KeyCode]
ck2left = input [toCode 'A']

ck2right : Input [KeyCode]
ck2right = input [toCode 'D']

Keyboard.keysDown 我单击按钮时,它们会接收输入。这对于普通的 Char 输入很有效,但是当我想使用符号时,我会得到一些奇怪的东西。我想默认给一个玩家方向键,但由于它必须自定义,我不能使用Keyboard.arrows. 玩家一将拥有:

ck1up : Input [KeyCode]
ck1up = input [8593]

ck1down : Input [KeyCode]
ck1down = input [8595]

ck1left : Input [KeyCode]
ck1left = input [8592]

ck1right : Input [KeyCode]
ck1right = input [8594]

这取自http://character-code.com/arrows-html-codes.php。这些是箭头键的 html 代码。我在按钮上显示当前键,并且箭头正确显示:

(button ck1up.handle kd (append "up: " (fromList [fromCode pkeys1.up])))

包含pkeys1.up密钥代码。但是,在游戏中箭头键没有响应。这是可以预料的,因为 html 是为布局而制作的,但是当我尝试使用 unicode 时,如http://library.elm-lang.org/catalog/elm-lang-Elm/0.13/Char toCodefromCode所述,键ingame 仍然无法工作,并且我得到一个错误图标,我希望按钮上有箭头符号。这可能是因为 unicode 是十六进制的。

但!我可以使用我新获得的功能来自定义控件,方法是在按下按钮的同时按住键。这确实使我的箭头键起作用,但它显示了以下符号:

up: &
down: (
left: %
right: '

这意味着使我的箭头键起作用的相同 unicode 正在显示这些随机符号。这不是很奇怪吗?如果有人知道我在哪里可以找到 Elm 使用的 unicode 值,那就太好了。如果没有输入任何键,我想将箭头键设置为默认值。是否有任何东西可以将这些 un​​icode 转换为 html,以便正确显示?

万一这很重要,我仍在使用 Elm 版本 13。

更新

所以我弄清楚了 KeyCodes 38、40、37 和 39 分别对应于箭头字符(并且显示符号对应于这些相同的值,但在 html 中)。

4

1 回答 1

2

As you've already figured out, key codes 37-40 are the ones you need. These can also be found in the source code of Keyboard.arrows.

KeyCode is not just some random name but alludes to JavaScripts keycodes being used. I cannot find the definition with a quick search in the ECMAScript docs and other internet sources suggest that browsers were not always compatible in their use of keycodes. So this is your standard web-programming mess that's getting slowly easier to deal with. That this bleeds through into Elm is arguably bad design though, best continue this discussion on the mailing list.

于 2014-12-17T20:16:50.883 回答