0

将一个KeyListener类添加到一个org.eclipse.swt.widgets.Text类。当我输入'a'时,我得到了多个 keyEvents。我只期待一个keyEvent。这是为什么。

text.addKeyListener(new KeyAdapter() {
    @Override
    public void keyReleased(KeyEvent keyEvent) {
        super.keyReleased(keyEvent);
        logger.log(Level.INFO, "keyEvent-->" + "\n" +  
                            "character :" + keyEvent.character + "\n" + 
                            "code      :" + keyEvent.keyCode + "\n" +
                            "location  :" + keyEvent.keyLocation + "\n" +
                            "stateMask :" + keyEvent.stateMask + "\n" +
                            "time      :" + keyEvent.time + "\n" +
                            "hashCode  :" + keyEvent.hashCode() + "\n" +
                            "data      :" + keyEvent.data + "\n" +
                            "doit      :" + keyEvent.doit + "\n");
    }
});

输出(在键盘上按一次“a”后):

keyReleased
INFO: keyEvent-->
character :a
code      :97
location  :0
stateMask :0
time      :15626510
hashCode  :692735
data      :null
doit      :true

keyReleased
INFO: keyEvent-->
character :a
code      :97
location  :0
stateMask :0
time      :15626510
hashCode  :32967617
data      :null
doit      :true

keyReleased
INFO: keyEvent-->
character :a
code      :97
location  :0
stateMask :0
time      :15626510
hashCode  :2412752
data      :null
doit      :true

keyReleased
INFO: keyEvent-->
character :a
code      :97
location  :0
stateMask :0
time      :15626510
hashCode  :17374804
data      :null
doit      :true

keyReleased
INFO: keyEvent-->
character :a
code      :97
location  :0
stateMask :0
time      :15626510
hashCode  :27668426
data      :null
doit      :true

keyReleased
INFO: keyEvent-->
character :a
code      :97
location  :0
stateMask :0
time      :15626510
hashCode  :25933099
data      :null
doit      :true
4

1 回答 1

1

您可能在该组件的整个生命周期中添加了多个 KeyAdapter - 尝试通过查看getKeyListeners().length返回的内容进行检查。

于 2013-01-30T13:30:03.550 回答