All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.lessvoid.nifty.renderer.lwjgl.input.LwjglKeyboardInputEventCreator Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package de.lessvoid.nifty.renderer.lwjgl.input;

import de.lessvoid.nifty.input.keyboard.KeyboardInputEvent;
import org.lwjgl.input.Keyboard;

import javax.annotation.Nonnull;


/**
 * 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
   */
  @Nonnull
  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);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy