这是我迄今为止所拥有的......我正在使用它作为键绑定。我想在应用程序运行时按“向下”箭头键,然后使其自动按下“向下”箭头键3次,然后在代码中按“Enter”键完成。我知道这不是最好的解释,但很抱歉,如果你还是不明白,我会尽力解释一下。
以下是代码:
public class MyKeyListener extends KeyAdapter{
@Override
public void keyPressed(KeyEvent evt){
}
KeyListener keyListener = new KeyListener() {
@Override
public void keyPressed(KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
if(keyCode == 1005){
System.out.println("So far, so good..");
}
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
我认为关键箭头是1005。 我到目前为止写了System.out.println(“到目前为止,很好......”);当我按下向下箭头键时,看看应用程序是否检测到它,但它没有......
有什么想法吗?
答案 0 :(得分:5)
我只是把它扔到那里因为我觉得它对你有用,这是一个虚拟的Java键盘,修改过我几年前在网上找到的一些资源,我不再记得链接到: / p>
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Keyboard {
public void main (String[] args) throws InterruptedException {
while(true) {
type("
");
}
}
private static Robot robot;
static Random rand = new Random();
public Keyboard() throws AWTException {
this.robot = new Robot();
}
public Keyboard(Robot robot) {
this.robot = robot;
}
public void type(CharSequence characters) throws InterruptedException {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
type(character);
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
public void type(char character) {
switch (character) {
case 'a': doType(KeyEvent.VK_A); break;
case 'b': doType(KeyEvent.VK_B); break;
case 'c': doType(KeyEvent.VK_C); break;
case 'd': doType(KeyEvent.VK_D); break;
case 'e': doType(KeyEvent.VK_E); break;
case 'f': doType(KeyEvent.VK_F); break;
case 'g': doType(KeyEvent.VK_G); break;
case 'h': doType(KeyEvent.VK_H); break;
case 'i': doType(KeyEvent.VK_I); break;
case 'j': doType(KeyEvent.VK_J); break;
case 'k': doType(KeyEvent.VK_K); break;
case 'l': doType(KeyEvent.VK_L); break;
case 'm': doType(KeyEvent.VK_M); break;
case 'n': doType(KeyEvent.VK_N); break;
case 'o': doType(KeyEvent.VK_O); break;
case 'p': doType(KeyEvent.VK_P); break;
case 'q': doType(KeyEvent.VK_Q); break;
case 'r': doType(KeyEvent.VK_R); break;
case 's': doType(KeyEvent.VK_S); break;
case 't': doType(KeyEvent.VK_T); break;
case 'u': doType(KeyEvent.VK_U); break;
case 'v': doType(KeyEvent.VK_V); break;
case 'w': doType(KeyEvent.VK_W); break;
case 'x': doType(KeyEvent.VK_X); break;
case 'y': doType(KeyEvent.VK_Y); break;
case 'z': doType(KeyEvent.VK_Z); break;
case 'A': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A); break;
case 'B': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B); break;
case 'C': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C); break;
case 'D': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D); break;
case 'E': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E); break;
case 'F': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F); break;
case 'G': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G); break;
case 'H': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H); break;
case 'I': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I); break;
case 'J': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J); break;
case 'K': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K); break;
case 'L': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L); break;
case 'M': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M); break;
case 'N': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N); break;
case 'O': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O); break;
case 'P': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P); break;
case 'Q': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q); break;
case 'R': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R); break;
case 'S': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S); break;
case 'T': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T); break;
case 'U': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U); break;
case 'V': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V); break;
case 'W': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W); break;
case 'X': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X); break;
case 'Y': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y); break;
case 'Z': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z); break;
case '`': doType(KeyEvent.VK_BACK_QUOTE); break;
case '0': doType(KeyEvent.VK_0); break;
case '1': doType(KeyEvent.VK_1); break;
case '2': doType(KeyEvent.VK_2); break;
case '3': doType(KeyEvent.VK_3); break;
case '4': doType(KeyEvent.VK_4); break;
case '5': doType(KeyEvent.VK_5); break;
case '6': doType(KeyEvent.VK_6); break;
case '7': doType(KeyEvent.VK_7); break;
case '8': doType(KeyEvent.VK_8); break;
case '9': doType(KeyEvent.VK_9); break;
case '-': doType(KeyEvent.VK_MINUS); break;
case '=': doType(KeyEvent.VK_EQUALS); break;
case '~': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_QUOTE); break;
case '!': doType(KeyEvent.VK_EXCLAMATION_MARK); break;
case '@': doType(KeyEvent.VK_AT); break;
case '#': doType(KeyEvent.VK_NUMBER_SIGN); break;
case '$': doType(KeyEvent.VK_DOLLAR); break;
case '%': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5); break;
case '^': doType(KeyEvent.VK_CIRCUMFLEX); break;
case '&': doType(KeyEvent.VK_AMPERSAND); break;
case '*': doType(KeyEvent.VK_ASTERISK); break;
case '(': doType(KeyEvent.VK_LEFT_PARENTHESIS); break;
case ')': doType(KeyEvent.VK_RIGHT_PARENTHESIS); break;
case '_': doType(KeyEvent.VK_UNDERSCORE); break;
case '+': doType(KeyEvent.VK_PLUS); break;
case '\t': doType(KeyEvent.VK_TAB); break;
case '\n': doType(KeyEvent.VK_ENTER); break;
case '[': doType(KeyEvent.VK_OPEN_BRACKET); break;
case ']': doType(KeyEvent.VK_CLOSE_BRACKET); break;
case '\\': doType(KeyEvent.VK_BACK_SLASH); break;
case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break;
case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break;
case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break;
case ';': doType(KeyEvent.VK_SEMICOLON); break;
case ':': doType(KeyEvent.VK_COLON); break;
case '\'': doType(KeyEvent.VK_QUOTE); break;
case '"': doType(KeyEvent.VK_QUOTEDBL); break;
case ',': doType(KeyEvent.VK_COMMA); break;
case '<': doType(KeyEvent.VK_LESS); break;
case '.': doType(KeyEvent.VK_PERIOD); break;
case '>': doType(KeyEvent.VK_GREATER); break;
case '/': doType(KeyEvent.VK_SLASH); break;
case '?': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SLASH); break;
case ' ': doType(KeyEvent.VK_SPACE); break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
private static void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
private static void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}
要根据您的需要进行修改,您可以:
for(int i = 0; i != 3; ++i) {
doType(KeyEvent.VK_DOWN);
}
doType(KeyEvent.VK_DOWN);会按下一次,然后释放它。
您可能会在代码中的“到目前为止,那么好......”部分之后插入它。
希望我帮忙!
答案 1 :(得分:3)
您可以改为使用键绑定。该教程是here,直接来自Sun Java教程。他们是一种更新(并且我认为更好)的方式来实现上述目标。
如果您开始使用密钥监听器,请转到this tutorial。它解释了如何做到这一点。
我从来没有按照你的方式创建一个监听器,所以不是100%确定出了什么问题,但我认为这是因为你正在创建一个类来监听关键事件,但是你需要在某个地方使用那个类否则,它不只是去。但不确定。
更新:如上所述,在使用您的密钥监听器的类中,您要添加它吗? addKeyListener( new MyKeyListener() );
答案 2 :(得分:0)
KeyEvent包含键代码常量(静态int),如VK_DOWN,.VK_A,VK_0等。 如果您使用的是eclipse之类的IDE,它会在您开始输入时显示一个列表。 或者您可以在KeyEvent field summary中查看完整列表。 这可以简化您未来程序中的这种编码。
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == e.VK_DOWN){
System.out.println("So far, so good..");
}
}
答案 3 :(得分:0)
对于对全球事件处理感兴趣的人,我完全推荐我最初发布的答案: How to detect a key press in Java