in my new 4k game th ekeyboard is very slow to respond keypresses. I was following the same ideia from kevglass source code of Neb4k:
public N() {
super("N");
setSize(640,510);
setResizable(false);
show();
enableEvents(56);
createBufferStrategy(2);
BufferStrategy strategy = getBufferStrategy();
long lastLoopTime = System.currentTimeMillis();
while (true)
{
int delta = (int) (System.currentTimeMillis() - lastLoopTime);
logic(delta);
lastLoopTime = System.currentTimeMillis();
draw((Graphics2D) strategy.getDrawGraphics());
strategy.show();
if (!isVisible()) {
System.exit(0);
}
}
}
In the logic method I test the controls variable to check what key was pressed.
The code for keyboard is:
protected void processKeyEvent(KeyEvent e)
{
int[] keys = new int[] {KeyEvent.VK_LEFT,KeyEvent.VK_RIGHT,KeyEvent.VK_UP,KeyEvent.VK_DOWN,KeyEvent.VK_SPACE};
for (int i=0;i<keys.length;i++)
{
if (e.getKeyCode() == keys[i]) {
controls[i] = e.getID() == KeyEvent.KEY_PRESSED;
}
}
if (e.getKeyCode() == 27)
{
System.exit(0);
}
}
Do u have some tip about it? Why the keypresses take too long to respond with this structure.