1

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.

Kaperly
  • 43
  • 9

3 Answers3

1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

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.

Community
  • 1
  • 1
Steven
  • 1,564
  • 1
  • 22
  • 34
0

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

user2277872
  • 2,963
  • 1
  • 21
  • 22