1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.jme3.input.lwjgl; 34 35 import com.jme3.input.KeyInput; 36 import com.jme3.input.RawInputListener; 37 import com.jme3.input.event.KeyInputEvent; 38 import com.jme3.system.lwjgl.LwjglAbstractDisplay; 39 import com.jme3.system.lwjgl.LwjglTimer; 40 import java.util.logging.Level; 41 import java.util.logging.Logger; 42 import org.lwjgl.LWJGLException; 43 import org.lwjgl.Sys; 44 import org.lwjgl.input.Keyboard; 45 46 public class LwjglKeyInput implements KeyInput { 47 48 private static final Logger logger = Logger.getLogger(LwjglKeyInput.class.getName()); 49 50 private LwjglAbstractDisplay context; 51 52 private RawInputListener listener; 53 LwjglKeyInput(LwjglAbstractDisplay context)54 public LwjglKeyInput(LwjglAbstractDisplay context){ 55 this.context = context; 56 } 57 initialize()58 public void initialize() { 59 if (!context.isRenderable()) 60 return; 61 62 try { 63 Keyboard.create(); 64 Keyboard.enableRepeatEvents(true); 65 logger.info("Keyboard created."); 66 } catch (LWJGLException ex) { 67 logger.log(Level.SEVERE, "Error while creating keyboard.", ex); 68 } 69 } 70 getKeyCount()71 public int getKeyCount(){ 72 return Keyboard.KEYBOARD_SIZE; 73 } 74 update()75 public void update() { 76 if (!context.isRenderable()) 77 return; 78 79 Keyboard.poll(); 80 while (Keyboard.next()){ 81 int keyCode = Keyboard.getEventKey(); 82 char keyChar = Keyboard.getEventCharacter(); 83 boolean pressed = Keyboard.getEventKeyState(); 84 boolean down = Keyboard.isRepeatEvent(); 85 long time = Keyboard.getEventNanoseconds(); 86 KeyInputEvent evt = new KeyInputEvent(keyCode, keyChar, pressed, down); 87 evt.setTime(time); 88 listener.onKeyEvent(evt); 89 } 90 } 91 destroy()92 public void destroy() { 93 if (!context.isRenderable()) 94 return; 95 96 Keyboard.destroy(); 97 logger.info("Keyboard destroyed."); 98 } 99 isInitialized()100 public boolean isInitialized() { 101 return Keyboard.isCreated(); 102 } 103 setInputListener(RawInputListener listener)104 public void setInputListener(RawInputListener listener) { 105 this.listener = listener; 106 } 107 getInputTimeNanos()108 public long getInputTimeNanos() { 109 return Sys.getTime() * LwjglTimer.LWJGL_TIME_TO_NANOS; 110 } 111 112 } 113