机器人类java,输入字符串问题
我正在使用以下循环,但它只输入第一个字符,其余的作为数字,知道吗?
import java.awt.*;
import javax.swing.KeyStroke;
public class test {
public static void main(String[] args) throws AWTException
{
Robot r = new Robot();
String s = "Face";
for (int i = 0; i < s.length(); i++)
{
char res = s.charAt(i);
r.keyPress(res);
r.keyRelease(res);
r.delay(1000);
}
}
}
输出输入:F135
I m using the following loop , but its only typing the first charecter and the rest as numbers, any idea ?
import java.awt.*;
import javax.swing.KeyStroke;
public class test {
public static void main(String[] args) throws AWTException
{
Robot r = new Robot();
String s = "Face";
for (int i = 0; i < s.length(); i++)
{
char res = s.charAt(i);
r.keyPress(res);
r.keyRelease(res);
r.delay(1000);
}
}
}
OUTPUT typing : F135
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
keyPress/Release 方法需要一个 int 值来表示您要键入的字符。这些值是由 KeyEvent.VK_??? 确定的每个字符的键码。变量。
尝试:
但是,即使这样也不适用于所有角色。例如,在我的键盘上,“%”位于“5”上方。您不能使用 VK_PERCENT。所需的击键是 VK_5 以及 Shift。无法知道键盘的实际映射来自动执行此操作。
所以机器人并不是做到这一点的好方法。
The keyPress/Release methods need an int value that represents the character you want to type. These value are the key code for each character as determined by the KeyEvent.VK_??? variables.
Try:
However, even this won't work for all characters. For example on my keyboard the "%" is above the "5". You can't use VK_PERCENT. The key stroke needed is VK_5 along with a shift. There is no way to know the actual mapping of your keyboard to do this automatically.
So a Robot is not a good way to do this.
Robot 类使用此处定义的关键代码: http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/KeyEvent.html,不是原始字符。你需要这样称呼它:
The Robot class uses key codes which are defined here: http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/KeyEvent.html, not raw characters. You need to call it like this:
@camickr 我对你的 scipt 做了一个小编辑,以容纳一个字符串而不是一个字母。请在下面找到。它帮助了我谢谢:)函数调用:
typeCharacter(robot, "thanks");
@ camickr I made a small edit to your scipt to accomodate a string rather than a letter . Please find below . It helped me Thanks :) function call :
typeCharacter(robot, "thanks");
您还可以将字符串加载到剪贴板,然后将其粘贴到您想要的位置。
You could also load your String to the clipboard and just paste it where ever you want to.
如果您创建一个接收要输入的字符串的函数,您可以让您的生活变得轻松:
You can make your life easy if you create a function receiving a string to be typed: