Javascript 在GWT中处理铁名单的键盘事件?

Javascript 在GWT中处理铁名单的键盘事件?,javascript,html,gwt,polymer,polymer-1.0,Javascript,Html,Gwt,Polymer,Polymer 1.0,我使用谷歌聚合物 <iron-list items="[[data]]" as="item"> <template> <div tabindex$="[[tabIndex]]"> Name: [[item.name]] </div> </template> </iron-list> 当我检查键绑定的当前值时,我定义了一个打印函数来将变量记录到控制台: 公共本机void打印(JavaScr

我使用谷歌聚合物

<iron-list items="[[data]]" as="item">
  <template>
    <div tabindex$="[[tabIndex]]">
      Name: [[item.name]]
    </div>
  </template>
</iron-list>
当我检查键绑定的当前值时,我定义了一个打印函数来将变量记录到控制台:

公共本机void打印(JavaScriptObject obj)/-{ 控制台日志(obj); }-/;

然后,我用以下方法打印当前值:

print(list.getKeyBindings());
结果是:

Object {up: "_didMoveUp", down: "_didMoveDown", enter: "_didEnter"}
似乎已经定义了一些键绑定,但我不知道在哪里可以找到函数
\u didMoveUp
\u didMoveDown
\u didEnter

当我这样做的时候

print(list.getKeyEventTarget());
我得到:

<iron-list class="fit x-scope iron-list-0" tabindex="1" style="overflow: auto;">
</iron-list>


如何设置处理程序以使用Vaadin Polymer GWT lib捕获键盘事件?按下enter等键时,如何接收事件?

如果要添加键事件自定义元素(我不知道是否理解并更正问题) 您必须实现Polymer.IronA11yKeysBehavior行为,然后使用
keyBindings
prototype属性来表示触发事件的键组合


按下:[[项目]]
聚合物({
是‘x元素’,
行为:[
聚合铁A11YKEYSBEHAVIOR
],
特性:{
preventDefault:{type:Boolean,value:true},
数据:{value:[]},
keyEventTarget:{
类型:对象,
值:函数(){
返回文档.body;
}
}
},
键绑定:{
“*pageup pagedown left right down home end space enter@~“$?!\\+:\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \,
“a”:“u handler”,
“shift+a alt+a”:“u handler”,
“shift+tab shift+space”:“\u处理程序”
},
_处理程序:函数(事件){
如果(this.preventDefault){//尝试设置真/假,然后按SHOT+a,然后按up或down
event.preventDefault();
}
console.log(event.detail.combo);
this.push('data',event.detail.combo);
}
});

回答这个问题

list.setKeyBindings(???);//不知道如何使用此函数

根据
com/vaadin/polymer/vaadin gwt polymer elements/1.2.3.1-SNAPSHOT/vaadin-gwt-polymer-elements-1.2.3.1-20160201.114641-2.jar!/com/vaadin/polymer/public/bower_components/iron list/iron list/iron list.html:292

keyBindings
应具有以下结构的
object

    {
      'up': '_didMoveUp',
      'down': '_didMoveDown',
      'enter': '_didEnter'
    }
要构造此类对象,可以使用以下命令:

        new JSONObject() {{
            put("up", new JSONString("_didMoveUp"));
            put("down", new JSONString("_didMoveDown"));
            put("enter", new JSONString("_didEnter"));
        }}.getJavaScriptObject();
我不知道在哪里可以找到函数_didMoveUp、_didMoveDown和 _迪登特

可以在这里找到它们:
com/vaadin/polymer/vaadin gwt polymer elements/1.2.3.1-SNAPSHOT/vaadin-gwt-polymer-elements-1.2.3.1-20160201.114641-2.jar!/com/vaadin/polymer/public/bower_components/iron list/iron list.html:1504

这是摘录

    _didMoveUp: function() {
      this._focusPhysicalItem(Math.max(0, this._focusedIndex - 1));
    },

    _didMoveDown: function() {
      this._focusPhysicalItem(Math.min(this._virtualCount, this._focusedIndex + 1));
    },

    _didEnter: function(e) {
      // focus the currently focused physical item
      this._focusPhysicalItem(this._focusedIndex);
      // toggle selection
      this._selectionHandler(e.detail.keyboardEvent);
    }
如何设置使用Vaadin捕获键盘事件的处理程序 聚合物GWT-lib

输入enter等键时如何接收事件 压力

我可以找到这种聚合物:不用于外部用途的属性应以下划线作为前缀

这就是为什么它们没有在
JsType
IronListElement
中公开的原因。 您可以使用
JSNI
更改此函数。我认为smth如下所示:

    private native static void changeDidMoveUp(IronListElement ironList) /*-{
        var _old = ironList._didMoveUp;
        ironList._didMoveUp = function() {
            console.log('tracking');
            _old();
        }
    }-*/;
或者添加一个新的

    IronListElement element ...

    com.vaadin.polymer.elemental.Function<Void, Event> func = event -> {
        logger.debug(event.detail);
        ...
        return null;
    };

    private native static void addUpdatePressed(IronListElement ironList, Function func) /*-{
        ironList._updatePressed = func;
    }-*/;

    {
        addUpdatePressed(element, func);
        element.addOwnKeyBinding("a", "_updatePressed");
        element.addOwnKeyBinding("shift+a alt+a", "_updatePressed");
        element.addOwnKeyBinding("shift+tab shift+space", "_updatePressed");
    }
IronListElement元素。。。
com.vaadin.polymer.element.Function func=事件->{
logger.debug(event.detail);
...
返回null;
};
私有本机静态void addUpdatePressed(IronListElement ironList,函数func)/*-{
ironList.\u updatePressed=func;
}-*/;
{
addUpdatePressed(元素,func);
元素addOwnKeyBinding(“a”,“_updatePressed”);
元素addOwnKeyBinding(“shift+a alt+a”,“u updatePressed”);
元素addOwnKeyBinding(“shift+tab shift+space”,“_updatePressed”);
}
应该可以。您可以从
IronList\getpolymerement()
获取元素


请记住,我还没有测试过这段代码:)

你想用铁名单做什么?我可以帮你添加按键事件的处理程序。这是JavaScript解决方案。请看我的编辑,我需要知道它是如何与GWT Vaadin聚合元素一起工作的。我不知道GWT。所以我的答案是这样的,也许有人会在我做的时候使用它我们在JSNI中的代码是:“一个同名的类型已经注册了”。这是因为它已经被定义了。以后是否可以添加处理程序?仍然没有回答我的问题?回答得很好,但是你能更详细地解释我最后两个问题之后的最后一部分吗?
    IronListElement element ...

    com.vaadin.polymer.elemental.Function<Void, Event> func = event -> {
        logger.debug(event.detail);
        ...
        return null;
    };

    private native static void addUpdatePressed(IronListElement ironList, Function func) /*-{
        ironList._updatePressed = func;
    }-*/;

    {
        addUpdatePressed(element, func);
        element.addOwnKeyBinding("a", "_updatePressed");
        element.addOwnKeyBinding("shift+a alt+a", "_updatePressed");
        element.addOwnKeyBinding("shift+tab shift+space", "_updatePressed");
    }