...I know that this could be done by using JFrame and KeyListener
No, you would almost never add a KeyListener to a JFrame, or use a KeyListener in general in Swing GUI's, even if you wanted to listen for keystrokes in an active GUI.
but want this my listener to run in the background.
Then you're looking for a key logger and will need to hook the keyboard input, something that cannot be done in plain vanilla core Java.
The easiest way that I do this is by using another language such as the AutoIt scripting language (if this is Windows), and then having my AutoIt notify my Java program via standard input and output sockets.
A more difficult solution (for me) is to use C or C++ to write your own keyboard hook, and then link this into Java via JNI or JNA. Regardless, any solution must be platform dependent.