我的程序中有一个键监听器,对于大多数键,我希望用户能够按住键。然而,屏幕截图的关键是我希望用户能够按住键,但它只需要一个截图任何想法吗?
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) {
}
}
答案 0 :(得分:0)
根据我的理解,当按住适当的键时,与每个键相关联的操作会重复,并且即使按住相应的键,您也希望停止其中一个操作重复。我认为您可以使用在调用keyPressed()
时设置的附加标志来解决此问题。然后screen_shot
仅在tick()
设置此标志时设置,每次调用tick()
时都会取消设置该标志(当然,在读取其值后)。或者,您可以仅在设置了标志并且设置screen_shot
标志时取消设置标志。我不认为这有任何不同。
答案 1 :(得分: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();
}
}
可能无法达到标准,因为您可能需要更改"如果"上面的屏幕截图声明。