I am writing a class that will read keystrokes only when the Chrome window is active:

Helper class, which reads the keys:

public class BoardLogger implements NativeKeyListener { private String letter=""; public String getLetter() { return letter; } public void clearLetter () { letter=""; } @Override public void nativeKeyTyped(NativeKeyEvent nke) { } @Override public void nativeKeyPressed(NativeKeyEvent nke) { System.out.println("Key pressed: "+NativeKeyEvent.getKeyText(nke.getKeyCode())); this.letter=NativeKeyEvent.getKeyText(nke.getKeyCode()); } @Override public void nativeKeyReleased(NativeKeyEvent nke) { } } 

ClientLogger class:

  public class ClientLogger implements Runnable { private static final int MAX_TITLE_LENGHT = 2014; private char [] buffer = new char [MAX_TITLE_LENGHT*2]; private BoardLogger boardlogger = new BoardLogger(); private String letter; private ArrayList <String> wordsList = new ArrayList <String>(); private final String REGEX = "(.* [Chrome]+)"; public ArrayList <String> getWordList() { return wordsList; } private String cheackActiveWindow() { User32DLL.GetWindowTextW(User32DLL.GetForegroundWindow(), buffer, MAX_TITLE_LENGHT); return Native.toString(buffer); } @Override public void run() { Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE); String word = ""; int i=0; try {GlobalScreen.registerNativeHook();} catch (NativeHookException ex) {ex.printStackTrace();} GlobalScreen.addNativeKeyListener(boardlogger); while (!Thread.currentThread().isInterrupted()) { Matcher m = p.matcher(this.cheackActiveWindow()); if (m.find()) { if (!boardlogger.getLetter().isEmpty()) { System.out.println("-------------------"); System.out.println("<<<<<<<<<<CHROME IS ATCIVE!!!"); System.out.println("-------------------"); letter=boardlogger.getLetter(); word+=word.concat(letter); boardlogger.clearLetter(); letter=""; } } else { System.out.println("-------------------"); System.out.println(">>>>>>>>CHROME IS NOT ATCIVE!!!"); System.out.println("-------------------"); wordsList.add(i, word); word=""; i++; } try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException ex) {ex.printStackTrace();} } GlobalScreen.removeNativeKeyListener(boardlogger); try {GlobalScreen.unregisterNativeHook();} catch (NativeHookException ex) {ex.printStackTrace();} } } 

I try to run:

 public class MainClass { public static void main(String[] args){ ClientLogger clientlogger = new ClientLogger (); Thread threadLogger = new Thread (clientlogger); threadLogger.start(); try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException ex) {ex.printStackTrace();} threadLogger.interrupt(); List <String> list = new ArrayList<String>(); list.addAll(clientlogger.getWordList()); list.removeIf(x->x.equals("")); System.out.println("from main: "+list); System.exit(0); } } 

When I enter, for example one, I do alt + tab, I return, I enter two at the output instead of

 [ONE, TWO] 

game turns out:

 [OONOONE, TTWTTWO] 

    1 answer 1

    from the string

     word+=word.concat(letter); 

    need to remove + to get

     word=word.concat(letter);