键持续按压很多次

时间:2017-12-06 05:24:11

标签: java youtube

我正在尝试创建一个程序:

  • 从直播中获取实时聊天消息
  • 如果实时聊天消息是某个控件,请按住该键两秒钟
  • 记下该实时聊天ID,不要再按该键。

这是我到目前为止所拥有的。当我运行程序并在聊天框中输入一个值时,它无限地按下键并且不会停止,冻结计算机。

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

class Game {
    String[] rawCommands = { "up", "down", "left", "right", "start", "select", "a", "b", "x", "y", "l", "r" };
    int[] transCommands = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_F,
            KeyEvent.VK_G, KeyEvent.VK_H, KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L };
    ArrayList<String> seen = new ArrayList<String>();

    void main() {
        while (true) {
            HashMap<Integer, String[]> messages = listChatMessages();

            for (int i = 0; i < messages.size(); i++) {
                String[] full = messages.get(i);
                String id = full[0];
                String msg = full[1];

                Result r = oneOfManyIgnoreCase(msg, rawCommands);
                if (r.bool) {
                    if (!oneOfManyIgnoreCase(id, seen.toArray(new String[seen.size()])).bool) {
                        int key = transCommands[Arrays.asList(rawCommands).indexOf(r.msg)];
                        keyPress(key);
                        if (seen.size() > 90) {
                            seen.remove(0);
                        }
                        seen.add(id);
                    }
                }
            }
        }
    }

    void deleteChatMessage(String id) {

    }

    HashMap<Integer, String[]> listChatMessages() {
        HashMap<Integer, String[]> messages = new HashMap<>();
        try {
            URL response = new URL("https://www.googleapis.com/youtube/v3/liveChat/m"
                    + "essages?liveChatId=EiEKGFVDaUxwMWJTNFpvc2JlaHA5bHppbGNJQRIFL2xpdmU&part=sn"
                    + "ippet&key=AIzaSyAYFLriWHDy8MJZ_zblU-GuovOoqg6L-28&maxResults=200&pageToken=GKf04s3c7tcCILjGoM7c7tcC");
            ReadableByteChannel rbc = Channels.newChannel(response.openStream());
            FileOutputStream fos = new FileOutputStream("messages.json");
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();

            JSONParser parser = new JSONParser();
            Object obj = parser.parse(new FileReader("messages.json"));
            JSONObject jObj = (JSONObject) obj;
            JSONArray jArr = (JSONArray) jObj.get("items");
            for (int i = 0; i < jArr.size(); i++) {
                JSONObject msg = (JSONObject) jArr.get(i);
                JSONObject snippet = (JSONObject) msg.get("snippet");
                JSONObject txtDetails = (JSONObject) snippet.get("textMessageDetails");
                String[] full = new String[2];
                full[1] = (String) txtDetails.get("messageText");
                full[0] = (String) msg.get("id");

                messages.put(i, full);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return messages;
    }

    void keyPress(int key) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Robot bot = new Robot();
                    bot.setAutoDelay(1);

                    int duration = 3000;
                    long start = System.currentTimeMillis();
                    while (System.currentTimeMillis() - start < duration) {
                        bot.keyPress(key);
                        bot.keyRelease(key);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }

    Result oneOfManyIgnoreCase(String one, String[] many) {
        Boolean match = false;
        String message = "";
        for (String man : many) {
            if (one.equalsIgnoreCase(man)) {
                match = true;
                message = man;
                break;
            }
        }
        Result r = new Result(match, message);
        return r;
    }
}

1 个答案:

答案 0 :(得分:0)

以下代码不是处理按键操作的好方法。通常,如果你想以编程方式按住某个键,那么你应该按下按键直到时间结束,然后在时间结束后等待2毫秒,然后释放按键。我不认为你给键盘驱动程序足够的时间来处理发布。另外。只尝试按键一次,或者至少按照您的需要进行一次。通常情况下,如果我没有弄错的话,我认为你只需要按一次并在时间结束时释放它。

while (System.currentTimeMillis() - start < duration) {
    bot.keyPress(key);
    bot.keyRelease(key);
}