z0w0 / helm

A functionally reactive game engine, with headgear to protect you from the headache of game development provided.
http://helm-engine.org/
MIT License
600 stars 69 forks source link

Get the character from the key #129

Open Ulrar opened 6 years ago

Ulrar commented 6 years ago

Hi,

I don't see any way to convert from Keyboard.PKey for example to the actual character P, to know what the player is typing. How would I do that, besides the obvious twenty-six case on the key ?

Thanks

AlexaDeWit commented 6 years ago

I'm not sure there is currently another way at present in helm. Helm doesn't implement subscriptions to or support for manipulating of a text input field.

Here's how one can create them in haskell. https://hackage.haskell.org/package/sdl2-2.4.0.1/docs/SDL-Input-Keyboard.html#g:2

Under text input/editing events here you can see how data gets fed back from sdl2. https://hackage.haskell.org/package/sdl2-2.4.0.1/docs/SDL-Event.html#g:6

A helm-friendly abstraction around this would be needed to do it the "easy way".

Might be possible to do in helm via Cmd but I can't really say for sure yet as I'm still learning.

Ulrar commented 6 years ago

Hey,

I think you might have misunderstood my question, I was just looking for a function to get the char from a key pressed event, not any kind of pre-made text field.

I've since made it myself (I'll skip the handling of shift for uppercase letters, but you get it) :

keyCodeToChar k = case k of
  KB.AKey       -> "a"
  KB.BKey       -> "b"
  KB.CKey       -> "c"
  KB.DKey       -> "d"
  KB.EKey       -> "e"
  KB.FKey       -> "f"
  KB.GKey       -> "g"
  KB.HKey       -> "h"
  KB.IKey       -> "i"
  KB.JKey       -> "j"
  KB.KKey       -> "k"
  KB.LKey       -> "l"
  KB.MKey       -> "m"
  KB.NKey       -> "n"
  KB.OKey       -> "o"
  KB.PKey       -> "p"
  KB.QKey       -> "q"
  KB.RKey       -> "r"
  KB.SKey       -> "s"
  KB.TKey       -> "t"
  KB.UKey       -> "u"
  KB.VKey       -> "v"
  KB.WKey       -> "w"
  KB.XKey       -> "x"
  KB.YKey       -> "y"
  KB.ZKey       -> "z"
  KB.Number0Key -> "0"
  KB.Number1Key -> "1"
  KB.Number2Key -> "2"
  KB.Number3Key -> "3"
  KB.Number4Key -> "4"
  KB.Number5Key -> "5"
  KB.Number6Key -> "6"
  KB.Number7Key -> "7"
  KB.Number8Key -> "8"
  KB.Number9Key -> "9"
  KB.SpaceKey   -> " "
  KB.MinusKey   -> "-"
  _             -> ""

Nothing fancy, but it seems like that doesn't exist anywhere in helm unfortunatly.

AlexaDeWit commented 6 years ago

The reason I went to elaborate on this is perhaps a bit more specific to those of us in Euroland, where we type weird multi-key series combinations for typing text.

é requires for instance two keypresses. I figured you wanted full blown text-entry, which is a different sdl2 module with no current helm support :(

Ulrar commented 6 years ago

I'm French so I'm well aware, but yeah that wasn't what I needed (I just wanted to have simple English text input, which is fairly easy to handle by hand). Although full blown utf8 support would be a nice feature, for sure !

Le sam. 19 mai 2018 21:18, Alexa DeWit notifications@github.com a écrit :

The reason I went to elaborate on this is perhaps a bit more specific to those of us in Euroland, where we type weird multi-key series combinations for typing text.

é requires for instance two keypresses. I figured you wanted full blown text-entry, which is a different sdl2 module with no current helm support :(

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/z0w0/helm/issues/129#issuecomment-390430011, or mute the thread https://github.com/notifications/unsubscribe-auth/AA1Tz9ewmhQW_N0eKDIKSB-CAY9DCxo3ks5t0H4agaJpZM4RPMaN .

geezee commented 6 years ago

Hey! I just submitted a pull request that might solve this issue, I pretty much ended up adding a subscription to SDL.Event.TextInputEvent. Here's the pull request