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
,当我按 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
KeyPressHandler
用于例如 SHIFT、CTRL、ALT 键。如果您想将事件附加到另一个键,则必须使用
KeyDownHandler
。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
.或者你可以试试这个
or you can try this
应使用 KeyUpHandler 而不是 KeyPresshandler。
KeyUpHandler should be used instead of KeyPresshandler.
他们可能会改变 FF 上的行为。我正在使用 GWT 2.4.0 和 Firefox 10。根据此 评论,在他们解决问题之前,您应该使用如下所示的内容:
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:
我从2.0.4更新到2.2.1时遇到了同样的问题,所以它似乎与gwt代码有关。
部分问题在于 KeyPressedEvent 代表本机(即特定于浏览器的)按键事件。在bug,其中一条评论说:
在解决这个混乱问题之前,最好的选择是使用 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:
Until this mess is sorted out, your best bet is to use the workaround with KeyDownHandler or KeyUpHandler.
GWT 2.5:
GWT 2.5: