keylistener
im trying to put a key listener into a game im making, what i need to do is check what key the user presses and if its an arrow key then i want to highlight a picture( change it so its a highlighted picture); ive tryed adding a key listener to a label, and a textfield but i cant manage to get it working, whenever i hit anything nothing happens as it should any help is appreciated
publicclass Codeextends Index{
staticint sent1 = 0;
staticint codeindex = 0;
protectedstatic ImageIcon createImageIcon(String path){
java.net.URL imgURL = MiniTV.class.getResource(path);
if (imgURL !=null){
returnnew ImageIcon(imgURL);
}else{
System.err.println("Couldn't find file: " + path);
returnnull;
}
}
publicvoid exeGame(){
// this is what index calls -
iconSetup();
}
publicvoid keyPressed(KeyEvent e){
int k = e.getKeyCode();
switch (e.getKeyCode()){
case KeyEvent.VK_UP:
lupo.setIcon(hup);
if (codeindex == 0){
lupo.setIcon(hup);
codeindex+= 1;
}elseif (codeindex == 1){
lupt.setIcon(hup);
codeindex+= 1;
}
break;
case KeyEvent.VK_DOWN:
if (codeindex == 2){
ldowno.setIcon(hdown);
codeindex+= 1;
}elseif (codeindex == 3){
ldownt.setIcon(hdown);
codeindex+= 1;
}
break;
case KeyEvent.VK_LEFT:
if (codeindex == 4){
llefto.setIcon(hleft);
codeindex+= 1;
}elseif (codeindex == 6){
lleftt.setIcon(hleft);
codeindex+= 1;
}
break;
case KeyEvent.VK_RIGHT:
if (codeindex == 5){
lrighto.setIcon(hright);
codeindex+= 1;
}elseif (codeindex == 7){
lrightt.setIcon(hright);
codeindex+= 1;
}
}
}
publicvoid iconSetup(){
stic.setIcon(dot);
title.setIcon(codem);
javax.swing.Timer timer =new javax.swing.Timer(2500, showarrow);
timer.start();
f (sent1==1){
timer.stop();
}
};
static ActionListener showarrow =new ActionListener(){
publicvoid actionPerformed(ActionEvent evt){
sent1 = 1;
lupo.setVisible(true);
lupt.setVisible(true);
ldowno.setVisible(true);
ldownt.setVisible(true);
llefto.setVisible(true);
lrighto.setVisible(true);
lleftt.setVisible(true);
lrightt.setVisible(true);
}
};
}
[5394 byte] By [
kohpowa] at [2007-11-27 6:59:13]

Use KeyBindings instead of KeyListeners.Here is a great tutorial: [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html] Sun's KeyBinding Tutorial[/url]
how exactly do the keybindings work, i tryed doing this
public void exeGame() {
field.requestFocus();
field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0),"bchk");
field.getActionMap().put("bchk", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (codeindex == 0){
lupo.setIcon(hup);
} else if (codeindex == 1){
lupt.setIcon(hup);
}
}
});
iconSetup();
}
but it didnt work, i dont really understand it from that tut
Here's a little demo:
import java.awt.event.*;
import javax.swing.*;
public class MapDemo extends JFrame {
private JLabel label;
public MapDemo() {
Action myAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
label.setText("You pressed F!");
}
};
label = new JLabel();
label.getInputMap().put(KeyStroke.getKeyStroke("F"), "fPressed");
label.getActionMap().put("fPressed", myAction);
this.getContentPane().add(label);
this.pack();
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setTitle("Demo");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] argv) {
new MapDemo().setVisible(true);
}
}
that didn't help, from what i see i did pretty much the same save the variables, i still cant get it working. and thats pretty much what was on the tut, which wasn't very clear on how to use them
even copying your code didnt work
public void exeGame() {
Action myAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
lupt.setIcon(hup);
ldownt.setIcon(hdown);
}
};
field.requestFocus();
field.getInputMap().put(KeyStroke.getKeyStroke("f"), "fPressed");
field.getActionMap().put("fPressed", myAction);
iconSetup();
}
}
Nowhere in the original code do I see a key listener being added. You've provided an implementation for the keyPressed method however it's not clear from the supplied code whether or not the class you're extending from (Index) extends the KeyAdapter class or implements the KeyListener interface. Also you're not requesting focus on the component. This is usually done after all the components have been rendered (usually after a setVisibile call). See attached sample working code that responds to key events.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class KeyTest
{
public static void main(String args[])
{
new KeyTestFrame();
}
}
class KeyTestFrame extends JFrame
{
private DrawCanvas drawCanvas = new DrawCanvas();
public KeyTestFrame()
{
super();
/* Components should be added to the container's content pane */
Container cp = getContentPane();
cp.add(BorderLayout.CENTER,drawCanvas);
/* Add the window listener */
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
dispose();
System.exit(0);
}
});
/* Size the frame */
setSize(200,200);
/* Center the frame */
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
/* Show the frame */
setVisible(true);
drawCanvas.requestFocusInWindow();
}
}
class DrawCanvas extends Canvas implements KeyListener
{
public DrawCanvas()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent evt)
{
System.out.println("You pressed the " + KeyEvent.getKeyText(evt.getKeyCode()) + " key!");
}
public void keyUp(KeyEvent evt)
{
}
public void keyDown(KeyEvent evt)
{
}
public void keyReleased(KeyEvent evt)
{
}
public void keyTyped(KeyEvent evt)
{
}
}
