按下按键和按住按键之间的延迟时间 [英] Delay between pressing a key and the key being read as held down
问题描述
我已经注意到,在按住某个键的同时,读取该键与按住该键之间会有短暂的延迟.我的意思是按住任意键并仔细观察.模式如下所示:h.....hhhhhhhhhhhhhhhhhhhhhh
其中.
表示暂停.很好,因为当我们键入内容时,尝试写一个字母会比较困难. '一世'.
I have noticed that while holding a key down, there is a short delay between the key being read as being held down. What I mean is this; hold down any key and observe closely. The pattern goes like this: h.....hhhhhhhhhhhhhhhhhhhhhh
where .
symbolizes a pause. This is fine, because when we type, it would be harder trying to write a single letter e.g. 'i'.
但是,如果我们想开发一款游戏,该怎么办?我的意思是,我们不希望延迟.这完全是我的问题.现在,让我向您展示我的代码:
But what if we want to make a game? I mean, we wouldn't like the delay. This is exacly my problem. So now, let me show you my code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tutorial3{
static MyPanel panel;
public static void main(String[] agrs){
createAndStartGui();
new gameplay();
}
static void createAndStartGui(){
JFrame f = new JFrame("tutorial 3");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setPreferredSize(new Dimension(500, 300));
f.addKeyListener(new MyKeyListener());
panel = new MyPanel();
f.add(panel);
f.pack();
f.setVisible(true);
}
}
class gameplay {
static String current_key = "stop";
public gameplay(){
begin();
}
void begin(){
while (true){
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Tutorial3.class.getName()).log(Level.SEVERE, null, ex);
}
Tutorial3.panel.user.move(current_key);
Tutorial3.panel.repaint();
System.out.println(current_key);
current_key = "stop";
}
}
static void key_event(){
}
}
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
switch(keyCode){
case KeyEvent.VK_UP:
gameplay.current_key = "up";
break;
case KeyEvent.VK_DOWN:
gameplay.current_key = "down";
break;
case KeyEvent.VK_LEFT:
gameplay.current_key = "left";
break;
case KeyEvent.VK_RIGHT:
gameplay.current_key = "right";
break;
default:
gameplay.current_key = "stop";
}
}
}
class MyRectangle{
int x;
int y;
public MyRectangle(int x, int y){
this.x = x;
this.y = y;
}
void move(String direction){
switch (direction){
case "up":
this.y -= 10;
break;
case "down":
this.y += 10;
break;
case "left":
this.x -= 10;
break;
case "right":
this.x += 10;
break;
default:
break;
}
}
}
class MyPanel extends JPanel{
MyRectangle user = new MyRectangle(10, 10);
public MyPanel(){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g.drawRect(user.x, user.y, 10, 10);
}
}
该代码基本上在JPanel上创建一个正方形,并根据用户的输入对其进行移动(向右箭头会将正方形向右移动10个像素).一切正常,除了延迟会破坏体验.
The code basically creates a square on a JPanel and moves it around according to the user's inputs (right arrow would move the square 10 pixels to the right). Everything works fine, except that there is a delay which ruins the experience.
我已经阅读了以下问题:忽略按键时的延迟被压住了?但可悲的是,它似乎仅适用于JavaScript.有帮助吗?
I had a read over this question: Ignoring delay when a key is held down? but sadly enough, it appears to only apply to JavaScript. Any help?
我也希望能链接到此类现有问题(我可能会错过).
I would also appreciate links to existing questions of this type (which I might have missed).
推荐答案
您现在正在做的是将current_key
设置为每100毫秒后停止",然后听keyPressed
再次分配方向.问题是您受到keyPressed
触发频率的限制.
What you are doing right now is setting current_key
to "stop" after each 100 ms, and listening for keyPressed
to assign a direction again. The problem with this is you are limited by how often keyPressed
is being fired.
您真正想做的是按下一个方向时开始移动,而松开该键时停止移动.
What you really want to do is start moving when a direction is pressed, and stop moving when the key is released.
一种简单的方法是从主循环中删除停止线,而在MyKeyListener
中创建一个keyReleased
方法,将current_key
设置为停止".
An easy way to do this is to remove the stop line from your main loop, and instead make a keyReleased
method in your MyKeyListener
which sets the current_key
to "stop".
这样,每当按下一个键时,我们就会一直朝那个方向移动,直到释放为止.
This way whenever a key is pressed, we keep moving in that direction until it is released.
例如
class gameplay {
static String current_key = "stop";
public gameplay() {
begin();
}
void begin() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Tutorial3.class.getName()).log(Level.SEVERE, null, ex);
}
Tutorial3.panel.user.move(current_key);
Tutorial3.panel.repaint();
System.out.println(current_key);
// current_key = "stop"; // only "stop" when the key is released
}
}
static void key_event() {}
}
class MyKeyListener extends KeyAdapter {
// set direction to "stop" when a key is released
@Override
public void keyReleased(KeyEvent e) {
gameplay.current_key = "stop";
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
gameplay.current_key = "up";
break;
case KeyEvent.VK_DOWN:
gameplay.current_key = "down";
break;
case KeyEvent.VK_LEFT:
gameplay.current_key = "left";
break;
case KeyEvent.VK_RIGHT:
gameplay.current_key = "right";
break;
default:
gameplay.current_key = "stop";
}
}
}
这篇关于按下按键和按住按键之间的延迟时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!