Nawaf Alsulami
Nawaf Alsulami

Reputation: 719

Check keypress types inside a polymer element?

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?

Upvotes: 4

Views: 3054

Answers (1)

MarioP
MarioP

Reputation: 3832

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.

Upvotes: 5

Related Questions