我如何改变 r.keyPress(KeyEvent.VK_W) 的结尾

how i can change the end of r.keyPress(KeyEvent.VK_W) the W out of the VK

好吧,我正在做 Keypressed 和 keyreleased,这与 VK_ 东西一起工作....我已经准备好 GUI 并且我能够保存和加载配置文件,但我想知道如何改变结束r.keyPress(KeyEvent.VK_W) 的 W 出 VK 与我的代码....

这是我的主要内容

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;



public class Main {

public static void main(String[] args) throws Exception{

    Config bot = new Config();
    bot.setVerbose(true);
    bot.connect("irc.twitch.tv", 6667,  "oauth:6u54pi07uzegv42dwee65gpgzmwwgi");
    bot.joinChannel("#mmolegion");

    JFrame frame =  new JFrame("TwitchBot");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setPreferredSize(new Dimension(700, 500));
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);


    KeyGetter.LoadKeys();
    try {
        Config.loadConfig();
    } catch (Exception e) {
        e.printStackTrace();
    }

    JMenuBar mb = new JMenuBar();

    JMenu file = new JMenu("File");
    mb.add(file);
    JMenu edit = new JMenu("Edit");
    mb.add(edit);
    JMenuItem options = new JMenuItem("Options");
    options.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            Config.openConfig(frame);
        }
    });


    frame.setJMenuBar(mb);
    edit.add(options);
    frame.pack();
    frame.setVisible(true);

 }  

 }

他是我的配置

import java.awt.Choice;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.jibble.pircbot.PircBot;


public class Config extends PircBot{

public static String left = "up", right = "right", up = "up", down = "down";
private static ArrayList<Choice> choices;

public Config() {
    this.setName("Rex__Bot");
 }


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();
         }
      }
     } 

public static void openConfig(JFrame frame){
    choices = new ArrayList<Choice>();
    JFrame options = new JFrame("Options");
    options.setSize(600, 400);
    options.setResizable(false);
    options.setLocationRelativeTo(frame);
    options.setLayout(null);
    Choice left = addChoice("left", options, 30, 30);
    left.select(Config.left);
    Choice right = addChoice("right", options, 30, 80);
    right.select(Config.right);
    Choice up = addChoice("up", options, 150, 30);
    up.select(Config.up);
    Choice down = addChoice("down", options, 150, 80);
    down.select(Config.down);
    JButton done = new JButton("ok");
    done.setBounds(150, 220, 100, 30);
    done.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            options.dispose();
            saveChanges();
        }
    });
    options.add(done);
    options.setVisible(true);
}

public static void saveChanges(){
    Choice left = choices.get(0);
    Choice right = choices.get(1);
    Choice up = choices.get(2);
    Choice down = choices.get(3);
    Config.left = left.getSelectedItem();
    Config.right = right.getSelectedItem();
    Config.up = up.getSelectedItem();
    Config.down = down.getSelectedItem();
    try{ 
        saveConfig();
    }
    catch(Exception e){
        e.printStackTrace();
    }

}

public static Choice addChoice(String name, JFrame options, int x, int y){
    JLabel label =  new JLabel(name);
    label.setBounds(x,  y - 20, 100, 20);
    Choice key = new Choice();
    for(String s: getKeyNames()){
        key.add(s);
    }
    key.setBounds(x, y, 100, 20);
    options.add(key);
    options.add(label);
    choices.add(key);
    return key;
}

public static ArrayList<String> getKeyNames(){
    ArrayList<String> result = new ArrayList<String>();
    for(String s: KeyGetter.keyNames){
        result.add(s);
        if(s.equalsIgnoreCase("F24")){
            break;
        }
        }
        return result;
}
public static void loadConfig() throws Exception{
    File directory = new File(getDefaultDirectory(), "/Twitchbot");
    if(!directory.exists()){
        directory.mkdirs();
    }
    File config = new File(directory,"config.txt");
    if(!config.exists()){
        config.createNewFile();
        System.out.println("File not found, saving default");
        saveConfig();
        return;
    }
    @SuppressWarnings("resource")
    Scanner s = new Scanner(config);
    HashMap<String, String> values = new HashMap<String, String>();     
    while(s.hasNextLine()){
        String[] entry = s.nextLine().split(":");
        String key =  entry[0];
        String value = entry[1];
        values.put(key, value);
    }

    if(!values.containsKey("left") || !values.containsKey("right") || !values.containsKey("up") || !values.containsKey("down")){
        System.out.println("Invalid names in config, saving default config");
        saveConfig();
        return;
    }
    String left = values.get("left");
    String right = values.get("right");
    String up = values.get("up");
    String down = values.get("down");

    if(!(getKeyNames().contains(left) && getKeyNames().contains(right) && getKeyNames().contains(up) && getKeyNames().contains(down))){
        System.out.println("Invalid key in config, saving default config");
    }
    Config.left = left;
    Config.right = right;
    Config.up = up;
    Config.down = down;
 }

public static void saveConfig() throws Exception{
    File directory = new File(getDefaultDirectory(), "/Twitchbot");
    if(!directory.exists()){
        directory.mkdirs();
    }
    File config = new File(directory,"config.txt");
    PrintWriter pw = new PrintWriter(config);
    pw.println("left:" + left);
    pw.println("right:" + right);
    pw.println("up:" + up);
    pw.println("down:" + down);
    pw.close();
}

public static String getDefaultDirectory(){
    String OS = System.getProperty("os.name").toUpperCase();
    if(OS.contains("WIN")){
        return System.getenv("APPDATA");
    }
    if(OS.contains("MAC")){
        return System.getProperty("user.home") + "Library/Application Support";
    }
    return System.getProperty("user.home");
 }

 }

这是我的 KeyGetter

import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;


public class KeyGetter {

public static HashMap<String, Integer> keys;
public static ArrayList<String> keyNames;

public static void LoadKeys(){
    keys = new HashMap<String, Integer>();
    keyNames = new ArrayList<String>();
    Field[] fields = KeyEvent.class.getFields();
    for(Field f: fields){
        if(Modifier.isStatic(f.getModifiers())){
            if(f.getName().startsWith("VK")){
             try{
                int num = f.getInt(null);
                String name = KeyEvent.getKeyText(num);
                keys.put(name, num);
                keyNames.add(name);
             }
             catch(Exception e){
                 e.printStackTrace();
             }
            }
        }
    }
}

}

那么有没有办法用我的配置文件更改 VK_W 的最后一个字母?

不知道你能不能做到。

为什么不只保存 int 值。例如 KeyEvent.VK_W 转换为:

public static final int VK_W = 87;

因此,如果您将 int walues 保存到您的配置中会容易得多。

您可以在此处找到完整列表: KeyEvent.VK_W

因此您只需使用以下代码即可:

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == 87) {
        System.out.println("W pressed");
    }
}

而不是:

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_W) {
        System.out.println("W pressed");
    }
}