I'm programming on java, so I have to make a game-like program. The game should be able to "pause" at any point. Can you press a key at any point so a message is displayed saying "game is paused"? Then when you press that key again it will say "game resumed"?
Thanks
It looks like you need to be able to read input directly from the keyboard into your game? Is your game built into a GUI or in the console?
This can be done by using the KeyListener interface Java provides.
https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html
I'll assume your using swing so this tutorial should be pretty useful.
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Sorry I can't go into more detail, more information about what you are trying to do in the comments would help me help you.
Edit: You mentioned you're using the command line. To get user input you can use java.util.Scanner
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Make a boolean, isPaused. If space is pressed, switch isPaused from true to false or vice versa. If isPaused is true, run the game, if not, run a pause screen instead.
Related
Not sure if I'm asking this question on the correct site (I have been wondering if this should rather be posted on the EE.stackoverflow site).
So I have an OLD serial input mouse, and I want the system to reprogram or retranslate it to a keyboard button-press.
The whole thing is that I have software with shortcut keys, but remembering all of them is just not possible. So, why does one want to have a whole keyboard when one can only have a set of three buttons? Using a keyboard, when one presses F11 (and holds it in), it activates the system's mic. While holding in F11, one wants to press F1 to start playing music, thus being a voice over song application.
I've been trying to research the possibility, and I've come to notice that there are a LOT of applications that can convert a USB device, such as a game controller to a keyboard button-press. That's what's driving my concept.
Is this even possible?
Thanks,
Johan Brink
Well, as described here, if you are using Windows, you can use X-Mouse Button Control for mouse. If you want to use gamepad, try J2K - A Joystick to Keyboard Mapper 1.1
Have a look on Makey Makey (www.makeymakey.com). This is a board which emulates a key stroke each time one of his input is closed.
I would like to make a program that allows the user to type in a message. The message will be typed in a JTextField. I would like to make it so that when every key is pressed it will play a short sound. I have the sound file as an mp3, its a short beep.
I only want it to work for keys A - Z, 0 - 9, backspace, and space.
I know how I would do this with the enter key; an action listener. I don't know how to do this with every key that is pressed before hitting enter.
A KeyListener in a JTextField will likely have problems with focus.
Instead, you can bind a key to an Action that plays a short Tone.
You need to add a KeyListener to your text field.
Here is some example documentation.
Here is a question from this site that will help in playing the mp3.
Good luck.
The best bet I would say is use a KeyListener. Implement that within your class, create the instance of whatever sound class you're going to use, create a method and within the keyPressed method, call your function, here is an example of how to do that. http://www.daniweb.com/software-development/java/threads/108969/linking-jbuttons-to-key-events-through-key-listeners
I'm trying to beat the game 'fastTyper 2' (basically just a typing game where you type words that appear as fast as possible) by using the java robot class to type in the letters for me. I've successfully taken in the letters to be typed. However, it seems that the robot's keyPress method isn't working due to some kind of protection the game has. Does anyone know of a way to get around this?
Thanks
I don't think that the game protection is stopping you because there is no way for a game(or any other program) to decide whether the key is pressed manually or programatically.
Maybe you need to check the logic at what point of time you need your Robot class to press the key and which key to press(and make sure that you pass correct parameter value to keyPress method)
I am trying to create a small Java program that has a simple command line like widget not so different from that of idle. I was originally planning to use a JTextArea and try to detect when someone presses enter, but I was wondering if there would be a better way of doing this?
I think, it is more stylish way to have ENTER to terminate. If you want to have along with some button that will not easy as idle one. I suggestion you have JTEXTAREA, you user can have wide area to see the things that they have keyed. I do not have much idea on this :) thanks
I'm looking for a way to inject a keystroke into the OS keyboard input buffer,
like when you click a button the program inserts one (or more) keyboard strokes. I wanted to do this in java because I want to run this in (win,linux and osx). I guess that I'll have to make use of the JNI, do anyone have some ideas?
Thanks all stackoverflowers ;)
My guess is that the java.awt.Robot class will do this for you:
new Robot().keyPress(...);
http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress(int)
java.awt.Robot "is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed."
Check java Robot . I believe this is what you are looking for.
Also check this out. Example