By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,825 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,825 IT Pros & Developers. It's quick & easy.

Keyboard isn't read

P: 1
I'm attempting to move a character in Java, hoping to make it into a game, but the keyboard doesn't function. I've looked over all the code, and nothing seems to be wrong. I'm about to give up. Can anyone help?
Expand|Select|Wrap|Line Numbers
  1. InputManager.java
  2. import java.awt.*;
  3.  
  4. //...
  5.  
  6. public class InputManager implements KeyListener, MouseListener,
  7.     MouseMotionListener, MouseWheelListener{
  8.  
  9. //...
  10.  
  11.     public InputManager(Component comp){
  12.         //...
  13.     }//ends Imput Manager constructor
  14.  
  15. //...
  16.  
  17.     public void clearMap(GameAction gameAction){
  18.         for(int i=0;i<keyActions.length;i++){
  19.             if(keyActions[i]==gameAction){
  20.                 keyActions[i]=null;
  21.             }
  22.  
  23.         }
  24.         gameAction.reset();
  25.  
  26.     }//ends clearMap
  27.  
  28.     public List getMaps(GameAction gameCode){
  29.         ArrayList list = new ArrayList();
  30.         for(int i=0;i<keyActions.length;i++){
  31.             if(keyActions[i]==gameCode){
  32.                 list.add(getKeyName(i));
  33.             }
  34.         }
  35.         for(int i=0;i<mouseActions.length;i++){
  36.             if(mouseActions[i]==gameCode){
  37.                 list.add(getMouseName(i));
  38.             }
  39.         }
  40.         return list;
  41.     }//ends getMaps
  42.  
  43.     public void resetAllGameActions(){
  44.         for(int i=0;i<keyActions.length;i++){
  45.             if(keyActions[i]!=null){
  46.                 keyActions[i].reset();
  47.             }
  48.         }
  49.         for(int i=0;i<mouseActions.length;i++){
  50.             if(mouseActions[i]!=null){
  51.                 mouseActions[i].reset();
  52.             }
  53.         }
  54.  
  55.     }//ends resetAllGameActions
  56.  
  57.     public static String getKeyName(int keyCode){
  58.         return KeyEvent.getKeyText(keyCode);
  59.     }//ends getKeyName
  60.  
  61. //...
  62.  
  63.     private GameAction getKeyAction(KeyEvent e){
  64.         int keyCode=e.getKeyCode();
  65.         if(keyCode<keyActions.length){
  66.             return keyActions[keyCode];
  67.         }
  68.         else { 
  69.             return null;
  70.         }
  71.     }//ends getKeyAction
  72.  
  73. //...
  74.  
  75.     public void keyPressed(KeyEvent e){
  76.         GameAction gameAction=getKeyAction(e);
  77.         if(gameAction!=null){
  78.             gameAction.release();
  79.         }
  80.         e.consume();
  81.     }//ends keyPressed
  82.  
  83.     public void keyReleased(KeyEvent e){
  84.         GameAction gameAction=getKeyAction(e);
  85.         if(gameAction!=null){
  86.             gameAction.release();
  87.         }
  88.         e.consume();
  89.     }//ends keyReleased
  90.  
  91. //...
  92.  
  93. }//ends InputManager
  94.  
  95. // and more...
  96.  
Nov 20 '08 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Did you add the KeyListener to some component?
Nov 21 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.