Check keypress types inside a polymer element?
-
22-12-2019 - |
Pregunta
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?
Solución
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.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow