I search online and get no clear explaination about the use of e.consume() which is often used in java KeyEvent Handle.Like the follow Code.
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
if ( c != KeyEvent.CHAR_UNDEFINED ) {
s = s + c;
repaint();
e.consume();
}
}
Look at the documentation: The KeyEvent
inherits consume
method from InputEvent class. The consume method consumes this event so that it will not be processed in the default manner by the source which originated it.
Consume function is responsible for not processing the KeyListeners
code during some specific kind of events happen. For example : If i want to make a textfield
in java such that it will only respond when digits are pressed, then I can use the consume method to consume (Not process the keyevents which were not caused due to the pressing of digits) such events.
From the JavaDocs
Consumes this event so that it will not be processed in the default manner by the source which originated it.
Essentially what it means is you don't want the event to be dispatched to or handled by any further event listeners.
Although, generally speaking, this is dependent on the implementation of the individual listeners
It is a method of the AWTEvent
Class. It is used to consume this
event, if it can be consumed.
Did you check AWTEvent.consume()
?
Furthermore, look at How Does consume() Work? for explanation about what exactly consume does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With