import java.awt.*;

/*QWERTYtoAZERTYTextField copyright 1999 David Faden
You are free to use, reuse, and redistribute this code for any purpose what-so-ever.
Use it at your own risk.
The TextComponents of Netscape and Internet Explorer's JVMs running on Macintoshes with AZERTY keyboards do not receive the correct key events--they receive the corresponding QWERTY character rather than the user pressed AZERTY key.  This class simply switches QWERTY input to AZERTY output. It does not know whether this is the correct action to take.

Brought to my attention by Guy Capra
Now trying to fix bug in original, that seemed to cause a doubling of input from AZERTY...
First I will try ignoring the second character of every pair
*/
public class QWERTYtoAZERTYTextField2 extends TextField {
  private boolean secondConsumed=true;

  public QWERTYtoAZERTYTextField2() {
    super();
  }
  
  public QWERTYtoAZERTYTextField2(int cols) {
    super(cols);
  }
  
  public QWERTYtoAZERTYTextField2(String text) {
    super(text);
  }
  
  public QWERTYtoAZERTYTextField2(String text, int cols) {
    super(text,cols);
  }

  public boolean keyDown(Event e, int key) {
    char c=QWERTYtoAZERTY((char)key);
    if(c!=0) {
      if(secondConsumed) {
        setText(getText()+c);
        //The following moves the cursor to the end of the text...
        select(getText().length(),getText().length());
        secondConsumed=false;
      }
      else secondConsumed=true;
      return true;
    }
    return false;
  }
  
  public static char QWERTYtoAZERTY(char qkey) {
    switch(qkey) {
      //normal
      case '`':
      return '2';
      
      case '1':
      return '&';
      
      case '2':
      return '';
      
      case '3':
      return '\"';
      
      case '4':
      return '\'';
      
      case '5':
      return '(';
      
      case '6':
      return '-';
      
      case '7':
      return '';
      
      case '8':
      return '_';
      
      case '9':
      return '';
      
      case '0':
      return '';
      
      case '-':
      return ')';
      
      case '=':
      return '=';
      
      //
      case 'q':
      return 'a';
      
      case 'w':
      return 'z';
      
      case 'e':
      return 'e';
      
      case 'r':
      return 'r';
      
      case 't':
      return 't';
      
      case 'y':
      return 'y';
      
      case 'u':
      return 'u';
      
      case 'i':
      return 'i';
      
      case 'o':
      return 'o';
      
      case 'p':
      return 'p';
      
      case '[':
      return '^';
      
      case ']':
      return '$';
      
      //didn't use backslash key '\' in previous row
      
      //
      case 'a':
      return 'q';
      
      case 's':
      return 's';
      
      case 'd':
      return 'd';
      
      case 'f':
      return 'f';
      
      case 'g':
      return 'g';
      
      case 'h':
      return 'h';
      
      case 'j':
      return 'j';
      
      case 'k':
      return 'k';
      
      case 'l':
      return 'l';
      
      case ';':
      return 'm';
      
      case '\'':
      return '';
      
      //
      case 'z':
      return '<';
      
      case 'x':
      return 'w';
      
      case 'c':
      return 'x';
      
      case 'v':
      return 'c';
      
      case 'b':
      return 'v';
      
      case 'n':
      return 'b';
      
      case 'm':
      return 'n';
      
      case ',':
      return ',';
      
      case '.':
      return ';';
      
      case '/':
      return ':';
      
      //These don't seem to match up...even with the TAB key, there
      //is still an extra character.
      /*case '':
      return '!';
      
      case '':
      return '*';*/
      
      //Capslocked ;
      
      case '~':
      return '1';
      
      case '!':
      return '2';
      
      case '@':
      return '3';
      
      case '#':
      return '4';
      
      case '$':
      return '5';
      
      case '%':
      return '6';
      
      case '^':
      return '7';
      
      case '&':
      return '8';
      
      case '*':
      return '9';
      
      case '(':
      return '0';
      
      case ')':
      return '';
      
      case '_':
      return '+';
      
      //
      case 'Q':
      return 'A';
      
      case 'W':
      return 'Z';
      
      case 'E':
      return 'E';
      
      case 'R':
      return 'R';
      
      case 'T':
      return 'T';
      
      case 'Y':
      return 'Y';
      
      case 'U':
      return 'U';
      
      case 'I':
      return 'I';
      
      case 'O':
      return 'O';
      
      case 'P':
      return 'P';
      
      case '{':
      return '';
      
      case '}':
      return '';
      
      //
      case 'A':
      return 'Q';
      
      case 'S':
      return 'S';
      
      case 'D':
      return 'D';
      
      case 'F':
      return 'F';
      
      case 'G':
      return 'G';
      
      case 'H':
      return 'H';
      
      case 'J':
      return 'J';
      
      case 'K':
      return 'K';
      
      case 'L':
      return 'L';
      
      case ':':
      return 'M';
      
      case '\"':
      return '%';
      
      //
      case 'Z':
      return '>';
      
      case 'X':
      return 'W';
      
      case 'C':
      return 'X';
      
      case 'V':
      return 'C';
      
      case 'B':
      return 'V';
      
      case 'N':
      return 'B';
      
      case 'M':
      return 'N';
      
      case '<':
      return '?';
      
      case '>':
      return '.';
      
      case '?':
      return '/';
      
      //There don't seem to be enough characters on the QWERTY keyboard
      //to handle these...
      /*case '':
      return '';
      
      case '':
      return '';*/
      
      //
        default:
          return 0;
        }
  }
  
}
