Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onkeydown event not working #36

Open
aryx opened this issue Nov 3, 2020 · 6 comments
Open

onkeydown event not working #36

aryx opened this issue Nov 3, 2020 · 6 comments

Comments

@aryx
Copy link

aryx commented Nov 3, 2020

For context, I try to port the elm-playground library to OCaml using ocaml-vdom. I manage to get a few examples working
but I am currently stuck with the way the keyboard is handled in ocaml-vdom vs Elm.

I would like to generate a msg `Keydown each time the user press a key anyway in the page, but it does not work.
Here is the smallest program showing the problem:

open Vdom

type model = string

let update _ = function
  | `Click -> 
      "Click:"
  | `Keydown s -> 
      "Keydown:" ^ s

let init = "EMPTY"

let view s =
  div
    ~a:[
    onkeydown (fun evt -> `Keydown (Printf.sprintf "%d" evt.which));
    autofocus;
  ]
    [
      div [text (Printf.sprintf "Content: %s" s)];
      div [input [] ~a:[onclick (fun _ -> `Click); type_button; value "Update"]]
    ]

let app = simple_app ~init ~view ~update ()


open Js_browser

let run () = Vdom_blit.run app |> Vdom_blit.dom |> Element.append_child (Document.body document)
let () = Window.set_onload window run
@aryx
Copy link
Author

aryx commented Nov 3, 2020

If I type a key in the page, nothing changes.
However, if I click on the button, and then I type a key anywhere, then the event gets triggered.

@aryx
Copy link
Author

aryx commented Nov 3, 2020

Note that Elm provides not only an event for keydown but also for keyup (which are useful to implememt games with elm-playground).

@aryx
Copy link
Author

aryx commented Dec 21, 2020

Any help? Is this project dead? Should I switch to another OCaml library (e.g., Brr by daniel buenzli?)

@alainfrisch
Copy link
Contributor

If no element under the div has the focus, the KeyDown event won't trigger. You might want to use a Custom elements to attach a global handler.

@aryx
Copy link
Author

aryx commented Dec 28, 2020

Is there any code doing that I could imitate? I'm not very familiar with those custom elements.

@aryx
Copy link
Author

aryx commented Oct 25, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants