Question

The following polymer element has an on-keypress event listener:

<polymer-element name="custom-element">
  <template>
    <input type="text" on-keypress="{{print}}" value="{{text}}">
  </template>
  <script type="application/dart" src="custom.dart"></script>
</polymer-element>

I want the element to listen for specific keypress types (e.g., enter, backspace, etc.). Is this possible?

Was it helpful?

Solution

You can't directly listen to specific key codes only, but of course you can check the keycode and act accordingly in your listener:

// method name based on the template in the question
void print(Event e, var detail, Node target) {
    int code = (e as KeyboardEvent).keyCode;
    switch(code) {
    case 13:
        // this is enter
    case 8:
        // this is backspace, but not in keypress event
    }
}

As the comment said, you can't check for more "advanced" keys in on-keypress, you would have to use on-keydown or on-keyup for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top