Java best way to do something without breaking program

I’m practicing in java and I want to make a simple game. In this game, there is a ball that is constantly moving nonstop. I know how to move the ball but there is also some key press control and events that are happening. So it’s not all about the ball. How can I make the ball moving and at the same time the program can catch and do my key press and do other things I want. Is it the thread I needed? Or what I don’t know. What is the best/efficient way to do this? Thanks.