Home | History | Annotate | Download | only in event
      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.event;
     34 
     35 import com.jme3.input.KeyInput;
     36 
     37 /**
     38  * Keyboard key event.
     39  *
     40  * @author Kirill Vainer
     41  */
     42 public class KeyInputEvent extends InputEvent {
     43 
     44     private int keyCode;
     45     private char keyChar;
     46     private boolean pressed;
     47     private boolean repeating;
     48 
     49     public KeyInputEvent(int keyCode, char keyChar, boolean pressed, boolean repeating) {
     50         this.keyCode = keyCode;
     51         this.keyChar = keyChar;
     52         this.pressed = pressed;
     53         this.repeating = repeating;
     54     }
     55 
     56     /**
     57      * Returns the key character. Returns 0 if the key has no character.
     58      *
     59      * @return the key character. 0 if the key has no character.
     60      */
     61     public char getKeyChar() {
     62         return keyChar;
     63     }
     64 
     65     /**
     66      * The key code.
     67      * <p>
     68      * See KEY_*** constants in {@link KeyInput}.
     69      *
     70      * @return key code.
     71      */
     72     public int getKeyCode() {
     73         return keyCode;
     74     }
     75 
     76     /**
     77      * Returns true if this event is key press, false is it was a key release.
     78      *
     79      * @return true if this event is key press, false is it was a key release.
     80      */
     81     public boolean isPressed() {
     82         return pressed;
     83     }
     84 
     85     /**
     86      * Returns true if this event is a repeat event. Not used anymore.
     87      *
     88      * @return true if this event is a repeat event
     89      */
     90     public boolean isRepeating() {
     91         return repeating;
     92     }
     93 
     94     /**
     95      * Returns true if this event is a key release, false if it was a key press.
     96      *
     97      * @return true if this event is a key release, false if it was a key press.
     98      */
     99     public boolean isReleased() {
    100         return !pressed;
    101     }
    102 
    103     @Override
    104     public String toString(){
    105         String str = "Key(CODE="+keyCode;
    106         if (keyChar != '\0')
    107             str = str + ", CHAR=" + keyChar;
    108 
    109         if (repeating){
    110             return str + ", REPEATING)";
    111         }else if (pressed){
    112             return str + ", PRESSED)";
    113         }else{
    114             return str + ", RELEASED)";
    115         }
    116     }
    117 }
    118