keyPressEvent.getCharCode()为所有特殊键返回0,如enter,tab,escape等 [英] keyPressEvent.getCharCode() returning 0 for all special keys like enter, tab, escape, etc

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

问题描述

我的代码:
$ b

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




$ b $ p
$ b这是附加到一个TextBox,当我按下输入。 event.getCharCode()只是零,而不是 13 。当我按tab时,它是 0 ,当我按下转义键时,它是 0 。呃!

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



如果我处理一个 KeyUpEvent ,这符合预期。



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

解决方案

使用 KeyPressHandler 如果你想附加一个 SHIFT CTRL ALT 键。 事件到另一个键你必须使用 KeyDownHandler

  nameField.addKeyDownHandler(new KeyDownHandler(){

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

}

});


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!

解决方案

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

    }

});

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆