I noticed there was a post concerning keyboard input, but none of the examples helped- the author was concerned about reading integers, and the answers were question-specific. I'm concerned about reading keyboard input for up, down, left, and right.<br>
<br>
All the books I have read have fallen short in one way or another- one covered Java 1.1, another only covered mouse-specific events (scrollbars, checkboxes, etc.), and another was software-specific (Borland JBuilder) code that didn't really apply to my situation at all. Here's my situation...<br>
<br>
I have one class extending Thread that I'm trying to control from another class. Currently, it works by pressing buttons- in my applet, I have buttons for "Up", "Down", "Left", and "Right". However, I'm trying to get it to work for keyboard input instead. Here's what I have:<br>
<br>
<br>
my main class extends ActionListener, KeyListener (for both mouse buttons and keyboard input).<br>
<br>
in my init(), I have these lines pertaining to keyboard input:<br>
this.addKeyListener(this);<br>
this.requestFocus();<br>
<br>
then I have a public void keyPressed method as such:<br>
public void keyPressed (KeyEvent e) {<br>
switch (e.getKeyCode()) {<br>
case e.VK_UP: blah; break;<br>
case e.VK_DOWN: blah; break;<br>
case e.VK_LEFT: blah; break;<br>
case e.VK_RIGHT: blah; break;<br>
default: blah; break;<br>
}<br>
}<br>
<br>
I have empty keyReleased and keyTyped methods.<br>
<br>
The whole thing compiles just fine- but when I press a key, nothing happens. I put something outside of the switch to see if the keyPressed function was being invoked at all (same for keyReleased and keyTyped), but with no result. Why isn't the applet aware of my typing?<br>
<br>
Thanks to anybody who can help...<br>
<br>
Liam Morley<br>
<A HREF="mailto:lmorley@wpi.edu">lmorley@wpi.edu</A><br>
<br>
All the books I have read have fallen short in one way or another- one covered Java 1.1, another only covered mouse-specific events (scrollbars, checkboxes, etc.), and another was software-specific (Borland JBuilder) code that didn't really apply to my situation at all. Here's my situation...<br>
<br>
I have one class extending Thread that I'm trying to control from another class. Currently, it works by pressing buttons- in my applet, I have buttons for "Up", "Down", "Left", and "Right". However, I'm trying to get it to work for keyboard input instead. Here's what I have:<br>
<br>
<br>
my main class extends ActionListener, KeyListener (for both mouse buttons and keyboard input).<br>
<br>
in my init(), I have these lines pertaining to keyboard input:<br>
this.addKeyListener(this);<br>
this.requestFocus();<br>
<br>
then I have a public void keyPressed method as such:<br>
public void keyPressed (KeyEvent e) {<br>
switch (e.getKeyCode()) {<br>
case e.VK_UP: blah; break;<br>
case e.VK_DOWN: blah; break;<br>
case e.VK_LEFT: blah; break;<br>
case e.VK_RIGHT: blah; break;<br>
default: blah; break;<br>
}<br>
}<br>
<br>
I have empty keyReleased and keyTyped methods.<br>
<br>
The whole thing compiles just fine- but when I press a key, nothing happens. I put something outside of the switch to see if the keyPressed function was being invoked at all (same for keyReleased and keyTyped), but with no result. Why isn't the applet aware of my typing?<br>
<br>
Thanks to anybody who can help...<br>
<br>
Liam Morley<br>
<A HREF="mailto:lmorley@wpi.edu">lmorley@wpi.edu</A><br>