不会调用聚合物按键事件处理程序 [英] Polymer keypress event handler not being called

查看:89
本文介绍了不会调用聚合物按键事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚为什么在以下聚合物中我无法调用keypressHandler函数.我究竟做错了什么? (我尝试将on-keypress属性放在span元素本身上,但仍然没有雪茄.)其余功能可以按预期工作.

Can't figure out why in the following polymer I am unable to get the keypressHandler function to be called. What am I doing wrong? (I tried putting the on-keypress attribute on the span element itself, but still no cigar.) The rest of the functionality works as expected.

<link rel="import" href="../bower_components/polymer/polymer.html">

<polymer-element name="psq-posscell" attributes="isposs nVal"  on-tap="{{toggle}}" on-keypress="{{keypressHandler}}" >
  <template>
    <link rel="stylesheet" href="../bower_components/polymer-flex-layout/polymer-flex-layout.css" />
    <link rel="stylesheet" href="psq.css" />
    <span class="flexbox align-center justify-center toggle" >{{isposs ? nVal : ' '}}</span>
    <style>
        .toggle {
            float:left; 
            border:1px solid white;
            text-align:center;  
            line-height:2;
            background-color:#f2f2f2;      
        }

        .toggle:hover {
            background-color:#0d2f5a;
            color:white; 
          }

    </style>

  </template>
  <script>
    Polymer('psq-posscell', {
      isposs: true,
      toggle: function() {
        this.isposs = !this.isposs;
        console.log("toggle called");
      },
      ispossChanged: function() {
        console.log("ispossChanged called");
      },
      keypressHandler: function(event, detail, sender) { 
        console.log("key pressed");
      },
    });
  </script>
</polymer-element>

推荐答案

按下键时,系统必须知道将事件发送到哪里.这就是为什么有focus概念的原因.为了使您的元素能够接收密钥,必须重点关注.

When you press a key, the system has to know where to send the event. This is why there is a concept of focus. For your element to receive keys it must be focused.

您可以通过设置tabIndex使其聚焦,例如

You can make it focus-able by setting tabIndex, e.g.

ready: function() { this.tabIndex = 0; }

现在,您可以通过在元素(this.focus())上调用focus()方法,或者通过对其进行制表或单击来使元素集中.

Now you can cause your element to be focused by calling the focus() method on the element (this.focus()), or by tabbing to it or clicking on it.

一旦您的元素被聚焦,它就会受到按键的按下.

Once your element is focused, it should receive key presses.

这篇关于不会调用聚合物按键事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆