import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Key extends Actor
{
    private String keyFromKeyboard;
    private String soundToPlay;
    private boolean isDown;
    
    /**
     * Create a new key.
     */
    public Key(String key, String sound)
    {
        keyFromKeyboard = key;
        isDown = false;
        soundToPlay = sound + ".wav";
    }

    /**
     * Do the action for this key.
     */
    public void act()
    {
        if(!isDown && Greenfoot.isKeyDown(keyFromKeyboard)) {
            setImage(new GreenfootImage("white-key-down.png"));
            isDown = true;
            play();
        }
        else if(isDown && !Greenfoot.isKeyDown(keyFromKeyboard)){
            setImage(new GreenfootImage("white-key.png"));
            isDown = false;
        }
    }
    
    private void play() {
        Greenfoot.playSound(soundToPlay);
    }
}