2017-06-03 98 views
0

我是新来的关键事件,并试图找出如何按空格键时调用一个函数。我包含了一些我想要做的伪代码。使用按键事件调用函数

public class scrap implements KeyListener { 
 
    
 
    jump() { 
 
     /////////// 
 
     ///code//// 
 
     /////////// 
 
    } 
 
    
 
\t @Override 
 
\t public void keyPressed(KeyEvent e) { 
 
\t \t  if(e == Space) { 
 
      jump() 
 
\t \t  } 
 
\t } 
 

 
\t @Override 
 
\t public void keyReleased(KeyEvent e) { 
 
\t \t  // TODO Auto-generated method stub 
 
    \t } 
 

 
\t @Override 
 
\t public void keyTyped(KeyEvent e) { 
 
\t \t  // TODO Auto-generated method stub 
 
\t } 
 
}

+2

的可能的复制[呼叫函数需要一个KeyEvent参数](https://stackoverflow.com/questions/33263360/call-a-function-that-re奎雷斯-A-KeyEvent的参数) –

回答

1

如何写一个按键侦听器:

public class KeyEventDemo ... implements KeyListener ... { 
...//where initialization occurs: 
    typingArea = new JTextField(20); 
    typingArea.addKeyListener(this); 

    //Uncomment this if you wish to turn off focus 
    //traversal. The focus subsystem consumes 
    //focus traversal keys, such as Tab and Shift Tab. 
    //If you uncomment the following line of code, this 
    //disables focus traversal and the Tab events 
    //become available to the key event listener. 
    //typingArea.setFocusTraversalKeysEnabled(false); 
... 
/** Handle the key typed event from the text field. */ 
public void keyTyped(KeyEvent e) { 
    displayInfo(e, "KEY TYPED: "); 
} 

/** Handle the key-pressed event from the text field. */ 
public void keyPressed(KeyEvent e) { 
    displayInfo(e, "KEY PRESSED: "); 
} 

/** Handle the key-released event from the text field. */ 
public void keyReleased(KeyEvent e) { 
    displayInfo(e, "KEY RELEASED: "); 
} 
... 
private void displayInfo(KeyEvent e, String keyStatus){ 

    //You should only rely on the key char if the event 
    //is a key typed event. 
    int id = e.getID(); 
    String keyString; 
    if (id == KeyEvent.KEY_TYPED) { 
     char c = e.getKeyChar(); 
     keyString = "key character = '" + c + "'"; 
    } else { 
     int keyCode = e.getKeyCode(); 
     keyString = "key code = " + keyCode 
       + " (" 
       + KeyEvent.getKeyText(keyCode) 
       + ")"; 
    } 

    int modifiersEx = e.getModifiersEx(); 
    String modString = "extended modifiers = " + modifiersEx; 
    String tmpString = KeyEvent.getModifiersExText(modifiersEx); 
    if (tmpString.length() > 0) { 
     modString += " (" + tmpString + ")"; 
    } else { 
     modString += " (no extended modifiers)"; 
    } 

    String actionString = "action key? "; 
    if (e.isActionKey()) { 
     actionString += "YES"; 
    } else { 
     actionString += "NO"; 
    } 

    String locationString = "key location: "; 
    int location = e.getKeyLocation(); 
    if (location == KeyEvent.KEY_LOCATION_STANDARD) { 
     locationString += "standard"; 
    } else if (location == KeyEvent.KEY_LOCATION_LEFT) { 
     locationString += "left"; 
    } else if (location == KeyEvent.KEY_LOCATION_RIGHT) { 
     locationString += "right"; 
    } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) { 
     locationString += "numpad"; 
    } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN) 
     locationString += "unknown"; 
    } 

    ...//Display information about the KeyEvent... 
    } 
} 

我发现这可能有助于您Complete Documentation

0

键盘每个键都有唯一的密钥code.So你可以检查按下的按键,如果它等于32(按键代码的空间按钮),你可以打电话 方法。

像这样:

@Override 
public void keyPressed(KeyEvent e) { 
    if(e.getKeyCode() == 32){ 
     jump(); 
    } 
} 
public void jump(){ 
    System.out.println("Jump"); 
} 

或者,您可以从VK_SPACE

if(e.getKeyCode() == KeyEvent.VK_SPACE){ 
    jump(); 
} 

那么你可以添加动作到您的按钮,如:

scrap s = new scrap(); 
buttonVar.addKeyListener(s);