Jump to content
  • Advertisement
Sign in to follow this  
shaun23

[java] Java KeyListener

This topic is 4654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I was wondering if someone could assist me with the following problem I have created a GUI that represents a small keyboard with 4 keys. I am trying to map each noteButton with a certain key on the actual keyboard of the pc. For example ,if I press the Shift key on the keyboard,then it should correspond to noteButton60 on the GUI keyboard and play the sound. At the moment I can click each noteButton using the mouse but I want to be able to do the same, but by using the keys on my keyboard. I have implemented the KeyListener class,but I am not sure how to solve the problem I explained above using this class. I would appreciate if someone could give me a hand with this.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
 
 
public  class SynthUI extends Frame implements ActionListener,KeyListener
                                               
{
 
     private static final int FRAME_WIDTH = 680;
     private static final int FRAME_HEIGHT = 400;
     
    //Declare and create buttons for the keyboard
        Button noteButton60 = new Button(); 
        Button noteButton61 = new Button(); 
        Button noteButton62 = new Button(); 
        Button noteButton63 = new Button(); 
       
              
    /**
     * Constructor for objects of class SynthUI
     */
    public SynthUI(SynthController synthController)
    {
        super("MIDI SynthController");
 
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setResizable(false);
        setLayout(null);
        setLocation(20, 5);
      
   
        titleLabel  = new Label("MIDI SynthController 2005");
        
 
        noteButton60.setBounds(40,130,40,230);
        noteButton61.setBounds(60,130,35,140);
        noteButton62.setBounds(80,130,40,230);
        noteButton63.setBounds(100,130,35,140);
  
 
        //Add the buttons to the GUI
        add(noteButton60);
        add(noteButton61);
        add(noteButton62);
        add(noteButton63);
        
     
    
        noteButton60.addActionListener(this);
        noteButton61.addActionListener(this);
        noteButton62.addActionListener(this);
        noteButton63.addActionListener(this);
       
         
        //KeyListener
          noteButton60.addKeyListener(this);
          noteButton61.addKeyListener(this);
          noteButton62.addKeyListener(this);
          noteButton63.addKeyListener(this);
         
          noteButton60.setFocusable(true) ;
          noteButton61.setFocusable(true) ;
          noteButton62.setFocusable(true) ;
          noteButton63.setFocusable(true) ;
       
          
    }
    
 
  
   public void keyTyped(KeyEvent e) { }
    
  
    public void keyPressed(KeyEvent e) { }
       
  
    public void keyReleased(KeyEvent e) { }
 
 
   }
 
   

Share this post


Link to post
Share on other sites
Advertisement
hey shaun32,

I'm not sure whether or not I understand your problem, but it sounds like you need to know whether or not a modifier key is being held down. Let me know if I'm on the wrong track here..

Think of the keyboard as a series of on/off switches, so assign a true/false value anytime a key is pressed/released, respectively.

First of all set up an array of boolean values.
The keycode would serve as an index into the array. I'm offline right now so i don't have a link, but there are constants like VK_UP, VK_RIGHT, VK_ESC, VK_SPACE, VK_W, etc. (VK_) stands for virtual key.

For example, make "i" an array from [0..255]. In your keyListener..

void keyPressed(keyEvent e) {
i[e.keyCode] = true;
}

void keyReleased(keyEvent e) {
i[e.keyCode] = false;
}

The boolean values are updated real-time (whenever a keyboard event is registered through the os), so you can check it before performing whatever sick schtick programming trick you have up your curdly little mind.

if (VK_K) System.out.println("KANSAS RULEZ!!");

Hope this helps, and have fun my friend.

--scrng

Share this post


Link to post
Share on other sites
Sorry,I should have explained this more clearly. I want the user to be able to play the keyboard using the keys on the actual keyboard of the computer.

I was informed that I needed to implement the keyListener class,but from my understanding ,the KeyListener methods simply indicate which keys have been pressed on the actual keyboard of the pc.

Share this post


Link to post
Share on other sites
let's say your keys are 'a' 's' 'd' 'f' on your keyboard that will each be mapped to the GUI buttons.

in the method keyPressed, check to see if any of 'a' 's' 'd' 'f' were pressed using if statements or something. say it turns out to be 'a' that was pressed. then have 'a' play whatever the corresponding GUI button was supposed to play.
if none of those buttons were pressed then simply do nothing. :)
maybe that clarifies some stuff up.

Share this post


Link to post
Share on other sites
Quote:
Original post by shaun23
Sorry,I should have explained this more clearly. I want the user to be able to play the keyboard using the keys on the actual keyboard of the computer.

I was informed that I needed to implement the keyListener class,but from my understanding ,the KeyListener methods simply indicate which keys have been pressed on the actual keyboard of the pc.


Again, se my link. It talks about how to bind actions to keys.

Lizard

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!