TheSharperDev

Educating about C# and F#

Keyup Event Listener - Fable/F#

Fable brings F# to the browser. I wanted to document how I created a keyup event listener.

Event Listener

I was playing around with F# and had to google a bit to get this to work. Documenting this for posterity’s sake.

textInput.addEventListener("keyup", fun (event: Browser.Types.Event) -> 
    //regular event doesn't have `key`, so a cast is required
    let keyEvent = event :?> Browser.Types.KeyboardEvent;

    //match against a particular key
    if keyEvent.key = "Enter" 
    then doSomethingWithEnter()

)

I am sure there are other ways of doing this, maybe I’ll update this post if I find any. No promises.