robot.keyRelease(KeyEvent.VK_CAPS_LOCK);进行大小写切换成大写,这个时候大小写锁定键的那个灯亮了,表明当前已经成功切换成大写,但这个时候再去通过代码去获取大小写状态,发现还是小写状态,可这时明明已经是大写了啊,为什么获取的值有问题?
3 个解决方案
#1
完整代码如下
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
public class Test {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
System.out.println("按下前" + isUpperLetter());
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
robot.waitForIdle();
System.out.println("按下后" + isUpperLetter());
}
private static boolean isUpperLetter() {
Toolkit.getDefaultToolkit().setDynamicLayout(false);
return Toolkit.getDefaultToolkit().getLockingKeyState(
KeyEvent.VK_CAPS_LOCK);
}
}
#2
上面多了一句无用代码
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
public class Test {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
System.out.println("按下前" + isUpperLetter());
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
robot.waitForIdle();
System.out.println("按下后" + isUpperLetter());
}
private static boolean isUpperLetter() {
return Toolkit.getDefaultToolkit().getLockingKeyState(
KeyEvent.VK_CAPS_LOCK);
}
}
#3
#1
完整代码如下
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
public class Test {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
System.out.println("按下前" + isUpperLetter());
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
robot.waitForIdle();
System.out.println("按下后" + isUpperLetter());
}
private static boolean isUpperLetter() {
Toolkit.getDefaultToolkit().setDynamicLayout(false);
return Toolkit.getDefaultToolkit().getLockingKeyState(
KeyEvent.VK_CAPS_LOCK);
}
}
#2
上面多了一句无用代码
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
public class Test {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
System.out.println("按下前" + isUpperLetter());
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
robot.waitForIdle();
System.out.println("按下后" + isUpperLetter());
}
private static boolean isUpperLetter() {
return Toolkit.getDefaultToolkit().getLockingKeyState(
KeyEvent.VK_CAPS_LOCK);
}
}