Getting keyboard input.
Hello I am an amateur (first year university) Java programmer. Recently I have been playing around with some of the functions in Java and came across the Robot class which I'd like to do more with.
What I want to happen is something along these lines:
1. A Key is pressed, robot does function A
2. When Key is released robot stops doing function A
3. A different key is pressed, robot does Function B
4. When Key is released robot stops doing function B
I managed to get the robot class working only if I use it in the main method and make it do a few things.
I have done keyboard input before using KeyEvent and KeyListener, but the problem here is there is no actual gui or something for it to use as a component. Is there a way I can hook it on to the raw state of the keyboard or something?
If there isn't, then is there a different way to go about getting the state of the keyboard?
Robot exists to drive GUIs, like for testing or demonstrations, not to listen
to GUIs.
If you don't have a visible window, then I don't think you can have an
"active" window and thus can't be getting keyboard focus, and finally
keystrokes. That's just the way it is.
What are you trying to do? What is your goal? Perhaps there is a more
conventional way to do what you want, but you have to explain your
goals, first.
Well I was just interested in making a program that can manipulate other external programs, so I could try something like make a game play by itself or something along those lines.
People have mentioned that there is something (I think .net) that can do things like that but I'm not sure what that is, and was hoping I could do something similar in java.
What I essentially want is some way of just making the program run in the background and react to keys to turn on/off robot scripts.
Can someone PLEASE try and help me, I'll explain exactly what I want to happen. If this is impossible to do with Java could someone tell me what I would do to go about doing this.
Here's a simple "bot" type program I want to run:
1. I press arrow left key, I am not using the java program directly, it is running in the background.
2. Program sees that I have hit left key and then tells the Robot class to hit the left key 2 more times
3. Program I am using (a game) thinks that I have hit the left key 3 times now and acts accordingly.
I have tried a lot of stuff but can not seem to get it to ever get the keyboard input since I am not directly using a java application.