检查聚合物元件内的按键类型?

Naw*_*ami 4 dart dart-polymer

以下聚合物元素具有on-keypress事件侦听器:

<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>
Run Code Online (Sandbox Code Playgroud)

我希望元素能够监听特定的按键类型(例如,输入,退格等).这可能吗?

Mar*_*ioP 5

您不能仅直接收听特定的密钥代码,但当然您可以检查密钥代码并在您的监听器中采取相应的行动:

// 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
    }
}
Run Code Online (Sandbox Code Playgroud)

正如评论所说,你不能检查更多"高级"键on-keypress,你必须使用on-keydownon-keyup为此.