我正在使用全屏独家小游戏,我需要能够从播放器接收键盘输入。
在我的程序中,我有一个Window,我将其设置为Full Screen Exclusive,以及一个渲染循环。
创建窗口:
private void initialize() {
//This is used for my game loop...
running = true;
//Create the instance variable 'window' here.
window = new Window(null);
//Ignoring OS paint requests...
window.setIgnoreRepaint(true);
//Set the window to full screen exclusive.
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);
...
游戏循环:
private void loop() {
Graphics graphics = window.getGraphics();
graphics.setColor(Color.CYAN);
graphics.fillRect(0, 0, 1920, 1080);
graphics.dispose();
}
我的进口:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
现在,这很好用。就像它应该的那样。它在我的屏幕上呈现青色矩形,然而,我需要能够检测到用户何时按下按键,例如按Escape
关闭程序,等等。我不知道该怎么做。 :(
我尝试在KeyListener
添加window
(无效)。
我尝试在JPanel
添加window
并添加一个监听器(也没用)。
我曾尝试过关注我的JPanel
并做同样的事情。
我尝试使用KeyListener创建JFrame
,然后将其传递给我的window
构造函数。
我尝试将带有键绑定的JFrame
传递给我的window
,而不是KeyListener。
显然,以上都没有奏效。 (错误没有被抛出,当我按下一个键或使用sysout
退出程序时,我根本无法让程序输出我的System.exit(int);
中的文本。我已经取出了所有的东西从上面的代码中为我工作;我目前有一个窗口和一个游戏循环。 如果还有其他任何方法可以让我全屏显示Key Input,请通知我。(我觉得好像有专门用于全屏独家的传统方式,但我还没有找到还是一个。)或者如果你相信有办法让我尝试过的方法之一,(也许你相信我做错了什么),请告诉我。 (在这一点上我有点绝望)。
答案 0 :(得分:2)
使用键绑定的示例。非常简单,如果您如此倾向,也会演示使用DisplayMode
,但是一旦它运行,只需按住空格,它就会更新,释放它,它会更新。双击关闭;)
import java.awt.DisplayMode;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setUndecorated(true);
f.add(new TestPane());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (device.isFullScreenSupported()) {
device.setFullScreenWindow(f);
if (device.isDisplayChangeSupported()) {
try {
List<DisplayMode> matchingModes = new ArrayList<>(25);
DisplayMode[] modes = device.getDisplayModes();
for (DisplayMode mode : modes) {
if (mode.getWidth() == 1280 && mode.getHeight() == 720) {
matchingModes.add(mode);
}
}
if (!matchingModes.isEmpty()) {
for (DisplayMode mode : matchingModes) {
try {
device.setDisplayMode(mode);
System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate());
break;
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
System.err.println("!! No matching modes available");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("Change display mode not supported");
}
} else {
System.err.println("Full screen not supported");
}
}
public static class TestPane extends JPanel {
private boolean spaced = false;
public TestPane() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocusInWindow(true);
if (e.getClickCount() == 2) {
SwingUtilities.windowForComponent(TestPane.this).dispose();
}
}
});
InputMap im = getInputMap();
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), "spaced-pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true), "spaced-released");
am.put("spaced-pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
spaced = true;
repaint();
}
});
am.put("spaced-released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
spaced = false;
repaint();
}
});
requestFocusInWindow(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
String text = getWidth() + "x" + getHeight();
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = (getHeight() - fm.getHeight()) / 2;
g.drawString(text, x, y + fm.getAscent());
GraphicsDevice device = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode mode = device.getDisplayMode();
text = mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate();
x = (getWidth() - fm.stringWidth(text)) / 2;
y += fm.getHeight();
g.drawString(text, x, y + fm.getAscent());
text = "Spaced [" + spaced + "]";
x = (getWidth() - fm.stringWidth(text)) / 2;
y += fm.getHeight();
g.drawString(text, x, y + fm.getAscent());
}
}
}
答案 1 :(得分:1)
我认为击键的默认事件处理是你只能从一个可聚焦的组件,即文本字段中获取它们。但是,引用此post,您可以尝试向KeyEventDispatcher
添加自定义KeyboardFocusManager
(我认为是AWT的基础事件处理)。