Package de.lessvoid.nifty.slick2d.input

Source Code of de.lessvoid.nifty.slick2d.input.LwjglKeyboardInputEventCreator

package de.lessvoid.nifty.slick2d.input;

import org.lwjgl.input.Keyboard;

import de.lessvoid.nifty.input.keyboard.KeyboardInputEvent;


/**
* KeyboardInputEventCreator.
* @author void
*/
public class LwjglKeyboardInputEventCreator {

  /**
   * shiftDown.
   */
  private boolean shiftDown = false;

  /**
   * controlDown.
   */
  private boolean controlDown = false;

  /**
   * create KeyboardInputEvent.
   * @param key key
   * @param character character
   * @param keyDown keyDown
   * @return event
   */
  public KeyboardInputEvent createEvent(final int key, final char character, final boolean keyDown) {
    if (isShiftDown(key, keyDown)) {
      shiftDown = true;
    } else if (isShiftUp(key, keyDown)) {
      shiftDown = false;
    } else if (isControlDown(key, keyDown)) {
      controlDown = true;
    } else if (isControlUp(key, keyDown)) {
      controlDown = false;
    }
    // because Nifty uses the same keyboard encoding like lwjgl does, we can directly forward
    // the keyboard event to Nifty without the need for conversion
    return new KeyboardInputEvent(key, character, keyDown, shiftDown, controlDown);
  }

  /**
   * checks if the shift key is given.
   * @param key key
   * @return true when shift has been pressed and false otherwise
   */
  private boolean isShiftKey(final int key) {
    return key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT;
  }

  /**
   * check if shift is down.
   * @param key key to check
   * @param keyDown keyDown
   * @return true when left or right shift has been pressed
   */
  private boolean isShiftDown(final int key, final boolean keyDown) {
    return keyDown && isShiftKey(key);
  }

  /**
   * check if shift is up.
   * @param key key
   * @param keyDown keyDown
   * @return true when left or right shift has been released
   */
  private boolean isShiftUp(final int key, final boolean keyDown) {
    return !keyDown && isShiftKey(key);
  }

  /**
   * check if the given key is the controlKey.
   * @param key key
   * @return true left or right control key pressed and false otherwise
   */
  private boolean isControlKey(final int key) {
    return key == Keyboard.KEY_RCONTROL || key == Keyboard.KEY_LCONTROL || key == Keyboard.KEY_LMETA || key == Keyboard.KEY_RMETA;
  }

  /**
   * check if control key is down.
   * @param key key
   * @param keyDown keyDown
   * @return controlDown
   */
  private boolean isControlDown(final int key, final boolean keyDown) {
    return keyDown && isControlKey(key);
  }

  /**
   * check if control key is up.
   * @param key key
   * @param keyDown keyDown
   * @return controlDown
   */
  private boolean isControlUp(final int key, final boolean keyDown) {
    return !keyDown && isControlKey(key);
  }
}
TOP

Related Classes of de.lessvoid.nifty.slick2d.input.LwjglKeyboardInputEventCreator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.