Q: Making thread to check if keyListener is pressed, and change variable's value from it So I made a game like a tetris, but the only shapes are squares. I want to change y value each time VK_RIGHT or VK_LEFT pressed via toLeft() or toRight().
Wrote it in keyPressed(), but its not working since Im putting thread on sleep from while before each loop. So Im stuck here. Guess I need new thread that will take responsibility for changing y's value on itself, but because Im not familiar with threads and never done that I dont have any idea how to make it work. Any help or hint would be really appreciated
p.s. here's the entire code in Shape class
public class Shape implements KeyListener {

//coordinates
public int x = 0, y = 0;

public Shape[][] map = new Shape[5][5];


public void toRight() {
    if (y < 4) {
        y++;
    }
}

public void toLeft() {
    if (y > 0) {
        y--;
    }
}


public void game() {
    while (true) {
        Shape block = new Shape();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            //nothing
        }

        if (map[x][0] != null && map[x][1] != null && map[x][2] != null && map[x][3] != null && map[x][4] != null) {

            for (int i = 4; i > 0; --i) {
                if (map[x - 1][0] != null) {
                    map[x][i] = map[x - 1][i];
                    map[x - 1][i] = null;
                }
            }
        }


        if (x != 4 && map[x + 1][y] != null) {
            map[x][y] = block;
            System.out.println("element " + x + " " + y);
            x = 0;
            y = 0;
        } else if (x == 4) {
            map[x][y] = block;
            System.out.println("element " + x + " " + y);
            x = 0;
            y = 0;

        } else {
            x++;
        }
        if (map[0][0] == block || map[0][1] == block || map[0][2] == block || map[0][3] == block || map[0][4] == block) {
            System.out.println("Game Over");
            break;
        }
    }

}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    switch (keyCode) {
        case KeyEvent.VK_RIGHT:
            toRight();
            System.out.println(y);
            break;
        case KeyEvent.VK_LEFT:
            toLeft();
            System.out.println(y);
            break;
    }
}

@Override
public void keyReleased(KeyEvent e) {

}

}
