Java 使用键事件调用函数
我对按键事件不太熟悉,我正试图弄清楚当按下空格键时如何调用函数。我包含了一些伪代码,用于我正在尝试做的事情Java 使用键事件调用函数,java,Java,我对按键事件不太熟悉,我正试图弄清楚当按下空格键时如何调用函数。我包含了一些伪代码,用于我正在尝试做的事情 public类实现KeyListener{ 跳转(){ /////////// ///代码//// /////////// } @凌驾 按下公共无效键(按键事件e){ if(e==空格){ 跳转() } } @凌驾 公共无效密钥已释放(密钥事件e){ //TODO自动生成的方法存根 } @凌驾 public void keyTyped(KeyEvent e){ //TODO自动生成的方法
public类实现KeyListener{
跳转(){
///////////
///代码////
///////////
}
@凌驾
按下公共无效键(按键事件e){
if(e==空格){
跳转()
}
}
@凌驾
公共无效密钥已释放(密钥事件e){
//TODO自动生成的方法存根
}
@凌驾
public void keyTyped(KeyEvent e){
//TODO自动生成的方法存根
}
}
您可以编写密钥侦听器:
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...
}
}
我发现这可能对您有所帮助键盘上的每个键都有唯一的键代码。因此,您可以检查是否按下了键,如果它等于
32
(空格键的键代码),您可以调用该方法
像这样:
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == 32){
jump();
}
}
public void jump(){
System.out.println("Jump");
}
或者您可以从VK\u空间中进行检查:
if(e.getKeyCode() == KeyEvent.VK_SPACE){
jump();
}
然后,您可以向按钮添加操作,如:
scrap s = new scrap();
buttonVar.addKeyListener(s);
可能重复的