Augment - keypress event

Ah - ok you’re looking at a very old post.

I’d do this differently now - without the use of native libraries.

from anvil.js.window import document

def on_keydown(event):
    print(event.key)

document.addEventListener("keydown", on_keydown)

The above code assumes that you want a global keydown event listener
You can define this code in a startup module or the top of a startup form.

You can also define this on the form itself
(but this means the form needs to have focus)

from anvil.js import get_dom_node

class Form1(Form1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    dom_node = get_dom_node(self)
    dom_node.addEventListener("keydown", self.on_keydown)

  def on_keydown(self, event):
    print(event.key)

4 Likes