gpt4 book ai didi

java - 发送 key Java 代码无法在 Mac 上运行

转载 作者:行者123 更新时间:2023-12-02 09:14:34 24 4
gpt4 key购买 nike

此代码用于 robotsframework 中的自动化目的。在取景器窗口上单击鼠标右键后,我想发送“e”、“d”等键并从上下文菜单中选择一个项目。这是Java代码:

    public void sendKeys(String text) throws Exception {
int len = text.length();
char curr;
for (int index=0; index<len; index++) {
curr = text.toUpperCase().charAt(index);
assertTrue(
String.format("'%c' is not an alphanumeric character", curr),
(curr>=KeyEvent.VK_A && curr<=KeyEvent.VK_Z) || (curr>=KeyEvent.VK_0 && curr<=KeyEvent.VK_9)
);
robot.keyPress(curr);
robot.keyRelease(curr);
}
System.out.println("Send Keys "+text);
}

在 Windows(几乎所有版本)上,我完全能够发送 key ,但在 Mac Sierra/El Capitan 上,它不会抛出错误,但实际上会选择文件。我尝试了发送键输入的不同组合,例如“e”、“d”、“o”等,使用所有这些输入选择文件,而不是实际选择上下文菜单上的项目。

PS:上下文菜单包含我在输入中用于发送键的项目。

最佳答案

这是我使用的:

public static void enterString(String value)
{
String Keyvalue=value.toUpperCase();
//System.out.println(Keyvalue);
for (int i=0;i<Keyvalue.length();i++)
{
//System.out.println(Keyvalue.charAt(i));
if(Keyvalue.charAt(i) == ':')
{
System.out.println("its a colon");
RoboKey.keyPress(KeyEvent.VK_SHIFT);
RoboKey.keyPress(KeyEvent.VK_SEMICOLON);
RoboKey.keyRelease(KeyEvent.VK_SEMICOLON);
RoboKey.keyRelease(KeyEvent.VK_SHIFT);

}
else if (Keyvalue.charAt(i) == '.')
{
RoboKey.keyPress(KeyEvent.VK_PERIOD);
}
else if (Keyvalue.charAt(i) == '_')
{
RoboKey.keyPress(KeyEvent.VK_SHIFT);
RoboKey.keyPress(KeyEvent.VK_MINUS);
RoboKey.keyRelease(KeyEvent.VK_MINUS);
RoboKey.keyRelease(KeyEvent.VK_SHIFT);
}
else
{
RoboKey.keyPress((int)Keyvalue.charAt(i));
}
}
}

关于java - 发送 key Java 代码无法在 Mac 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com