Override keypress method in lwuit

2019-08-27 02:56发布

问题:

How to override keypressed method form in lwuit? if suppose user press numeric 1 from keypad how can I know that user has pressed 1? In canvas we just need to handle keypressed method. but in lwuit how to handle this method. and which listener I have to implements?

回答1:

keypressed is method in interface so you always override that methos in class

see this example

if you dont khow exact key code then print key code in

public void keyPressed(keyCode){
System.out.println(keyCode);
}

run your program, press "1" and see output



回答2:

You can override the form's keyPressed/Releaseed methods (normally you should override release!). Every focusable component has these methods as well and will receive the keyPressed/Released calls when it has focus so you can override them in component as well. The keycode you will receive for numeric/character keys is the actual character (e.g. for your case '1').

You can register global listeners for the form using key listener/game listener in the form (addKeyListener, addGameKeyListener).

Notice that LWUIT has a "game key" concept for navigation/action buttons since the values for these buttons often differ between platforms.