执行从 JSON 读取的事件
execute events read from JSON
我正在从 JSON 文件中读取两件事:
actionOnPress:"robot.KeyPress(e)"
(机器人是一个 java.awt.robot 实例)和
event:"KeyEvent.VK_4"
我要执行
robot.KeyPress(KeyEvent.VK_4)
执行此操作的最简单方法(最好不需要下载库)是什么?此代码应该也适用于 robot.mousePress 和 robot.mouseMove 等
我已经用 ScriptEngine 尝试过不同的东西,但 none 似乎有效。
非常感谢,Kamik423
编辑:应该是通用的。用户应该能够指定不同的事件,例如 FOR EXAMPLE robot
好的,自己解决了。这是代码:
package test;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class executor extends JFrame {
String import1 = "java.awt.Robot";
String import2 = "java.awt.event.KeyEvent";
String setup1 = "r = new Robot";
String executionType = "r.keyPress(event)";
String event = "KeyEvent.VK_4";
private JTextField textField;
static ScriptEngineManager manager = new ScriptEngineManager();
static ScriptEngine engine = manager.getEngineByName("JavaScript");
public executor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,100,100);
textField = new JTextField();
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
try {
engine.eval("importClass(" + import1 + ")");
engine.eval("importClass(" + import2 + ")");
engine.eval(setup1);
engine.eval(executionType.replaceAll("event", event));
} catch (ScriptException e) {
e.printStackTrace();
}
}
});
getContentPane().add(textField, BorderLayout.CENTER);
textField.setColumns(10);
}
public static void main(String[] args) {
executor ex = new executor();
ex.setVisible(true);
}
}
我正在从 JSON 文件中读取两件事:
actionOnPress:"robot.KeyPress(e)"
(机器人是一个 java.awt.robot 实例)和
event:"KeyEvent.VK_4"
我要执行
robot.KeyPress(KeyEvent.VK_4)
执行此操作的最简单方法(最好不需要下载库)是什么?此代码应该也适用于 robot.mousePress 和 robot.mouseMove 等
我已经用 ScriptEngine 尝试过不同的东西,但 none 似乎有效。
非常感谢,Kamik423
编辑:应该是通用的。用户应该能够指定不同的事件,例如 FOR EXAMPLE robot
好的,自己解决了。这是代码:
package test;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class executor extends JFrame {
String import1 = "java.awt.Robot";
String import2 = "java.awt.event.KeyEvent";
String setup1 = "r = new Robot";
String executionType = "r.keyPress(event)";
String event = "KeyEvent.VK_4";
private JTextField textField;
static ScriptEngineManager manager = new ScriptEngineManager();
static ScriptEngine engine = manager.getEngineByName("JavaScript");
public executor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,100,100);
textField = new JTextField();
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
try {
engine.eval("importClass(" + import1 + ")");
engine.eval("importClass(" + import2 + ")");
engine.eval(setup1);
engine.eval(executionType.replaceAll("event", event));
} catch (ScriptException e) {
e.printStackTrace();
}
}
});
getContentPane().add(textField, BorderLayout.CENTER);
textField.setColumns(10);
}
public static void main(String[] args) {
executor ex = new executor();
ex.setVisible(true);
}
}