By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

URGENT : keyevent not working!!

P: 8
below is a simple program to check key event ,it doesnt give any error nor an
exception at runtime plz tell where im goin wrong



code<text>


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class keys extends Applet implements KeyListener
{
Panel p;
TextField t1;
public void init()
{
setLayout(null);
setBackground(Color.black);
t1=new TextField(20);
p=new Panel();
p.setBackground(Color.black);
p.add(t1);
p.setBounds(50,50,500,100);
add(p);
p.setFocusable(true);
resize(500,500);
setVisible(true);
addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
System.out.println("KEY");
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT :
System.out.println("KEY RIGHT");
break;
}
}
public void keyTyped(KeyEvent e)
{
System.out.println("KEY");
}
public void keyReleased(KeyEvent e)
{
System.out.println("KEY");
}
}
Jul 28 '07 #1
Share this Question
Share on Google+
3 Replies


blazedaces
100+
P: 284
below is a simple program to check key event ,it doesnt give any error nor an
exception at runtime plz tell where im goin wrong
Here, have a look at java's keyListener tutorial: http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

It should cover most of your problems...

Good luck,
-blazed
Jul 28 '07 #2

P: 8
Here, have a look at java's keyListener tutorial: http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

It should cover most of your problems...

Good luck,
-blazed
thanx a ton man it helped one should add requestFocus()
after addKeyListener(this);
Jul 29 '07 #3

Expert 10K+
P: 11,448
Why don't you simply register your keylistener to your t1 textfield?

kind regards,

Jos
Jul 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.