原文:http://wiki.lwjgl.org/wiki/LWJGL_Basics_2_%28Input%29
Introduction 介绍
LWJGL用Keyboard和Mouse类自己做输入处理。为此,必须轮询Keyboard和Mouse类。当你调用Display.update()方法时,这些都会被自动处理,因为在那些类里已经包含了poll()方法,所以无需再手动调用它。
The Mouse 鼠标类Mouse
可以用以下方法取到鼠标在窗口上的位置:
int x = Mouse.getX(); // will return the X coordinate on the Display.
int y = Mouse.getY(); // will return the Y coordinate on the Display.
0,0坐标处是画面左下角(和传统OpenGL里的保持一致)
如果鼠标被按下,Mouse.isButtonDown(int button)方法返回true,否则一直返回false。鼠标键位序号从0开始,到buttonCount结束。
boolean leftButtonDown = Mouse.isButtonDown(0); // is left mouse button down.
boolean rightButtonDown = Mouse.isButtonDown(1); // is right mouse button down.
The Keyboard 键盘类Keyboard
如果键盘上相应的键被按下,Keyboard.isKeyDown(int key)方法将返回true,否则一直返回false。键盘全键对应列表可以在这里查到。
boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_X); // is x key down.
The Event Buffer 事件缓冲
使用isKeyDown()或isButtonDown()方法已经效果很不错了,但是如果轮询效果不够快,仍然可能错过某些按下的键。
为了避免这个问题,可以使用事件缓冲。此缓冲区会保留窗口收到的所有键盘鼠标事件,使用getEvent()方法可以访问此缓冲区。
next()方法用来遍历缓冲区。每次调用此方法,就会指向缓冲区里的下一个键盘/鼠标事件,直到没有新事件了,它会返回false。
比如键盘缓冲区可以像下面这样遍历:
while (Keyboard.next()) {
// get event key here
}
Keyboard.getEventKey()方法将返回本事件是由哪个按键引发的。
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
System.out.println("A Key Event");
}
如果某个事件对应按键处于按住状态,Keyboard.getEventState()方法返回true,松开后返回false。
使用以上俩方法就可以知道事件对应哪个键按下,以及此键目前被按住还是已经放开。
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
if (Keyboard.getEventKeyState()) {
System.out.println("A Key Pressed");
}
else {
System.out.println("A Key Released");
}
}
下面的例子就是遍历缓冲区并且检查A键的事件。
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
System.out.println("A Key Pressed");
}
}
else {
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
System.out.println("A Key Released");
}
}
}
键盘类Keyboard和鼠标类Mouse完整的API可以在它们的javadoc页面找到。
Example 例子
一个完整的例子:
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class InputExample {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL here
while (!Display.isCloseRequested()) {
// render OpenGL here
pollInput();
Display.update();
}
Display.destroy();
}
public void pollInput() {
if (Mouse.isButtonDown(0)) {
int x = Mouse.getX();
int y = Mouse.getY();
System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
System.out.println("SPACE KEY IS DOWN");
}
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
System.out.println("A Key Pressed");
}
if (Keyboard.getEventKey() == Keyboard.KEY_S) {
System.out.println("S Key Pressed");
}
if (Keyboard.getEventKey() == Keyboard.KEY_D) {
System.out.println("D Key Pressed");
}
} else {
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
System.out.println("A Key Released");
}
if (Keyboard.getEventKey() == Keyboard.KEY_S) {
System.out.println("S Key Released");
}
if (Keyboard.getEventKey() == Keyboard.KEY_D) {
System.out.println("D Key Released");
}
}
}
}
public static void main(String[] argv) {
InputExample inputExample = new InputExample();
inputExample.start();
}
}