0

我的程序中有一个按键监听器,对于大多数按键,我希望用户能够按住按键。但是截图的关键我希望用户能够按住键但它只需要一个截图有什么想法吗?

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) {  
    }
}

编辑:

为什么这不起作用:

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) {  
}

}

4

2 回答 2

0

据我了解,当按住相应的键时,与每个键关联的操作都会重复,并且即使按住相应的键,您也希望阻止其中一个操作重复。keyPressed()我认为您可以使用在调用时设置的附加标志来解决此问题。然后screen_shot仅在设置tick()此标志并且每次tick()调用该标志都未设置时才设置(当然在读取其值之后)。或者,您可以仅在设置标志并且您正在设置标志时取消设置screen_shot标志。我不认为这有什么不同。

于 2013-01-13T23:05:45.887 回答
0

你可以这样尝试:

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();
    }   
}

可能无法达到标准,因为您可能必须更改上面屏幕截图的“if”语句。

于 2014-10-14T10:17:52.420 回答