Exercise 5: Keyboard Input

Home/Java - Tutorial/Java - Applet/Exercise 5: Keyboard Input
Exercise 5: Keyboard Input 2015-12-04T17:42:23+00:00

[ Home | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 ]

The source code:

(Download the below file: Keyboard1.java)

Try clicking and typing into the applet. You’ll probably have to click at least once before you begin typing, to give the applet the keyboard focus.

 

Go here for more information.

Here’s a second applet that nicely integrates most of what we’ve learned so far.

(Download the below file: Keyboard2.java)

Click, type, and move the mouse. You might see some flickering. Depending on the speed of your computer, you might also find that the mouse position is being sampled too quickly or too slowly. The upcoming lessons will give you tools to fix both of these problems.

 


Update (December 2010): I received the following email message that might be useful to some readers.

Thank you for your applet tutorial. I think that there is part of your tutorial that is out of data with modern java.awt tool kits that prevents lesson 5 on keyboard inputs to work correctly. In most cases it will not type the text because clicking does not by default cause the window to receive focus. If your users update the mouseClicked method below to include “this.requestFocusInWindow();” then it will cause the key press events to be fired as is expected in the tutorial.

Best Regards,

Lance Jensen