How to play a sound when a key is pressed - java

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

Related

Pressing key at any point to make an action

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.

Java Swing GUI Hidden Input

I made a simple JFrame with Swing. I want to know how I would go about making a non-visible input that would open up another JFrame I have in another class. (Like cheat codes in video games, you enter a combination and something happens.) I am not sure how to capture the user input without a text field.
You should use keybinding attached to your JFrame. You would want to store keystrokes as a String internally and after each keystroke, see whether the user has entered a recognizable cheat code or just listen for a return keypress as a delimiter for the code.
Keep in mind that if a component within that JFrame has focus and also implements the same key bindings, then that component will take precedence over the JFrame, effectively intercepting the keystrokes.

Converting Serial input to Keyboard Button-press

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.

Java Robot class keyPress not typing into google chrome

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)

KeyEvent#consume() method

Could any one explain what is consume() method and its purpose since I couldn't find much relevant information of it on the net? (example might be the best for a novice like me)
The consume method marks the event as processed. Typically this is executed from your program code when you've chosen to act upon the input, e.g. when you wish to prevent a text input from receiving the key stroke.
Or, to take the example given in the Javadocs:
[The consume method] allows listeners and component subclasses to "consume" the event so that the source will not process them in their default manner. For example, consuming mousePressed events on a Button component will prevent the Button from being activated.
Suppose you playing a Game FIFA for example your team is South Africa playing Russia .In normal circumstances if am marking a Russian player and l press key T in instance ,it will invoke a SlideTackle() ,sliding tackling function on the Russian player .
But what if l want to only make it possible that a sliding tackle can only occur whilst they is a opponent player near only that's when l will call consume() method so that the SlidingTackle() function doesn't respond to key press T when Russian opponent is not close .
That's how best l understood

Categories