本文介绍了keyPressEvent.getCharCode() 为所有特殊键(如 enter、tab、escape 等)返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

@Override
public void onKeyPress(KeyPressEvent event)
{
    if (event.getCharCode() == KeyCodes.KEY_ENTER)
    {
        registerButton.click();
    }
}

这是附加到一个文本框,当我按下回车键时它会触发.event.getCharCode() 只是零,而不是 13.当我按 tab 时,它是 0,当我按 Esc 时,它是 0.啊!

This is attached to a TextBox, and it does fire when I press enter. event.getCharCode() is just zero, not 13. When I press tab, it's 0, and when I press escape, it's 0. Argh!

昨天它运行正常,项目中的其他地方发生了变化以影响这一点 - 但我不确定它可能是什么.真的好像最后一天没有做任何相关的改变.

This was working properly yesterday, and something has changed somewhere else in the project to affect this - but I'm not sure what it could be. It really seems like no relevant changes have been made in the last day.

如果我改为处理 KeyUpEvent,这将按预期工作.

If instead I handle a KeyUpEvent, this works as expected.

我使用的是 GWT 2.1.0.感谢您的任何想法!

I'm using GWT 2.1.0. Thanks for any ideas!

推荐答案

KeyPressHandler 用于例如 、、 键.

the KeyPressHandler is used for example for the , , keys.

如果要将事件附加到另一个键,则必须使用 KeyDownHandler.

If you want to attach an event to another key you have to use KeyDownHandler.

nameField.addKeyDownHandler(new KeyDownHandler() {

    @Override
    public void onKeyDown(KeyDownEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            Window.alert("hello");
        }

    }

});

这篇关于keyPressEvent.getCharCode() 为所有特殊键(如 enter、tab、escape 等)返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 15:23