Perry Smith

Java Questions & Answers

Archive

Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page

keyboard action on canvas subclass
Thursday, 08-Apr-99 20:58:26

194.158.187.242 writes:

How to make a canvas subclass respond to keyboard input?

The canvas should react to the cursor keys. Also the Esc key should cancel a mouse drag. The mouse events are fine, but the keyboard events don't seem to work.

I've done an 'implements KeyListener', created methods for keyPressed(), KeyReleased() & keyTyped(). The constructor does a this.addKeyListener(this). Can't see what I've missed out.

Yet it seems like the keyMethods are not initiated. Suspecting that it was because the canvas did not have the cursor focus, I've overloaded the isFocusTraversable() method, but that does not seem to solve the problem.

So, how to make a canvas respond to keyboard input?

Henk Jan


Responses
Re: keyboard action on canvas subclass
Friday, 09-Apr-99 03:17:49

202.54.7.11 writes:

hi,
if u could mail the code, i can try to help.

Ganesh


solution: requestFocus()
Sunday, 11-Apr-99 16:39:51

194.158.187.242 writes:

Thanks Ganesh for the offer. Found a workaround after a weekend of 'background puzzling':

Do on mouseDown:
canvasSubclass.requestFocus();
(I searched extensively for getFocus and setFocus methods, but couldn't find them).

The events were not triggered because the canvas did not have the focus. The tab key skipped the canvas and jumped to the next field. A mouse down does not automatically set the focus.

It is still a mystery to me why a canvas can't get a focus in a normal way. But as 7 of 9 would say: it's irrelevant.

Henk Jan


Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page