3

I have a simple game and when I run the program I have to click on the window before the game will accept user input.

When I play games like The Binding of Isaac they accept user input on the main menu without me ever clicking them.

Is there a way to set the focus of my keyboard to my game without clicking it first? There was another question on this: Have to click before pressing key , but it was left unanswered.

Community
  • 1
  • 1

2 Answers2

1

If you have JFrame, or something like this (something inherited from java.awt.Component), you can try:

window.requestFocus();

Link to javadoc


EDIT:
In case of JFrame, I have found this question:
How to focus a JFrame?
One of the answer is the same as I advice
This can help you
Community
  • 1
  • 1
maskacovnik
  • 3,080
  • 5
  • 20
  • 26
  • @maskaconvnik This didn't seem to work for me. I tried to to use a few of the methods the answerers suggested none seemed to get the job done. Here's a screenshot of my current code: https://twitter.com/RobersonWade/status/614160888422137856 – Щąđε Яօɮɛʀֆօπ Jun 25 '15 at 20:00
1

Call window.requestFocus(); after calling main.start() so you override any other focus request done in the meanwhile

Luis Sieira
  • 29,926
  • 3
  • 31
  • 53