001package org.vishia.gral.awt;
002
003import java.awt.event.KeyEvent;
004
005import org.vishia.util.KeyCode;
006
007/**Adaption from AWT-KeyCodes to universal vishia key codes.
008 * @author Hartmut Schorrig
009 *
010 */
011public class AwtGralKey extends KeyCode
012{
013    /**Version, history and license
014   * <ul>
015   * <li>2015-30-10 Hartmut created for Key handling from SwtGralkeyin the GRAL. 
016   * </ul>
017   * 
018   * <b>Copyright/Copyleft</b>:
019   * For this source the LGPL Lesser General Public License,
020   * published by the Free Software Foundation is valid.
021   * It means:
022   * <ol>
023   * <li> You can use this source without any restriction for any desired purpose.
024   * <li> You can redistribute copies of this source to everybody.
025   * <li> Every user of this source, also the user of redistribute copies
026   *    with or without payment, must accept this license for further using.
027   * <li> But the LPGL is not appropriate for a whole software product,
028   *    if this source is only a part of them. It means, the user
029   *    must publish this part of source,
030   *    but don't need to publish the whole source of the own product.
031   * <li> You can study and modify (improve) this source
032   *    for own using or for redistribution, but you have to license the
033   *    modified sources likewise under this LGPL Lesser General Public License.
034   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
035   * </ol>
036   * If you are intent to use this sources without publishing its usage, you can get
037   * a second license subscribing a special contract with the author. 
038   * 
039   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
040   * 
041   */
042  @SuppressWarnings("hiding")
043  public final static int version = 20131116;
044
045  /**This class isn't instantiated. Only the static method and the static definitions of KeyCode
046   * are used.  */
047  private AwtGralKey()
048  { super(0);
049  }
050  
051  public static int convertFromAwt(int keyCode, int stateMask, char characterKey)
052  {
053    final int stateKeys, key;    
054    if((stateMask & (KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT)) == KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT){
055      stateKeys = ctrl + alt + shift;
056    } else if((stateMask & (KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT)) == KeyEvent.VK_ALT + KeyEvent.VK_CONTROL){
057      stateKeys = ctrl + alt;
058    } else if((stateMask & (KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT)) == KeyEvent.VK_CONTROL + KeyEvent.VK_SHIFT){
059      stateKeys = ctrl + shift;
060    } else if((stateMask & (KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT)) == KeyEvent.VK_ALT + KeyEvent.VK_SHIFT){
061      stateKeys = alt + shift;
062    } else if((stateMask & (KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT)) == KeyEvent.VK_CONTROL){
063      stateKeys = ctrl;
064    } else if((stateMask & (KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT)) == KeyEvent.VK_ALT){
065      stateKeys = alt;
066    } else if((stateMask & (KeyEvent.VK_CONTROL + KeyEvent.VK_ALT + KeyEvent.VK_SHIFT)) == KeyEvent.VK_SHIFT){
067      stateKeys = shift;
068    } else{
069      stateKeys = 0;
070    } 
071    switch(keyCode){
072      case KeyEvent.VK_LEFT:  key = left; break;
073      case KeyEvent.VK_RIGHT: key = right; break;
074      case KeyEvent.VK_UP:    key = up; break;
075      case KeyEvent.VK_DOWN:  key = dn; break;
076      case KeyEvent.VK_PAGE_UP:     key = pgup; break;
077      case KeyEvent.VK_PAGE_DOWN:   key = pgdn; break;
078      case KeyEvent.VK_F1:          key = F1; break;
079      case KeyEvent.VK_F2:          key = F2; break;
080      case KeyEvent.VK_F3:          key = F3; break;
081      case KeyEvent.VK_F4:          key = F4; break;
082      case KeyEvent.VK_F5:          key = F5; break;
083      case KeyEvent.VK_F6:          key = F6; break;
084      case KeyEvent.VK_F7:          key = F7; break;
085      case KeyEvent.VK_F8:          key = F8; break;
086      case KeyEvent.VK_F9:          key = F9; break;
087      case KeyEvent.VK_F10:         key = F10; break;
088      case KeyEvent.VK_F11:         key = F11; break;
089      case KeyEvent.VK_F12:         key = F12; break;
090      case KeyEvent.VK_HOME:        key = home; break;
091      case KeyEvent.VK_END:         key = end; break;
092      case KeyEvent.VK_INSERT:      key = ins; break;
093      case KeyEvent.VK_DELETE:      key = del; break;
094      case KeyEvent.VK_TAB:         key = tab; break;
095      case 0x0d:            key = enter; break;
096      case 0x08:            key = back; break;
097      case 0x1b:            key = esc; break;
098      default:{
099        //if(characterKey == )
100        key = 0;
101      }
102    }
103    if(key == 0 && characterKey >= ' ' && characterKey != KeyEvent.CHAR_UNDEFINED) {
104      if(stateKeys == shift ) {
105        return characterKey;
106      } else {
107        return stateKeys + characterKey;
108      }
109    } else {
110      return stateKeys + key;
111    }
112  }
113  
114  
115  
116  public enum MouseAction{
117    down, up, upMovedOutside, doubleClick
118  };
119  
120  
121  public static int convertMouseKey(int button, MouseAction action, int stateMask){
122    final int keyMouse;
123    switch(action){
124      case down:{
125        switch(button){ 
126          case 1: keyMouse = KeyCode.mouse1Down; break; 
127          case 3: keyMouse = KeyCode.mouse2Down; break;
128          case 2: keyMouse = KeyCode.mouse3Down; break;
129          default: keyMouse = KeyCode.mouse3Down; break;  //other key
130        }
131
132      } break;
133      case up:{
134        switch(button){ 
135          case   1: keyMouse = KeyCode.mouse1Up; break; 
136          case   3: keyMouse = KeyCode.mouse2Up; break;
137          case   2: keyMouse = KeyCode.mouse3Up; break;
138          default: keyMouse = KeyCode.mouse3Up; break;  //other key
139        }
140        
141      } break;
142      case upMovedOutside:{
143        switch(button){ 
144          case   1: keyMouse = KeyCode.mouse1UpMoved; break; 
145          case   3: keyMouse = KeyCode.mouse2UpMoved; break;
146          case   2: keyMouse = KeyCode.mouse3Up; break;
147          default: keyMouse = KeyCode.mouse3Up; break;  //other key
148        }
149        
150      } break;
151      case doubleClick:{
152        switch(button){ 
153          case 1: keyMouse = KeyCode.mouse1Double; break; 
154          case 3: keyMouse = KeyCode.mouse2Double; break;
155          case 2: keyMouse = KeyCode.mouse3Down; break;
156          default: keyMouse = KeyCode.mouse3Down; break;  //other key
157        }
158        
159      } break;
160      default: keyMouse = 0;  //unused, only because compiling error
161    }
162    final int keyCode = AwtGralKey.convertFromAwt(keyMouse, stateMask, '\0');
163    return keyCode;
164  }
165  
166  
167
168}