PricingBlog

Not having success with Keydown and Keyup events. Could use some help.

  • burner918-1258154939398754304

    Sean

    1 year ago

    I've not been having any success with the keydown and keyup events and could use some help. Basically, my setup involves a modal created such that I'm using the top div of the modal as an overlay (absolute and 100vh, 100vw) and then the div within this has the modal background (canvas). I'd like it so that whenever this modal is visible, the user could press the Esc key to hide the modal. I've tried setting keydown and keyup events to the modal (the top element overlay) but those neither of those events aren't detected (I've logged to the console and see nothing). Could I be setting the keydown event on the wrong element? FYI, my modal isn't a component -- it's just a transparent div within the main structure that has absolute position and 100vh, 100vw and within this div is my modal background.
  • lucasg-1258156584589066280

    Lucas G

    1 year ago

    Key press events can be tricky as you might have to manage focus states and if it's an input element, etc.
  • One way to go about it that I found simple to implement was to go with a custom action
  • function listenForKeyPress() {
    document.addEventListener('keypress', function(event) {
    const keyValue = event.key;
    ctx.triggerActionEvent('keyValue', keyValue);
    });
    }
  • 1258156793763463250-image.png
  • Run the action onLoad and use a switch condition to run your logic
  • burner918-1258157776547483648

    Sean

    1 year ago

    Thank you, Lucas. I'll give this a shot.
  • burner918-1258158357156462612

    Sean

    1 year ago

    Quick question. What is ctx? Where is that object defined? Is that an in-built Toddle function?
  • lucasg-1258163097152262145

    Lucas G

    1 year ago

    ctx = context
  • toddle custom actions use args and ctx objects to bring data into and out of the function
  • triggerActionEvent is a function to pass the data into toddle
  • burner918-1258169507097088020

    Sean

    1 year ago

    Thank you. Also, you hinted at there being another way that requires focus and state change. How would that approach work? If I can get it to work natively without writing custom code then that would be ideal so was curious to know what the native Toddle way for this would be
  • lucasg-1258173013988343979

    Lucas G

    1 year ago

    Sure
  • For a div to be able to listen for keydown events, it needs to be in focus
  • Here's an example
  • When the div is in focus, the background turns green and it shows the key pressed
  • Otherwise it is grey and it doesn't record key values
  • lucasg-1258174120420704328

    Lucas G

    1 year ago

    They custom action route will allow you to listen for key presses regardless of whether or not the div is in focus
  • Which is handy for many reasons lol
  • burner918-1258174591432654959

    Sean

    1 year ago

    Aha. This is very helpful. Thanks so much Lucas.