Click to See Complete Forum and Search --> : Java Key Listener


emus
10-17-2003, 02:54 AM
When I need a key listener in java and just need to listen for a keyPressed event, why is it that both the keyReleased and keyTyped method need to be overridden in addiiton to the only method I really need? Is this meant to guard against errors?

Thanks,

emus

Stuka
10-17-2003, 12:28 PM
KeyListener is an interface - you MUST override all the definitions in an interface, as there is no 'default' implementation. If you want to have it easier, rather than implementing KeyListener, you can extend KeyAdapter (which implements KeyListener with all empty methods). There is, AFAIK, an 'Adapter' class like this for every 'Listener' interface.

emus
10-17-2003, 06:07 PM
Thank you very much Stuka, I'll read up on KeyAdapters.