Results 1 to 7 of 7
  1. #1
    steven.smith is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Question Key Binding (hotkey) for Sending Audio, how do I?

    Hey there all!
    I have a Java app that sends audio to a server. As of now, a person has to click and hold the button while they speak through their microphone. I would like to add a hotkey so that they only have to press the specified key to record their voice. Also, I would like to be able to use the down key as a possible hotkey (yes I know it's weird, but I do have my reasons). After reading around I've found that Key Binding is probably the best way to go, but I'm unsure of how to go about this. The code is as follows:

    Java Code:
    		setTitle("RecordAudio");				
    		JButton button=new JButton("Push this Button to Speak");
    		button.addMouseListener(this);
    		add(button);		
    		setSize(new Dimension(300,100));
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    
    			
    			line.open(format);
    			line.start();
    
    				int count = line.read(buffer, 0, buffer.length);
    				if (count > 0) {
    
    					output.writeInt(reverse(count));
    
    					output.write(buffer, 0, count);
    				}
    			}			
    			
    			output.writeInt(0);
    			output.close();
    			line.close();
    			
    		} catch (LineUnavailableException e) {
    			say("Line unavailable: " + e);
    		} catch (IOException e) {
    			say("I/O problems: " + e);
    		}
    	}
    	
    	public void stopRunning()
    	{
    		running=false;
    	}
    	
    	@Override
    	public void mouseClicked(MouseEvent e) {
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    	}
    	@Override
    	public void mouseExited(MouseEvent e) {
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    
    		Thread thread=new Thread(this);
    		thread.start();
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    
    		stopRunning();
    	}
    	
    	public static void main(String [] args)
    	{
    
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (Exception e) {
    			e.printStackTrace();
    			return;
    		}
    		
    		
    		MicrophoneInput mic=null;
    		try {
    			mic = new MicrophoneInput();
    			mic.setVisible(true);
    		} catch (LineUnavailableException e) {
    			say("Line unavailable: "+e);
    		}
    	}
    How would I attach a random hotkey such as "F3" to the button input? Also, would Key Binding be the best way, or is there another simpler way?

    Thanks in advance for any and all help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Key Binding (hotkey) for Sending Audio, how do I?

    Why do they have to hold the key down? Why not toggle the state: First press ON second press OFF.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    steven.smith is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Key Binding (hotkey) for Sending Audio, how do I?

    Quote Originally Posted by Norm View Post
    Why do they have to hold the key down? Why not toggle the state: First press ON second press OFF.
    I'm assuming you're talking about the JButton instead of the hotkey, so the answer is because of how the server end translates the audio. If they have a start/stop button the user can very likely leave it recording on accident which would definitely mess with the server end of things.
    So the original question remains, what do I need to do to bind a hotkey to the button?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Key Binding (hotkey) for Sending Audio, how do I?

    Not sure how a "hot" key will change the leaving it on problem. You will get the same one event in the program when the key is pressed. The user will be required to tell the system when to turn off.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    steven.smith is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Key Binding (hotkey) for Sending Audio, how do I?

    Quote Originally Posted by Norm View Post
    Not sure how a "hot" key will change the leaving it on problem. You will get the same one event in the program when the key is pressed. The user will be required to tell the system when to turn off.
    Well, I figured that there would be some sort of way to attach a hotkey to simulate what the mouse button does (i.e. mouse press on button, record, release mouse button, stop recording). I thought there was a way to do the same exact thing except by pressing/releasing a predefined keyboard key.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Key Binding (hotkey) for Sending Audio, how do I?

    I don't know what you mean by a "hot" key and how it is related to a mouse event.
    How could that be used to turn off the recording? Wouldn't the user still have to remember to do something?

    This took a while to find:
    http://docs.oracle.com/javase/tutori...eybinding.html
    Last edited by Norm; 03-30-2012 at 02:55 AM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Key Binding (hotkey) for Sending Audio, how do I?

    Key Bindings could very well work for you, but you'll have to take care to do all audio code in a background thread. You would create an Action for activating the audio that is triggered by keypress and another Action to stop audio recording that is triggered by key release, and again while the Action code's actionPerformed must be on the EDT or event thread, the audio driver code must then be called in a background thread.

Similar Threads

  1. How to add hotkey (F12) to a button?
    By steven.smith in forum AWT / Swing
    Replies: 7
    Last Post: 01-21-2012, 04:49 AM
  2. Replies: 0
    Last Post: 11-07-2011, 09:48 AM
  3. Help: Key Binding
    By mine0926 in forum New To Java
    Replies: 0
    Last Post: 08-06-2010, 04:52 AM
  4. outside hotkey
    By Dennis in forum Advanced Java
    Replies: 4
    Last Post: 06-18-2010, 07:37 PM
  5. How to add hotkey(Ctrl+VK) to a button?
    By rpwtdj in forum Advanced Java
    Replies: 5
    Last Post: 01-10-2008, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •