检票口关键事件->拿到钥匙!

发布于 2024-09-12 19:30:03 字数 182 浏览 6 评论 0原文

还有一个问题: 我创建了一个输入字段并添加了一个 AjaxFormComponentUpdatingBehavior(“onkeyup”)。现在我只想在按下右键(空格键)时执行一些代码。 我怎样才能获得最后按下的键?我以为它会存储在目标属性中,但我在那里找不到它......有什么简单的方法来解决这个问题吗?

谢谢大家! 西尔维斯大学

one more question:
I created an inputfield and added an AjaxFormComponentUpdatingBehavior ("onkeyup"). Now I want to execute some code only if the right key (space-key) is pressed.
How am I able to get the last pressed key? I thought it would be stored in the target attribute but I couldn't find it there... Is there any easy way to solve this problem?

Thx guys!
CU Sylvus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高速公鹿 2024-09-19 19:30:03

如果您想捕获键,则不应使用 AjaxFormComponentUpdatingBehavior。此行为是为更新表单组件模型的操作保留的。我可能会尝试单独使用 javascript 来完成此操作,特别是如果您使用的是 mootools 或 prototype 等 javascript 框架。这是 mootools 的一些示例代码(无需将其发送到服务器):

    this.add(new TextField<String>("textField").add(new AbstractBehavior(){

        private static final long serialVersionUID = 1L;
        private Component component;

        @Override
        public void bind(final Component component){
            this.component = component.setOutputMarkupId(true);
        }

        @Override
        public void renderHead(final IHeaderResponse response){
            response.renderOnDomReadyJavascript(
                "$('" + this.component.getMarkupId() + "')" +
                    ".addEvent('keyup',function(event){" +
                        "if(' '==event.key){" +
                            "alert('you pressed space!!!')" +
                        "}" +
                    "}" +
                ");");
        };

    }));

如果没有可用的 js 库,这里有一个仅 wicket 的解决方案:

        @Override
        public void renderHead(final IHeaderResponse response){
            response.renderJavascriptReference(WicketEventReference.INSTANCE);
            response.renderOnDomReadyJavascript("Wicket.Event.add('"
                + this.component.getMarkupId()
                + "',onkeyup',function(event){" + "if(' '==event.key){"
                + "alert('you pressed space!!!')" + "}" + "}" + ");");
        };

但这并不能解决事件处理中的跨浏览器问题

You should not use an AjaxFormComponentUpdatingBehavior if you want to capture keys. This behavior is reserved for actions that update the form component model. I would probably try to do it in javascript alone, especially if you are using a javascript framework like mootools or prototype. Here is some sample code for mootools (no need to send this to the server):

    this.add(new TextField<String>("textField").add(new AbstractBehavior(){

        private static final long serialVersionUID = 1L;
        private Component component;

        @Override
        public void bind(final Component component){
            this.component = component.setOutputMarkupId(true);
        }

        @Override
        public void renderHead(final IHeaderResponse response){
            response.renderOnDomReadyJavascript(
                "$('" + this.component.getMarkupId() + "')" +
                    ".addEvent('keyup',function(event){" +
                        "if(' '==event.key){" +
                            "alert('you pressed space!!!')" +
                        "}" +
                    "}" +
                ");");
        };

    }));

if no js library is available, here's a wicket-only solution:

        @Override
        public void renderHead(final IHeaderResponse response){
            response.renderJavascriptReference(WicketEventReference.INSTANCE);
            response.renderOnDomReadyJavascript("Wicket.Event.add('"
                + this.component.getMarkupId()
                + "',onkeyup',function(event){" + "if(' '==event.key){"
                + "alert('you pressed space!!!')" + "}" + "}" + ");");
        };

but this does not deal with cross-browser issues in event handling

╰ゝ天使的微笑 2024-09-19 19:30:03

感谢 Google 和 Firebug,我找到了解决方案。

searchInput.add(new AbstractBehavior() {
    private static final long   serialVersionUID    = 1L;
    private Component           component;

    @Override
    public void bind(final Component component) {
        this.component = component.setOutputMarkupId(true);
    }

    @Override
    public void renderHead(final IHeaderResponse response) {
        response.renderJavascriptReference(WicketEventReference.INSTANCE);

        response.renderOnDomReadyJavascript("document.getElementById('" +
          this.component.getMarkupId() + "').onkeyup=function(event){\n" +
          "if(32==event.keyCode){\n" + "alert('you pressed space!!!')" + "\n}" +
          "}");
    }
});

I found the solution, thanks to Google and Firebug.

searchInput.add(new AbstractBehavior() {
    private static final long   serialVersionUID    = 1L;
    private Component           component;

    @Override
    public void bind(final Component component) {
        this.component = component.setOutputMarkupId(true);
    }

    @Override
    public void renderHead(final IHeaderResponse response) {
        response.renderJavascriptReference(WicketEventReference.INSTANCE);

        response.renderOnDomReadyJavascript("document.getElementById('" +
          this.component.getMarkupId() + "').onkeyup=function(event){\n" +
          "if(32==event.keyCode){\n" + "alert('you pressed space!!!')" + "\n}" +
          "}");
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文