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

时间:2014-01-11 00:15:21

标签: 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>

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

1 个答案:

答案 0 :(得分: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
    }
}

正如评论所说,您无法在on-keypress中检查更多“高级”密钥,您必须使用on-keydownon-keyup