0

I will try to provide as much detail as i can here. Please note I haven't posted to this forum before, so if this is a duplicate question, my apologies.

I have a mid-2010 MacBook Pro and run OSx Mavericks. I recently (last week) installed the standard edition of Windows 8.1 using bootcamp. The install went well, and everything is working ok; however....

I am a student of Java, and recently wrote a swing component for the game Sokoban. It runs very well on my Mac, and in Windows 7.....but it is not working on my Mac when in Windows 8.1. When I invoke the main method from either BlueJ or Eclipse, the program runs, but my keys are not responding. I know my keyboard works, as I have been able to use other programs wirhout issue. That said, I have narrowed my focus to this being a Java compatibility issue with Windows 8.1....am I correct? Here is my Key Listener from Sokoban. Keys are enumerated in a separate enum. I use the up, down, left, right, r, and n keys only.

/*
 * This is the key listener for the class Sokoban.
 */
public class MyKeyListener implements KeyListener //Inner class that contains the key     listener.
    {
       public void keyPressed(KeyEvent event) 
       {
         if(isReady) { //Prevents actions from occuring and graphics being drawn before the level contents are loaded into gamePlay
           int decimalCode = event.getKeyCode(); // get key code of the key pressed
           //Find the current position of the PLAYER
           for(int i = 0; i < curLevHt; i++) { 
               for(int j = 0; j < curLevWd; j++) {
                   if (gamePlay[i][j] == Contents.PLAYER || gamePlay[i][j] == Contents.PLAYERONGOAL) {
                       x = i; //assign the array coordinates to x and y
                       y = j;
                    }
                }
           }
           //For the given arrow key press, assign the appropriate offsets to dx, ddx, dy, and ddy; then call method gameMove().
           if(decimalCode == Keys.UPARROW.value && !gameComplete) {dx = x - 1; dy = y; ddx = dx -1; ddy = dy; gameMove();}//Move up offset
           if(decimalCode == Keys.DOWNARROW.value && !gameComplete) {dx = x + 1; dy = y; ddx = dx + 1; ddy = dy; gameMove();}//Move down offset
           if(decimalCode == Keys.LEFTARROW.value && !gameComplete) {dx = x; dy = y - 1; ddx = dx; ddy = dy -1; gameMove();}//Move left offset
           if(decimalCode == Keys.RIGHTARROW.value && !gameComplete) {dx = x; dy = y + 1; ddx = dx; ddy = dy + 1; gameMove();}//Move right offset
           //Increment to the next level of play when N key is pressed OR if a level is completed by calling method nextLevel().
           if((goal || decimalCode == Keys.N.value) && currentLevel < numberOfLevels - 1) {nextLevel();}
           //Reset the current level of play when R key is pressed by calling the method levelReset();
           if(decimalCode == Keys.R.value && !gameComplete) {levelReset();}
           //If last level of game is successfully completed turn gameComplete true, and turn on a "splash screen"
           if(goal && currentLevel == numberOfLevels - 1) { //Game over.
               gameComplete = true;
               levelDesc.setText("GAME OVER, Press N to Restart");
               timer.stop();//Stop the timer at the completion of the last level in the game.
               repaint();
           }
           if(goal && decimalCode == Keys.N.value && currentLevel == numberOfLevels -1) {restartGame();}
         }
       }
       public void keyReleased(KeyEvent event){
       }
       public void keyTyped(KeyEvent event){
       }
    }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Mike
  • 1
  • 1
  • 1
    The usual answer for similar questions is to avoid using KeyListeners and instead use Key Bindings. I recommend that you check out the key bindings tutorials (first hit on Google). Also, if you search on this term and my user name/number, you'll find numerous answers and code showing how to use them for similar situations. – Hovercraft Full Of Eels Apr 13 '14 at 15:04
  • This is why things like [libGDX](http://libgdx.badlogicgames.com/) were invented, might help you in the future with cross platform. – CodeCamper Apr 13 '14 at 15:06
  • For example check these answers: http://stackoverflow.com/a/8961998/522444, http://stackoverflow.com/a/6887354/522444, http://stackoverflow.com/a/12545773/522444 – Hovercraft Full Of Eels Apr 13 '14 at 15:16
  • possible duplicate of [Java KeyListener Not Registering Arrow Keys](http://stackoverflow.com/questions/8961938/java-keylistener-not-registering-arrow-keys) – Hovercraft Full Of Eels Apr 13 '14 at 16:42
  • Thank you for your comments; I will look more into key binding later on....however I am looking to know why my KeyListener doesn't work in Windows 8.1. The way it currently written is acceptable and functions in Windows 7 and in Mac OSX Mavericks. Is there a known issue with Java and Windows 8.1 with regards to key events? – Mike Apr 14 '14 at 14:58
  • I've discovered the issue is with the IDE. I was using BlueJ 3.1.0 and JDK 1.8 on my Mac, and version 3.1.1 and JDK 1.8 on my Windows. When I upgraded BlueJ to 3.1.1 on my Mac, I encountered the same issue. I reverted to version 3.1.0 on both OS's and the issue is now gone. – Mike Apr 16 '14 at 02:13

0 Answers0