I am working on a small game using full screen exclusive and I need to be able to receive keyboard input from the player.
In my program, I have a Window which I set to Full Screen Exclusive, and a rendering loop.
Window creation:
private void initialize() {
//This is used for my game loop...
running = true;
//Create the instance variable 'window' here.
window = new Window(null);
//Ignoring OS paint requests...
window.setIgnoreRepaint(true);
//Set the window to full screen exclusive.
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);
...
Game Loop:
private void loop() {
Graphics graphics = window.getGraphics();
graphics.setColor(Color.CYAN);
graphics.fillRect(0, 0, 1920, 1080);
graphics.dispose();
}
My Imports:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
Now, this works fine. Just as it should. It renders a cyan colored rectangle, across my screen, however, I need to be able to detect when the user hits keys e.g. hitting Escape
to close the program, and such. I don't know how to do this. :(
I have tried adding a KeyListener
to my window
(didn't work).
I have tried adding a JPanel
to my window
and adding a Listener to that (also didn't work).
I have tried requesting focus on my JPanel
and doing the same thing above.
I have tried making a JFrame
with a KeyListener then passing it into my window
's constructor.
I have tried passing the same JFrame
with Key Bindings to my window
, rather than a KeyListener.
Obviously, none of the above has worked. (Errors weren't thrown, I simply couldn't get the program to output the text in my sysout
s when I pressed a key or exit the program using System.exit(int);
) I have taken out everything that didn't work for me from the above code; I currently have a window and a game loop. If there are any other ways for me to get Key Input in full screen exclusive, please inform me. (I feel as if there is a conventional way specifically for full screen exclusive, but I haven't found one yet.) Or if you believe there is a way to get one of the methods I have tried to work, (maybe you believe I did something wrong), please let me know. (I am getting somewhat desperate at this point).