好的....我想要的是让用户通过GUI为我的keyPress选择他想要的输入,我有一个地方我保存并加载带有VK Key代码的配置。 所以VK_W = 87,它在配置文件中保存了87。所以我想要一个选择或按钮,可以选择将被选为KeyPressed的VK /号码。
这是我的代码
public class Twitchbot extends PircBot {
int VK_W = 87;
int VK_S = 83;
int[] saveInformation = {VK_W, VK_S};
int VK_WLoc = 0;
int VK_SLoc = 1;
public Twitchbot() {
configRead("Config.cfg");
updateConfig();
configSave("Config.cfg");
this.setName("Rex__Bot");
}
private void updateConfig(){
System.out.println(VK_W);
System.out.println(VK_S);
VK_W = saveInformation[VK_WLoc];
VK_S = saveInformation[VK_SLoc];
System.out.println(VK_W);
System.out.println(VK_S);
}
private void configRead(String filePath) {
File inputFile;
BufferedReader inputReader;
try {
inputFile = new File(filePath);
inputReader = new BufferedReader(new FileReader(inputFile));
for(int i = 0; i < saveInformation.length; i++){
saveInformation[i] = Integer.parseInt(inputReader.readLine());
}
System.out.println(filePath);
inputReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void configSave(String filePath) {
File outputFile;
BufferedWriter outputWriter;
try {
outputFile = new File(filePath);
outputWriter = new BufferedWriter(new FileWriter(outputFile));
for(int i = 0; i < saveInformation.length; i++){
outputWriter.write(Integer.toString(saveInformation[i]) + "\n");
}
outputWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onMessage(String channel, String sender, String login, String hostname, String message) {
if(message.equals("up")) {
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_W);
r.delay(300);
r.keyRelease(KeyEvent.VK_W);
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
}