0

I have a key listener in my program and for most keys I want the user to be able to hold down a key. However the key for screenshots I want the user to be able to hold down the key yet it only takes one screenshot any ideas?

package me.bevilacqua.game;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class InputHandler implements KeyListener{

    private boolean[] keys = new boolean[120];
    public boolean up , down , left , right , screen_shot;

    public void tick() {
        up =  keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];    //Will be Jump
        down =  keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];  //Will not be down but maybe slide or better yet action key or maybe shoot
        left =  keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
        right =  keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];    
        screen_shot = keys[KeyEvent.VK_F5] || keys[KeyEvent.VK_BACK_SLASH];
    }

    public void keyPressed(KeyEvent e) {
        keys[e.getKeyCode()] = true;
    }

    public void keyReleased(KeyEvent e) {
        keys[e.getKeyCode()] = false;
    }

    public void keyTyped(KeyEvent e) {  
    }
}

EDIT:

Why doesn't this work:

package me.bevilacqua.game;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;


public class InputHandler implements KeyListener{
private boolean[] keys = new boolean[120];
public long current , last = -1;
public boolean up , down , left , right , screen_shot;
public boolean shotFlag = true; //True if allowed

public void tick() {
    up =  keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];    //Will be Jump
    down =  keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];  //Will not be down but maybe slide or better yet action key or maybe shoot
    left =  keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
    right =  keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];    
    screen_shot = keys[KeyEvent.VK_F5];
}

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() != KeyEvent.VK_F5) keys[e.getKeyCode()] = true;
    else if (e.getKeyCode() == KeyEvent.VK_F5 && shotFlag) {
        keys[e.getKeyCode()] = true;
        shotFlag = false;
    }


}

public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
    if(e.getKeyCode() == KeyEvent.VK_F5) shotFlag = true;
}

public void keyTyped(KeyEvent e) {  
}

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Bevilacqua
  • 465
  • 3
  • 8
  • 19
  • So, you have a flag in you system to stop key repeats, but for certain keys, you want to allow those repeats to occur, is that correct? – MadProgrammer Jan 13 '13 at 22:56
  • Well currently all keys repeat and i need to add a flag to prevent certain keys from repeating – Bevilacqua Jan 13 '13 at 22:59
  • 1
    This seems like a design issue. How is the `tick()` method used? – Code-Apprentice Jan 13 '13 at 23:01
  • Your listener is stopping the screen shot key from repeating. You could check for the key down event of the screen shot and start some kind of timer the fires every 'n' periods, taking a screen shot, until the key is released... – MadProgrammer Jan 13 '13 at 23:03
  • Updated but still not working – Bevilacqua Jan 13 '13 at 23:18
  • 1
    from this code not possible to answering, for better help sooner post an [SSCCE](http://sscce.org/), have look at `KeyBindings` in the case that GUI is based on `Swing` – mKorbel Jan 13 '13 at 23:19

2 Answers2

0

From what I understand, the action associated with every key repeats when the appropriate key is held down and you want to stop one of those actions from repeating even when the corresponding key is held down. I think you can solve this with an additional flag that is set when keyPressed() is called. Then screen_shot is only set in tick() when this flag is set and the flag is unset every time tick() is called (after reading its value of course). Alternatively, you can unset the flag only when it is set and you are setting the screen_shot flag. I don't think it makes any difference either way.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

You could try it like this:

public boolean up = false;
public boolean down = false;
public boolean left = false;
public boolean right = false;    
public boolean screen_shot = false;

//...

f.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_D)
                {
                    right = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_A)
                {
                    left = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_S)
                {
                    down = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_W)
                {
                    up = true;
                }
                if (e.getKeyCode() == KeyEvent.VK_F5)
                {
                    screen_shot = true;
                }
            }
            public void keyReleased(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_D)
                {
                    right = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_A)
                {
                    left = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_S)
                {
                    down = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_W)
                {
                    up = false;
                }
                if (e.getKeyCode() == KeyEvent.VK_F5)
                {
                    screen_shot = false;
                }
            }
//...

@Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        this.setBackground(Color.white);
        g.drawImage(character, characterLocation.x, characterLocation.y, this);

        if (right)
        {
            characterLocation.x += 1;
        }
        if (left)
        {
            characterLocation.x -= 1;
        }
        if (down)
        {
            characterLocation.y += 1;
        }
        if (up)
        {
            characterLocation.y -= 1;
        }
        if (screen_shot)
        {
            BufferedImage shot = character.createScreenCapture(new Rectangle(100,100));
        }
        repaint();
    }   
}

Might not work up to par as you might have to change the "if" statement for screen shot above.

Codereaper
  • 31
  • 7