ok im doing a tetris game and i want the piece to move left when i click 'a' and right when i click 'd' and turn clockwise when i click 'w' and turn counterclockwise when i click 's'. heres what i have

public class Movement extends JPanel implements KeyListener {

        public Movement (){
               setFocusable(this);
               addnewKeyListener(this);
         }

         public void keyTyped (KeyEvent e){
                 if (e.getKeyCode == 'a')
                       //shift left code
                 else if (e.getKeyCode == 'd')
                       //shift right code
                 else if (e.getKeyCode == 'w')
                       //rotate clockwise code
                 else if (e.getKeyCode == 's')
                       //rotate counterclockwise code
          }
          //irrelevant keyPressed and key Realeased methods that i dont do anything in

in the frame i use the built in keyTyped method to call

       move.keyTyped(evt)//evt being the KeyEvent the method uses

however it is not reading the keys being typed any key being typed for that matter. Please help!

And... ?

You haven't stated a question. Also, why would Movement be a class of it's own? A class encapsulates state and behavior. What state and behavior would be represented by Movement?

Edit: It looks like your post got cut off initially and you have added more to it, so the part about no longer really applies. The rest does though.

wat i want is that the Movement class should implement the KeyListener interface and read the keyboard when a key is TYPED, not pressed or released, and then proceeds to process that KeyEvent. If that KeyEvent happens to be an 'a','d','w','s' do a specific action to the Tetris piece. Now i had this initially working when it was just the frame and the movement class, but i moved both of those to a different file with my other class for this project and now it doesn't work. I then went back to my old one which i hadn't touched in several days and that did not work, i am very confused

P.S. This only my second year programming and im relativily new to graphics, animation and reading keyboards

Given that KeyListener is somewhat sensitive to component focus issues, it's often easier to use the InputMap and ActionMap of the root pane of the top level window (JFrame most likely). This is done by registering the keystroke inputs to actions like so:

getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
      KeyStroke.getKeyStroke("A"), "MoveLeft");
    
    getRootPane().getActionMap().put("MoveLeft", new AbstractAction() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            // move left
        }
    });

ok i tryed KeyBinders before but i could not get it to work, ill try this, but where exactly should it go, in the Movement class? or directly into the keyTyped method of the frame?

Not knowing a thing about how you structured your program I really couldn't say. You need to register the input keys and their actions with the top-level window. How you go about that depends on the rest of your structure. It has nothing at all to do with keyTyped. It's a separate input-action model altogether.
http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html

ok, thanks for the help

Actually, you don't even need to put them on the top-level window. I had a slightly different scenario in mind when I suggested that. You can bind them to whatever panel you are drawing to for the game screen. Perhaps this tiny example will help

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class KeyDemo extends JFrame {

    public KeyDemo(){
        super();
        DisplayPanel display = new DisplayPanel();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(display, BorderLayout.CENTER);
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 150);
        setVisible(true);
    }
    
    class DisplayPanel extends JPanel{
        private Ellipse2D.Float ellipse;
        
        public DisplayPanel(){
            super();
            getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),"MoveLeft");
            getActionMap().put("MoveLeft", new MoveLeftAction());
            
            getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),"MoveRight");
            getActionMap().put("MoveRight", new MoveRightAction());
        }
        

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setBackground(Color.WHITE);
            g2.clearRect(0, 0, getWidth(), getHeight());
            
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.BLUE);
            if (ellipse==null){
                ellipse = new Ellipse2D.Float(
                    (getWidth()-50)/2,(getHeight()-50)/2,50,50);
            }
            g2.fill(ellipse);
        }
        
        class MoveLeftAction extends AbstractAction{
            public void actionPerformed(ActionEvent e) {
                if (ellipse.x>2){
                    ellipse.x -= 2;
                    repaint();
                }
            }
        }
        
        class MoveRightAction extends AbstractAction{
            public void actionPerformed(ActionEvent e) {
                if (ellipse.x < (getWidth()-ellipse.width-2)){
                    ellipse.x += 2;
                    repaint();
                }
            }
        }
    }
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new KeyDemo();
            }
        });
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.