我想发送som下划线。但它会重播错误。说:
_ 上述字符的密钥代码无效
此错误是默认情况下的sendet: 默认: 抛出新的IllegalArgumentException("不能输入字符" +字符);
我的环境:
IntelliJ IDEA 2018.1(终极版) JRE:1.8.0_152-release-1136-b20 amd64 JVM:JetBrains s.r.o的OpenJDK 64位服务器VM Windows 10 10.0
BTW我现在(28.4.2018 20:38)将它转换为kotlin我得到了同样的错误:https://gist.github.com/f61039faddaad67eaaf8c825f94934c4
来源:
import java.awt.*;
import java.awt.event.KeyEvent;
//
public class Keyboard {
private Robot robot;
public static void main(String... args) throws Exception
{
Keyboard keyboard = new Keyboard();
keyboard.type("_");
}
public Keyboard() throws AWTException
{
this.robot = new Robot();
robot.delay(500);
robot.setAutoDelay(2);
robot.setAutoWaitForIdle(true);
}
public Keyboard(Robot robot)
{
this.robot = robot;
}
public void type(CharSequence characters)
{
int length = characters.length();
for (int i = 0; i < length; i++)
{
char character = characters.charAt(i);
type(character);
}
}
public void enter()
{
robot.keyPress(KeyEvent.VK_ENTER);
}
public void type(char character){
System.out.println(character);
switch (character) {
case '_': doType(KeyEvent.VK_UNDERSCORE); break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
private void doType(int keyCode){
try{
robot.keyPress(keyCode);
robot.keyRelease(keyCode);
}
catch(Exception e){
System.out.println("Invalid key code(s) for above character");
}
}
private void doType(int keyCode1, int keyCode2){
try{
robot.keyPress(keyCode1);
robot.keyPress(keyCode2);
robot.keyRelease(keyCode2);
robot.keyRelease(keyCode1);
}
catch(Exception e){
System.out.println("Invalid key code(s) for above character");
}
}
}