سؤال

I have had trouble figuring out why the KeyEvents, which in theory would when pressed/released, will not trigger and have had some finding an answer on the web that satisfy the issue.

When ever any of the four implemented keys are pressed/released nothing happens.

(I cut down the code to what I think was need, but don't mind posting it all)

//Outter Class imports
import javax.swing.*;

import java.util.*;
import java.util.ArrayList;

import java.awt.*;
import java.awt.Image;
import java.awt.image.*;
import javax.imageio.*;


//Inner Class(1) imports
import java.awt.event.*;    
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Game extends JPanel {

      //fields...


    public Game(){
        GWin = new JFrame("Space Invaders");
        JPanel panel = new JPanel();
        panel.setSize(new Dimension(Length, Height));
        panel.setLayout(null);
        addKeyListener(new KeyInputHandler());
        GWin.add(panel);

        WindowListener exitListener = new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int confirm = JOptionPane.showOptionDialog(null, "Are You Sure to Close Application?", "Exit Confirmation",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);

                if (confirm == 0) {
                   GWin.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);      
                }  
            }
        };

        GWin.addWindowListener(exitListener);

        GWin.setExtendedState(Frame.MAXIMIZED_BOTH); 
        GWin.setUndecorated(true);  

        GWin.setVisible(true);
        requestFocus();

        startGame();
    }

    public static void main(String arg[]) { 
        SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new WIN(Length ,Height);
                }
            });
         }

    private class KeyInputHandler extends KeyAdapter {
    /**
     * A class to handle keyboard input from the user. The class
     * handles both dynamic input during game play, i.e. left/right 
     * and shoot, and more static type input (i.e. press any key to
     * continue)
     */
        @Override 
        public void keyPressed(KeyEvent e) {
            if (waitingForKeyPress) {
                return;
            }   

            int key = e.getKeyCode();
            if (key == KeyEvent.VK_A){
                leftPressed = true;
            }
            if (key == KeyEvent.VK_F) {
                    rightPressed = true;
            }
            if (key == KeyEvent.VK_SPACE) {
                    firePressed = true;                                                                                                                                                                                                                                                                                     
            }
        }

        @Override 
        public void keyReleased(KeyEvent e) {
            if (waitingForKeyPress) {
                return;
            }   

            int key = e.getKeyCode();

            if (key == KeyEvent.VK_A) {
                    leftPressed = false;
            }
            if (key == KeyEvent.VK_F) {
                    rightPressed = false;
            }
            if (key == KeyEvent.VK_SPACE) {
                    firePressed = false;                                                                                                                                                                                                                                                                                        
            }
        }

        @Override
        public void keyTyped(KeyEvent e){   
            int key = e.getKeyCode();
            if (waitingForKeyPress) {
                if (pressCount == 1) {
                    waitingForKeyPress = false;
                    pressCount = 0;
                } else {
                    pressCount++;
                }
            }

            if (e.getKeyChar() == 27) {
                GWin.dispose();
            }
        }
    }
}
هل كانت مفيدة؟

المحلول

My first guess would that your JPanel descendant Game is not yet focusable. Try:

this.setFocusable(true);
this.requestFocus();

before requesting the focus. Also be sure to read Oracle's documentation on how Swing focus management works - it will safe you a lot of time because there are more pitfalls (asynchronous behaviour...) :-)!

Good luck!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top