Java 中的按键问题
Trouble with key pressings in Java
这里已经有很多 "key pressing in Java" 个问题,所以我确实阅读了其中的一些(实际上从一个那里得到了一段代码)。不回答我一直遇到的问题。我发现这段代码可以检测某个键是否被按下以及它是否被释放但是我无法摆脱这个错误
在这种情况下,我正在测试是否按下 'W'
代码:
private static boolean IsPressing(String string) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent ke) {
synchronized (IsKeyPressed.class) {
boolean wPressed;
switch (ke.getID()) {
case KeyEvent.KEY_PRESSED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = true;
}
break;
case KeyEvent.KEY_RELEASED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = false;
}
break;
}
return false;
}
}
});
return false;
}
}
所以基本上在第六行 (synchronized (IsKeyPressed.class) {) 它告诉我 "IsKeyPressed.class cannot be resolved to a type",然后我得到四个 eclipse 选项:
1.Create Class
2.Create 界面
3.Create枚举
4.Fix 项目
我认为前三个没有帮助我实现检测是否按下某个键的目标,最后一个什么也没做
我做错了什么?
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
************************************************ *你需要这个*********************************************
public class IsKeyPressed {
private static boolean wPressed = false;
public static boolean isWPressed() {
synchronized (IsKeyPressed.class) {
return wPressed;
}
}
public static void main(String[] args) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent ke) {
synchronized (Test.class) {
switch (ke.getID()) {
case KeyEvent.KEY_PRESSED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = true;
System.out.println("test");
}
break;
case KeyEvent.KEY_RELEASED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = false;
System.out.println("test1");
}
break;
}
return false;
}
}
});
JFrame test = new JFrame();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
那么您可以随时使用:
//////////////This code goes in method//////////////////////
if (IsKeyPressed.isWPressed()) {
// do your thing.
}
/////////////////////////////////////////////////////////////////////
此代码取自 here
这里已经有很多 "key pressing in Java" 个问题,所以我确实阅读了其中的一些(实际上从一个那里得到了一段代码)。不回答我一直遇到的问题。我发现这段代码可以检测某个键是否被按下以及它是否被释放但是我无法摆脱这个错误
在这种情况下,我正在测试是否按下 'W'
代码:
private static boolean IsPressing(String string) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent ke) {
synchronized (IsKeyPressed.class) {
boolean wPressed;
switch (ke.getID()) {
case KeyEvent.KEY_PRESSED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = true;
}
break;
case KeyEvent.KEY_RELEASED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = false;
}
break;
}
return false;
}
}
});
return false;
}
}
所以基本上在第六行 (synchronized (IsKeyPressed.class) {) 它告诉我 "IsKeyPressed.class cannot be resolved to a type",然后我得到四个 eclipse 选项:
1.Create Class 2.Create 界面 3.Create枚举 4.Fix 项目
我认为前三个没有帮助我实现检测是否按下某个键的目标,最后一个什么也没做
我做错了什么?
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
************************************************ *你需要这个*********************************************
public class IsKeyPressed {
private static boolean wPressed = false;
public static boolean isWPressed() {
synchronized (IsKeyPressed.class) {
return wPressed;
}
}
public static void main(String[] args) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent ke) {
synchronized (Test.class) {
switch (ke.getID()) {
case KeyEvent.KEY_PRESSED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = true;
System.out.println("test");
}
break;
case KeyEvent.KEY_RELEASED:
if (ke.getKeyCode() == KeyEvent.VK_W) {
wPressed = false;
System.out.println("test1");
}
break;
}
return false;
}
}
});
JFrame test = new JFrame();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
那么您可以随时使用:
//////////////This code goes in method//////////////////////
if (IsKeyPressed.isWPressed()) {
// do your thing.
}
/////////////////////////////////////////////////////////////////////
此代码取自 here