<commit_before>package spaar.mods.keysimulator;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class KeySimulator {

  public static Map<String, Integer> SpecialKeys = new HashMap<String, Integer>();

  static {
    SpecialKeys.put("alt", KeyEvent.VK_ALT);
    SpecialKeys.put("ctrl", KeyEvent.VK_CONTROL);
    SpecialKeys.put("shift", KeyEvent.VK_SHIFT);
    SpecialKeys.put("left arrow", KeyEvent.VK_LEFT);
    SpecialKeys.put("right arrow", KeyEvent.VK_RIGHT);
    SpecialKeys.put("up arrow", KeyEvent.VK_UP);
    SpecialKeys.put("down arrow", KeyEvent.VK_DOWN);
    SpecialKeys.put("numpad 0", KeyEvent.VK_NUMPAD0);
    SpecialKeys.put("numpad 1", KeyEvent.VK_NUMPAD1);
    SpecialKeys.put("numpad 2", KeyEvent.VK_NUMPAD2);
    SpecialKeys.put("numpad 3", KeyEvent.VK_NUMPAD3);
    SpecialKeys.put("numpad 4", KeyEvent.VK_NUMPAD4);
    SpecialKeys.put("numpad 5", KeyEvent.VK_NUMPAD5);
    SpecialKeys.put("numpad 6", KeyEvent.VK_NUMPAD6);
    SpecialKeys.put("numpad 7", KeyEvent.VK_NUMPAD7);
    SpecialKeys.put("numpad 8", KeyEvent.VK_NUMPAD8);
    SpecialKeys.put("numpad 9", KeyEvent.VK_NUMPAD9);
  }

  public static void main(String[] args) {
    try {
      Robot robot = new Robot();
      Scanner in = new Scanner(new InputStreamReader(System.in, StandardCharsets.US_ASCII));
      PrintStream out = new PrintStream(System.out, false, StandardCharsets.US_ASCII.name());

      while (true) {
        while (!in.hasNextLine()) {
          ;
        }

        String input = in.nextLine();

        if (input.equals("init")) {
          out.println("ok");
          out.flush();
          continue;
        }

        String mode = input.split(":")[0];
        String[] keys = input.split(":")[1].split(",");

        int[] keyCodes = ParseKeys(keys);

        if (mode.equals("c")) {
          boolean valid = true;
          for (int keyCode : keyCodes) {
            if (keyCode == KeyEvent.VK_UNDEFINED) {
              valid = false;
              break;
            }
          }
          if (valid) {
            out.println("v");
            out.flush();
          }
          else {
            out.println("n");
            out.flush();
          }
        }

        for (int keyCode : keyCodes) {
          if (keyCode != KeyEvent.VK_UNDEFINED) {
            if (mode.equals("p")) {
              robot.keyPress(keyCode);
              robot.keyRelease(keyCode);
            } else if (mode.equals("h")) {
              robot.keyPress(keyCode);
            } else if (mode.equals("r")) {
              robot.keyRelease(keyCode);
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      while (true) {
        ;
      }
    }
  }

  public static int[] ParseKeys(String[] keys) {
    int[] keyCodes = new int[keys.length];
    for (int i = 0; i < keys.length; i++) {
      String key = keys[i];<commit_msg>Reduce likelyhood of KeySimulator hanging and clogging CPU<commit_after>package spaar.mods.keysimulator;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class KeySimulator {

  public static Map<String, Integer> SpecialKeys = new HashMap<String, Integer>();

  static {
    SpecialKeys.put("alt", KeyEvent.VK_ALT);
    SpecialKeys.put("ctrl", KeyEvent.VK_CONTROL);
    SpecialKeys.put("shift", KeyEvent.VK_SHIFT);
    SpecialKeys.put("left arrow", KeyEvent.VK_LEFT);
    SpecialKeys.put("right arrow", KeyEvent.VK_RIGHT);
    SpecialKeys.put("up arrow", KeyEvent.VK_UP);
    SpecialKeys.put("down arrow", KeyEvent.VK_DOWN);
    SpecialKeys.put("numpad 0", KeyEvent.VK_NUMPAD0);
    SpecialKeys.put("numpad 1", KeyEvent.VK_NUMPAD1);
    SpecialKeys.put("numpad 2", KeyEvent.VK_NUMPAD2);
    SpecialKeys.put("numpad 3", KeyEvent.VK_NUMPAD3);
    SpecialKeys.put("numpad 4", KeyEvent.VK_NUMPAD4);
    SpecialKeys.put("numpad 5", KeyEvent.VK_NUMPAD5);
    SpecialKeys.put("numpad 6", KeyEvent.VK_NUMPAD6);
    SpecialKeys.put("numpad 7", KeyEvent.VK_NUMPAD7);
    SpecialKeys.put("numpad 8", KeyEvent.VK_NUMPAD8);
    SpecialKeys.put("numpad 9", KeyEvent.VK_NUMPAD9);
  }

  public static void main(String[] args) {
    try {
      Robot robot = new Robot();
      Scanner in = new Scanner(new InputStreamReader(System.in, StandardCharsets.US_ASCII));
      PrintStream out = new PrintStream(System.out, false, StandardCharsets.US_ASCII.name());

      while (true) {
        while (!in.hasNextLine()) {
          ;
        }

        String input = in.nextLine();

        if (input.equals("init")) {
          out.println("ok");
          out.flush();
          continue;
        }

        String mode = input.split(":")[0];
        String[] keys = input.split(":")[1].split(",");

        int[] keyCodes = ParseKeys(keys);

        if (mode.equals("c")) {
          boolean valid = true;
          for (int keyCode : keyCodes) {
            if (keyCode == KeyEvent.VK_UNDEFINED) {
              valid = false;
              break;
            }
          }
          if (valid) {
            out.println("v");
            out.flush();
          }
          else {
            out.println("n");
            out.flush();
          }
        }

        for (int keyCode : keyCodes) {
          if (keyCode != KeyEvent.VK_UNDEFINED) {
            if (mode.equals("p")) {
              robot.keyPress(keyCode);
              robot.keyRelease(keyCode);
            } else if (mode.equals("h")) {
              robot.keyPress(keyCode);
            } else if (mode.equals("r")) {
              robot.keyRelease(keyCode);
            }
          }
        }
      }
    } catch (Exception e) {
      main(args);
    }
  }

  public static int[] ParseKeys(String[] keys) {
    int[] keyCodes = new int[keys.length];
    for (int i = 0; i < keys.length; i++) {
      String key = keys[i];
{'fraction_non_alphanumeric': 0.1401129943502825, 'fraction_numerical': 0.0074253430185633575, 'mean_word_length': 2.3347685683530677, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2769944', 'n_tokens_mistral': 2053, 'n_tokens_neox': 2017, 'n_words': 382}