Hi, I'm trying to program Tetris. The thing is, I can't seem to get the Keylistener to work in the JApplet, and I have no idea why :(
Here's my code:

import java.util.Random;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

 * Class Tetris - write a description of the class here
 * @author (Neil Semmel) 
 * @version (Nov. 7, 2008)
public class Tetris extends JApplet implements KeyListener
    Random randy = new Random();
    shapes test = new shapes();
    public int x_pos = 220;
    public int y_pos = 0;
    public void init()
    public void paint(Graphics g)
    // ----- Key Events -----
    public void keyPressed(KeyEvent e) 
        char code = e.getKeyChar();
        if(code == 'a')
        x_pos = x_pos-20;
        if(code == 'd')
        x_pos = x_pos+20;
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    // ----- End Key Events -----
    public void reDraw(Graphics g)
    public void sleep(int time)
        long now = System.currentTimeMillis();
        while(now+time > System.currentTimeMillis());
    public void getPiece(Graphics g)
        int pieceNum = randy.nextInt(7);
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:

What do you mean by "can't get it to work"? Because your key listener does respond as long as you make sure it has focus (i.e. click in the panel). You can add a call to requestFocusInWindow() in init() if you want to make sure it gets focus without the click.

I couldn't say if any of the other parts work because they reference variables and classes that do not exist in the code you posted. I just commented those out when I ran it.

Ok, so, I just looked back through my code, and pressing a or d wouldn't do anything, the way i set it up >.< So, I replaced the getPiece() method in the paint method with drawRect(x_pos,y_pos, 25,25); , but the KeyListener still didn;t change anything. (and yes, i did add the requestFocusInWindow(), it didn't do anything :( )

I just added System.out.println() calls in your KeyListener and ran it in both the applet viewer and a web page and they worked fine as long as it had the focus.

You need to add a call to repaint() after you update your coordinates in the KeyListener.

Hmm, neither work for me, even after adding in repaint(). I normally use BlueJ as my compiler, but i used NetBeans also, and it didn't work on that either....

I was looking for a similar solution as am upgrading an old applet, and came across your question.
Hi, I'm trying to program Tetris. The thing is, I can't seem to get the Keylistener to work in the JApplet, and I have no idea why :(
Here's my code:
... Snipped ...
As no one has yet answered this properly here, I will document what I did:

1) Implement your applet with KeyEventDispatcher interface. public class SnakeApplet extends JApplet implements java.awt.KeyEventDispatcher { 2) The compiler will insist that you implement method: public boolean dispatchKeyEvent(java.awt.event.KeyEvent evt) There you put your processing on the key events.

3) The dispatcher has to be registered with the KeyboardFocusManager KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this); which would normally be done in the applet init() method.
In our case the applet is the dispatcher so use "this".

Hope this helps. I might be posting his solution on other sites which I came across.

Thanks! I found a different way to do it, but i lost the jump drive the file was on... and cant remember how i did it...
But again, thanks for letting me know how to do it! =D