package ge.edu.freeuni.sdp.arkanoid.view.terminal;

import java.awt.*;
import java.awt.event.KeyEvent;

public class PressedKeyWatcher {
    private static boolean leftPressed = false;
    private static boolean rightPressed = false;
    private static boolean spacePressed = false;
    private static boolean escapePressed = false;

    public static boolean isLeftPressed() {
        synchronized (PressedKeyWatcher.class) {
            return leftPressed;
        }
    }

    public static boolean isRightPressed() {
        synchronized (PressedKeyWatcher.class) {
            return rightPressed;
        }
    }

    public static boolean isSpacePressed() {
        synchronized (PressedKeyWatcher.class) {
            return spacePressed;
        }
    }

    public static boolean isEscapePressed() {
        synchronized (PressedKeyWatcher.class) {
            return escapePressed;
        }
    }


    public static void init() {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

            public boolean dispatchKeyEvent(KeyEvent ke) {
                synchronized (PressedKeyWatcher.class) {
                    switch (ke.getID()) {
                        case KeyEvent.KEY_PRESSED:
                            if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
                                leftPressed = true;
                            }
                            if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
                                rightPressed = true;
                            }
                            if (ke.getKeyCode() == KeyEvent.VK_SPACE) {
                                spacePressed = true;
                            }
                            if (ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                escapePressed = true;
                            }
                            break;

                        case KeyEvent.KEY_RELEASED:
                            if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
                                leftPressed = false;
                            }
                            if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
                                rightPressed = false;
                            }
                            if (ke.getKeyCode() == KeyEvent.VK_SPACE) {
                                spacePressed = false;
                            }
                            if (ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                escapePressed = false;
                            }
                            break;
                    }
                    return false;
                }
            }
        });
    }

}


@@ -141,32 +141,41 @@
  private static 
+volatile 
 boolean leftPres
@@ -198,32 +198,41 @@
  private static 
+volatile 
 boolean rightPre
@@ -256,32 +256,41 @@
  private static 
+volatile 
 boolean spacePre
@@ -314,32 +314,41 @@
  private static 
+volatile 
 boolean escapePr
@@ -411,61 +411,8 @@
 ) %7B%0A
-        synchronized (PressedKeyWatcher.class) %7B%0A    
     
@@ -431,34 +431,24 @@
 eftPressed;%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -491,61 +491,8 @@
 ) %7B%0A
-        synchronized (PressedKeyWatcher.class) %7B%0A    
     
@@ -512,34 +512,24 @@
 ghtPressed;%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -572,61 +572,8 @@
 ) %7B%0A
-        synchronized (PressedKeyWatcher.class) %7B%0A    
     
@@ -593,34 +593,24 @@
 acePressed;%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -654,61 +654,8 @@
 ) %7B%0A
-        synchronized (PressedKeyWatcher.class) %7B%0A    
     
@@ -680,26 +680,16 @@
 ressed;%0A
-        %7D%0A
     %7D%0A%0A%0A
@@ -895,69 +895,8 @@
 ) %7B%0A
-                synchronized (PressedKeyWatcher.class) %7B%0A    
     
@@ -941,36 +941,32 @@
                 
-  
-  
 case KeyEvent.KE
@@ -972,28 +972,24 @@
 EY_PRESSED:%0A
-    
             
@@ -1063,36 +1063,32 @@
                 
-    
 leftPressed = tr
@@ -1107,36 +1107,32 @@
                 
-    
 %7D%0A              
@@ -1133,36 +1133,32 @@
                 
- 
-   
 if (ke.getKeyCod
@@ -1205,36 +1205,32 @@
                 
-    
 rightPressed = t
@@ -1254,34 +1254,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -1348,36 +1348,32 @@
                 
- 
-   
 spacePressed = t
@@ -1397,34 +1397,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -1464,36 +1464,32 @@
 nt.VK_ESCAPE) %7B%0A
-    
                 
@@ -1542,34 +1542,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -1592,36 +1592,32 @@
                 
-    
 case KeyEvent.KE
@@ -1644,36 +1644,32 @@
                 
-   
- 
 if (ke.getKeyCod
@@ -1715,36 +1715,32 @@
                 
-    
 leftPressed = fa
@@ -1764,34 +1764,26 @@
             
-   
- 
 %7D%0A
-    
             
@@ -1858,36 +1858,32 @@
                 
-  
-  
 rightPressed = f
@@ -1908,34 +1908,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2002,36 +2002,32 @@
                 
-    
 spacePressed = f
@@ -2052,34 +2052,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -2147,36 +2147,32 @@
                 
-    
 escapePressed = 
@@ -2170,36 +2170,32 @@
 ressed = false;%0A
-    
                 
@@ -2232,23 +2232,15 @@
     
-    
 break;%0A
-    
     
@@ -2265,28 +2265,24 @@
             
-    
 return false
@@ -2283,34 +2283,16 @@
  false;%0A
-                %7D%0A
         

Use volatile variables when reading key press