Skip to content

Is it possible to detect a key press event ? #552

Closed Answered by wwwillchen
colinrees asked this question in Q&A
Discussion options

You must be logged in to vote

I'd recommend creating a web component: https://google.github.io/mesop/web_components/ - then you can detect a keypress event by writing JS code, e.g.

document.addEventListener('keydown', function(event) {
  const keyName = event.key;

  // Check if the pressed key is "a"
  if (keyName === 'a') {
    console.log("You pressed the 'a' key!"); 
  }

  // Example: Detect the Enter key
  if (keyName === 'Enter') {
    console.log("You pressed Enter!");
  }
});

You can trigger an event from your web component which you can use to call a Python function (e.g. the on_click event handler or an extracted function). We've considered supporting keyboard shortcuts in a more first-class way in Mesop, …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by wwwillchen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants