Anvil Community Forum

Application Keyboard Shortcuts

Writers block again, so here’s a little library to add to your applications.

It adds the Mousetrap keyboard trapper (found here : https://github.com/ccampbell/mousetrap)
and will send key combinations back to your Anvil application to be processed in Python. You will need a paid account as it uses custom html features.

https://anvil.works/ide#clone:ZKSAUGBAXG5YHLHN=MGKGSFKNHKSQ4HIAPBGBTX4L

WARNING - I have no idea how nicely this will play with the Anvil runtime. I merely had a need for it and I was stuck on my main project, so do be wary!

1 Like

Nice. I love keyboard shortcuts.

Hi David - just stumbled on this 2.5 years later while trying to add keyboard shortcuts to my app… For the benefit of others landing here, and not to denigrate your mousetrap solution (which I’m happy to say still works even today!), here’s an alternative solution that also includes Hover and Focus:

As a third option, I came up with this quick and dirty approach based on creating an ‘invisible’ TextBox hidden in your form layout. Several big limitations, but if you want to return basic keyboard input with a simple copy/paste of pure Anvil+Python, it might do the job for you:

class GetKeys(GetKeysTemplate):
  def __init__(self, **properties):
      # Set Form properties and Data Bindings.
      self.init_components(**properties)
      # Make an 'invisible' textbox that blends in with your form design
      self.text_box_1.foreground = self.text_box_1.background = '#ffffff'
      self.text_box_1.border = "hidden"

  def text_box_1_change(self, **event_args):
    """This method is called when the text in this text box is edited"""
    print("You pressed:",self.text_box_1.text)
    self.text_box_1.text = ""

  def text_box_1_show(self, **event_args):
    """This method is called when the TextBox is shown on the screen"""
    self.text_box_1.focus()

# Remember to then set the Show and Change events in your Design panel