lwjgl2-arm64/src/java/org/lwjgl/test/input/KeyboardTest.java

225 lines
5.7 KiB
Java
Raw Normal View History

2004-06-12 22:28:34 +02:00
/*
* Copyright (c) 2002-2004 LWJGL Project
2003-08-17 18:58:19 +02:00
* All rights reserved.
2004-06-12 22:28:34 +02:00
*
2003-08-17 18:58:19 +02:00
* Redistribution and use in source and binary forms, with or without
2004-06-12 22:28:34 +02:00
* modification, are permitted provided that the following conditions are
2003-08-17 18:58:19 +02:00
* met:
2004-06-12 22:28:34 +02:00
*
* * Redistributions of source code must retain the above copyright
2003-08-17 18:58:19 +02:00
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
2004-06-12 22:28:34 +02:00
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
2003-08-17 18:58:19 +02:00
* from this software without specific prior written permission.
2004-06-12 22:28:34 +02:00
*
2003-08-17 18:58:19 +02:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2004-06-12 22:28:34 +02:00
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2003-08-17 18:58:19 +02:00
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2004-06-12 22:28:34 +02:00
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2003-08-17 18:58:19 +02:00
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.input;
import org.lwjgl.input.Keyboard;
2004-07-03 15:48:52 +02:00
import org.lwjgl.opengl.Display;
2004-07-06 18:08:17 +02:00
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.GLU;
2004-04-18 22:01:28 +02:00
import org.lwjgl.util.vector.Vector2f;
2003-08-17 18:58:19 +02:00
/**
* <br>
* Keyboard test
*
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision$
2006-03-23 20:32:21 +01:00
* $Id$
2003-08-17 18:58:19 +02:00
*/
public class KeyboardTest {
/** position of quad to draw */
private Vector2f position = new Vector2f(320.0f, 240.0f);
2003-08-17 18:58:19 +02:00
/** Display mode selected */
private DisplayMode displayMode;
2003-08-17 18:58:19 +02:00
/** Creates a new instance of MouseTest */
public KeyboardTest() {
}
private void initialize() {
// create display and opengl
setupDisplay(false);
try {
Keyboard.create();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
2004-07-22 16:56:40 +02:00
/**
* Sets the display mode for fullscreen mode
*/
protected boolean setDisplayMode() {
try {
// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
2004-07-22 16:56:40 +02:00
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 640,
"height=" + 480,
"freq=" + 60,
"bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()
});
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
2003-08-17 18:58:19 +02:00
private void setupDisplay(boolean fullscreen) {
try {
2004-07-22 16:56:40 +02:00
setDisplayMode();
2004-07-03 15:48:52 +02:00
Display.create();
2003-08-17 18:58:19 +02:00
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
initializeOpenGL();
2003-08-17 18:58:19 +02:00
}
private void initializeOpenGL() {
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
2003-08-17 18:58:19 +02:00
}
public void executeTest() {
initialize();
createKeyboard();
wiggleKeyboard();
Keyboard.destroy();
2004-07-03 15:48:52 +02:00
Display.destroy();
2003-08-17 18:58:19 +02:00
}
private void createKeyboard() {
try {
Keyboard.create();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
private void wiggleKeyboard() {
2004-07-03 15:48:52 +02:00
while (!Display.isCloseRequested()) {
Display.update();
2004-07-03 15:48:52 +02:00
if (!Display.isVisible()) {
2003-08-17 18:58:19 +02:00
try {
Thread.sleep(100);
} catch (InterruptedException inte) {
inte.printStackTrace();
}
continue;
}
//check keys, buffered
2003-10-07 13:23:12 +02:00
Keyboard.poll();
2003-08-17 18:58:19 +02:00
int count = Keyboard.getNumKeyboardEvents();
while (Keyboard.next()) {
2004-02-04 21:44:07 +01:00
System.out.println("Checking key:" + Keyboard.getKeyName(Keyboard.getEventKey()));
2006-11-20 09:04:56 +01:00
System.out.println("Pressed:" + Keyboard.getEventKeyState());
2004-02-05 17:23:04 +01:00
System.out.println("Key character: " + Keyboard.getEventCharacter());
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
2003-08-17 18:58:19 +02:00
return;
}
2006-07-01 01:37:12 +02:00
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
position.x += 1;
}
2003-08-17 18:58:19 +02:00
2006-07-01 01:37:12 +02:00
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
position.x -= 1;
}
2006-07-01 01:37:12 +02:00
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
position.y += 1;
}
2006-07-01 01:37:12 +02:00
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
position.y -= 1;
2003-08-17 18:58:19 +02:00
}
2006-07-01 01:37:12 +02:00
2003-08-17 18:58:19 +02:00
if (count > 0) {
System.out.println();
}
if (position.x < 0) {
2003-08-17 18:58:19 +02:00
position.x = 0;
} else if (position.x > 640 - 60) {
position.x = 640 - 60;
2003-08-17 18:58:19 +02:00
}
if (position.y < 0) {
2003-08-17 18:58:19 +02:00
position.y = 0;
} else if (position.y > 480 - 30) {
position.y = 480 - 30;
2003-08-17 18:58:19 +02:00
}
render();
try {
Thread.sleep(0);
} catch (Exception e) {
}
}
}
2003-08-17 18:58:19 +02:00
private void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
2003-08-17 18:58:19 +02:00
GL11.glBegin(GL11.GL_POLYGON);
2003-08-17 18:58:19 +02:00
{
float color = 1.0f;
GL11.glColor3f(color, color, color);
GL11.glVertex2f(position.x + 0.0f, position.y + 0.0f);
GL11.glVertex2f(position.x + 0.0f, position.y + 30.0f);
GL11.glVertex2f(position.x + 40.0f, position.y + 30.0f);
GL11.glVertex2f(position.x + 60.0f, position.y + 15.f);
GL11.glVertex2f(position.x + 40.0f, position.y + 0.0f);
2003-08-17 18:58:19 +02:00
}
GL11.glEnd();
2003-08-17 18:58:19 +02:00
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
KeyboardTest kt = new KeyboardTest();
kt.executeTest();
System.exit(0);
2003-08-17 18:58:19 +02:00
}
}