Augment - keypress event

I tried using keypress event in my project but it doesn’t seem to work on blank form and I get this error

1 Like

Hi @kvanitha780,

I’ve moved your question to a new thread.
Do you have a clone you could share that demonstrates the issue?

For anvil-extras queries it’s usually best to ask on the anvil-extras github page:

1 Like

Due to privacy reasons, I’m not allowed to show the project :sweat_smile:

Sharing a project is often too much.
It’s usually best to create a small app that only demonstrates the issue you’re facing.
This makes it much easier for others to look at the code and make suggestions.

I tried doing that but It works in the small app , not in the project : (

https://anvil.works/build#clone:Y6OZKEPYQE32VBZZ=2PWYFZAWOKIZD7RMLHF5JTGV

I tried the keypress event in material design where it works fine but in Custom HTML it doesn’t. Please have a look @stucork

@kvanitha780 - the clone doesn’t really seem to demonstrate anything?
Is it the right clone?
All I see is a button click handler and a keypress method.
But there are no dependencies and the keypress method isn’t hooked up to anything.

my bad @stucork, I’m not good at coding. I was trying to do something like the app you made(link down below). The keypress event works fine in Material Design(with navbar and sidebar) but not in Custom HTML. I was thinking to add this button in my main project as a custom component, because of the error I mentioned above, where on pressing a button in keyboard it will open another form and can you please guide me how to hook the keypress event?

https://anvil.works/build#clone:CUK6QTBXGX6ARR4T=BGUXMPH3FDHMMAOZIFJQK637

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

I like this solution, but started running into problems when the form was loaded and unloaded.
It would trigger the event as many times as it was reloading without clearing it.

@arkulinskis best to start a new thread linking to this one with a full description of the issue your facing.

A clone link with a minimal working example will be helpful to give pointers.