From 99588933cf6036c6b24bad6cc97c26f378aa67d0 Mon Sep 17 00:00:00 2001 From: Caspian Rychlik-Prince Date: Mon, 7 Apr 2003 15:21:09 +0000 Subject: [PATCH] Added some isCreated() methods to input classes and keyboard key name mapping --- src/java/org/lwjgl/input/Controller.java | 7 ++++ src/java/org/lwjgl/input/Keyboard.java | 39 +++++++++++++++++++ src/java/org/lwjgl/input/Mouse.java | 17 ++++++--- src/java/org/lwjgl/openal/BaseAL.java | 48 ++++++++++++------------ 4 files changed, 81 insertions(+), 30 deletions(-) diff --git a/src/java/org/lwjgl/input/Controller.java b/src/java/org/lwjgl/input/Controller.java index 82e11be9..f66174d8 100644 --- a/src/java/org/lwjgl/input/Controller.java +++ b/src/java/org/lwjgl/input/Controller.java @@ -158,6 +158,13 @@ public class Controller { created = true; } + /** + * @return true if the controller has been created + */ + public static boolean isCreated() { + return created; + } + /** * "Destroy" the controller */ diff --git a/src/java/org/lwjgl/input/Keyboard.java b/src/java/org/lwjgl/input/Keyboard.java index 9f63c08e..21fba8be 100644 --- a/src/java/org/lwjgl/input/Keyboard.java +++ b/src/java/org/lwjgl/input/Keyboard.java @@ -32,6 +32,8 @@ package org.lwjgl.input; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -184,6 +186,27 @@ public class Keyboard { public static final int KEY_APPS = 0xDD; /* AppMenu key */ public static final int KEY_POWER = 0xDE; public static final int KEY_SLEEP = 0xDF; + + /** Key names */ + private static final String[] keyName = new String[255]; + static { + // Use reflection to find out key names + Field[] field = Keyboard.class.getFields(); + try { + for (int i = 0; i < field.length; i++) { + if (Modifier.isStatic(field[i].getModifiers()) + && Modifier.isPublic(field[i].getModifiers()) + && Modifier.isFinal(field[i].getModifiers()) + && field[i].getType() == int.class + && field[i].getName().startsWith("KEY_")) { + keyName[field[i].getInt(null)] = field[i].getName().substring(4); + } + + } + } catch (Exception e) { + } + + } /** Has the keyboard been created? */ private static boolean created; @@ -260,6 +283,13 @@ public class Keyboard { */ private static native boolean nCreate(); + /** + * @return true if the keyboard has been created + */ + public static boolean isCreated() { + return created; + } + /** * "Destroy" the keyboard */ @@ -357,6 +387,15 @@ public class Keyboard { return keyDownBuffer.get(key) != 0; } + /** + * Gets a key's name + * @param key The key + * @return a String with the key's human readable name in it or null if the key is unnamed + */ + public static String getKeyName(int key) { + return keyName[key]; + } + /** * Gets the number of keyboard events waiting after doing a read(). * @return the number of keyboard events diff --git a/src/java/org/lwjgl/input/Mouse.java b/src/java/org/lwjgl/input/Mouse.java index 25dadf0e..306ec6a8 100644 --- a/src/java/org/lwjgl/input/Mouse.java +++ b/src/java/org/lwjgl/input/Mouse.java @@ -104,9 +104,9 @@ public class Mouse { if (!nCreate()) throw new Exception("The mouse could not be created."); created = true; - - //set mouse buttons - buttons = new boolean[buttonCount]; + + //set mouse buttons + buttons = new boolean[buttonCount]; } /** @@ -116,6 +116,13 @@ public class Mouse { */ private static native boolean nCreate(); + /** + * @return true if the mouse has been created + */ + public static boolean isCreated() { + return created; + } + /** * "Destroy" the mouse */ @@ -123,8 +130,8 @@ public class Mouse { if (!created) return; created = false; - buttons = null; - + buttons = null; + nDestroy(); } diff --git a/src/java/org/lwjgl/openal/BaseAL.java b/src/java/org/lwjgl/openal/BaseAL.java index e057a207..4f835124 100644 --- a/src/java/org/lwjgl/openal/BaseAL.java +++ b/src/java/org/lwjgl/openal/BaseAL.java @@ -29,7 +29,7 @@ * 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.openal; +package org.lwjgl.openal; import java.io.File; import java.util.StringTokenizer; @@ -49,25 +49,25 @@ import java.util.StringTokenizer; public abstract class BaseAL { /** Has the ALC object been created? */ protected static boolean created; - + static { initialize(); } - - /** + + /** * Override to provide any initialization code after creation. */ - protected void init() { + protected void init() throws Exception { } - + /** * Static initialization */ private static void initialize() { System.loadLibrary(org.lwjgl.Sys.getLibraryName()); } - - /** + + /** * Creates the AL instance * * @throws Exception if a failiure occured in the AL creation process @@ -83,49 +83,47 @@ public abstract class BaseAL { String libname; // libname is hardcoded atm - this will change in a near future... - libname = (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) - ? "libopenal.so" - : "OpenAL32.dll"; + libname = (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) ? "libopenal.so" : "OpenAL32.dll"; StringTokenizer st = new StringTokenizer(libpath, seperator); - + //create needed string array - String[] oalPaths = new String[st.countTokens()+1]; + String[] oalPaths = new String[st.countTokens() + 1]; //build paths - for(int i=0;i