keyPressEvent.getCharCode() 对于所有特殊键(如 Enter、Tab、Escape 等)返回 0

发布于 2024-09-30 14:51:58 字数 536 浏览 4 评论 0原文

我的代码:

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

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

昨天它工作正常,项目中其他地方发生了一些变化来影响这一点 - 但我不确定它可能是什么。看起来确实在最后一天没有做出任何相关的改变。

如果我处理 KeyUpEvent,这将按预期工作。

我正在使用 GWT 2.1.0。感谢您的任何想法!

My code:

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

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.

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

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

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

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

发布评论

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

评论(6

赠意 2024-10-07 14:51:58

KeyPressHandler 用于例如 SHIFTCTRLALT 键。

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

nameField.addKeyDownHandler(new KeyDownHandler() {

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

    }

});

the KeyPressHandler is used for example for the SHIFT, CTRL, ALT keys.

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");
        }

    }

});
沦落红尘 2024-10-07 14:51:58

或者你可以试试这个

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
}

or you can try this

if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
}
千寻… 2024-10-07 14:51:58

应使用 KeyUpHandler 而不是 KeyPresshandler。

newSymbolTextBox.addKeyUpHandler(new KeyUpHandler() {
    @Override
    public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            addStock();
        }
    }
});

KeyUpHandler should be used instead of KeyPresshandler.

newSymbolTextBox.addKeyUpHandler(new KeyUpHandler() {
    @Override
    public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            addStock();
        }
    }
});
徒留西风 2024-10-07 14:51:58

他们可能会改变 FF 上的行为。我正在使用 GWT 2.4.0 和 Firefox 10。根据此 评论,在他们解决问题之前,您应该使用如下所示的内容:

@Override
public void onKeyPress(KeyPressEvent event) {
    int keyCode = event.getUnicodeCharCode();
    if (keyCode == 0) {
        // Probably Firefox
        keyCode = event.getNativeEvent().getKeyCode();
    }
    if (keyCode == KeyCodes.KEY_ENTER) {
        // Do something when Enter is pressed.
    }
}

They might change the behavior on FF. I'm using GWT 2.4.0, and Firefox 10. According to this comment, You should use something like below before they fix the problem:

@Override
public void onKeyPress(KeyPressEvent event) {
    int keyCode = event.getUnicodeCharCode();
    if (keyCode == 0) {
        // Probably Firefox
        keyCode = event.getNativeEvent().getKeyCode();
    }
    if (keyCode == KeyCodes.KEY_ENTER) {
        // Do something when Enter is pressed.
    }
}
耳钉梦 2024-10-07 14:51:58

我从2.0.4更新到2.2.1时遇到了同样的问题,所以它似乎与gwt代码有关。

部分问题在于 KeyPressedEvent 代表本机(即特定于浏览器的)按键事件。在bug,其中一条评论说:

但是,预计“escape”不会生成 KeyPressEvent(IE 和 WebKit 行为);你必须使用 KeyDown 或 KeyUp 来实现这些。
不幸的是,Firefox(和 Opera)比其​​他浏览器(IE 和 WebKit,它们具有最明智的实现,W3C 即将在 DOM 3 事件中标准化的实现,AFAICT)触发更多的按键事件,但在这种情况下 getCharCode() 为 0所以你可以安全地忽略它们。一个例外(可能还有其他例外)是“enter”键。
浏览器中的 Key/char 事件是如此混乱,以至于 GWT 没有做太多事情来同质化事物(至少现在是这样)。

在解决这个混乱问题之前,最好的选择是使用 KeyDownHandler 或 KeyUpHandler 来解决问题。

I got the same problem when updating from 2.0.4 to 2.2.1, so it seems to be related to the gwt code.

Part of the issue is that KeyPressedEvent represents a native (ie browser-specific) key press event. In the bug you filed on this issue, one of the comments says:

It is however expected that "escape" does not generate a KeyPressEvent (IE and WebKit behavior); you have to use KeyDown or KeyUp for those.
Firefox (and Opera) unfortunately fires many more keypress events than others (IE and WebKit, which have the most sensible implementation, the one the W3C is about to standardize in DOM 3 Events, AFAICT), but in that case getCharCode() is 0 so you can safely ignore them. The one exception (there might be others) is the "enter" key.
Key/char events in browsers are such a mess that GWT doesn't do much to homogenize things (at least for now).

Until this mess is sorted out, your best bet is to use the workaround with KeyDownHandler or KeyUpHandler.

疯狂的代价 2024-10-07 14:51:58

GWT 2.5:

public void onKeyPress(KeyPressEvent event) {
    if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
        // Event
    }
}

GWT 2.5:

public void onKeyPress(KeyPressEvent event) {
    if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
        // Event
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文