hanes Posted November 10, 2006 Report Share Posted November 10, 2006 (edited) Hi, Im making an quick program in java. It all seems to work, but the applets never get keyEvents.... For example, the code below is an applet that when ran, should move a red dot left or right depending on key presses. But for some reason in linux, NO KEY EVENTS for the applet at all. In windoes or mac it works fine. I have tested this with konqueror, appletviewer and firefox.. no dice. Anyone know whats going on? example code import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; public class Bubbles extends JApplet implements KeyListener { int msg = 0; public void init() { msg = 10; addKeyListener(this); } //this function happens when someone hits a button public void keyPressed(KeyEvent e) { System.out.println("SFDF"); msg = e.getKeyCode(); repaint(); } public void keyReleased(KeyEvent e) { System.out.println("SFDF"); repaint(); } public void keyTyped(KeyEvent e) { System.out.println("SFDF"); msg = e.getKeyCode(); repaint(); } public void paint(Graphics g) { g.drawString("code = " + msg, 20,20); } } Edited November 10, 2006 by hanes Quote Link to comment Share on other sites More sharing options...
neddie Posted November 10, 2006 Report Share Posted November 10, 2006 Have you tried clicking on the applet to make sure it's got keyboard focus from the browser? Quote Link to comment Share on other sites More sharing options...
hanes Posted November 10, 2006 Author Report Share Posted November 10, 2006 I teach Java, and my students ALWAYS bug me about this, and, they never click on the browser... so its a good tip. BUT Yeah, I have clicked and clicked and clicked... i dont get it Quote Link to comment Share on other sites More sharing options...
neddie Posted November 10, 2006 Report Share Posted November 10, 2006 Seems like the JApplet is unable to get the keyboard focus - I'm not quite sure why but even a setFocusable(true) doesn't fix it. Of course, usually there's a bit more in the applet so it's not usually a problem. Workaround fix: use a Canvas instead: import java.awt.Canvas; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JApplet; public class Testclick extends JApplet implements KeyListener { int msg = 0; Canvas canvas = null; public void init() { msg = 1; canvas = new Canvas() { public void paint(Graphics g) { g.drawString("code = " + msg, 20,20); } }; canvas.addKeyListener(this); getContentPane().add(canvas); } // this function happens when someone hits a button public void keyPressed(KeyEvent e) { System.out.println("key press: " + e.getKeyCode()); msg = e.getKeyCode(); canvas.repaint(); } public void keyReleased(KeyEvent e) { System.out.println("key release"); } public void keyTyped(KeyEvent e) { System.out.println("key typed: " + e.getKeyChar()); } } Does that work? Quote Link to comment Share on other sites More sharing options...
hanes Posted November 10, 2006 Author Report Share Posted November 10, 2006 Yes it works. Thanks a lot. It also really pisses me off. How many years has sun had to get this thing right? I remember how excited I was when I was learning Java, thinking that I was learning a professional and well thought out language. Now, I see the bug reports grow, the lag persist (remember when Sun was touting that Java was fast?!) I gotta find another language to teach :) Anyways, thanks for the help. Quote Link to comment Share on other sites More sharing options...
neddie Posted November 10, 2006 Report Share Posted November 10, 2006 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.