0

Looking to translate a character into an integer for use with awt robots key controls. I have this solution which works right now but its long, messy, and I'll need to add every single possible key that could be entered, which is a lot. Is there a simpler or neater way to complete this task? Thanks!

(I'm using +10000 for capital letters right now, I'll just run a check while I'm typing the chars and say if num>10000, hold shift and remove 10000 from the number)

public int charToNum(char character) {
    switch (character) {
        case 'a': return(KeyEvent.VK_A);
        case 'b': return(KeyEvent.VK_B);
        case 'c': return(KeyEvent.VK_C);
        case 'd': return(KeyEvent.VK_D);
        case 'e': return(KeyEvent.VK_E);
        case 'f': return(KeyEvent.VK_F);
        case 'g': return(KeyEvent.VK_G);
        case 'h': return(KeyEvent.VK_H);
        case 'i': return(KeyEvent.VK_I);
        case 'j': return(KeyEvent.VK_J);
        case 'k': return(KeyEvent.VK_K);
        case 'l': return(KeyEvent.VK_L);
        case 'm': return(KeyEvent.VK_M);
        case 'n': return(KeyEvent.VK_N);
        case 'o': return(KeyEvent.VK_O);
        case 'p': return(KeyEvent.VK_P);
        case 'q': return(KeyEvent.VK_Q);
        case 'r': return(KeyEvent.VK_R);
        case 's': return(KeyEvent.VK_S);
        case 't': return(KeyEvent.VK_T);
        case 'u': return(KeyEvent.VK_U);
        case 'v': return(KeyEvent.VK_V);
        case 'w': return(KeyEvent.VK_W);
        case 'x': return(KeyEvent.VK_X);
        case 'y': return(KeyEvent.VK_Y);
        case 'z': return(KeyEvent.VK_Z);
        case 'A': return(KeyEvent.VK_A);
        case 'B': return(10000 + KeyEvent.VK_B);
        case 'C': return(10000 + KeyEvent.VK_C);
        case 'D': return(10000 + KeyEvent.VK_D);
        case 'E': return(10000 + KeyEvent.VK_E);
        case 'F': return(10000 + KeyEvent.VK_F);
        case 'G': return(10000 + KeyEvent.VK_G);
        case 'H': return(10000 + KeyEvent.VK_H);
        case 'I': return(10000 + KeyEvent.VK_I);
        case 'J': return(10000 + KeyEvent.VK_J);
        case 'K': return(10000 + KeyEvent.VK_K);
        case 'L': return(10000 + KeyEvent.VK_L);
        case 'M': return(10000 + KeyEvent.VK_M);
        case 'N': return(10000 + KeyEvent.VK_N);
        case 'O': return(10000 + KeyEvent.VK_O);
        case 'P': return(10000 + KeyEvent.VK_P);
        case 'Q': return(10000 + KeyEvent.VK_Q);
        case 'R': return(10000 + KeyEvent.VK_R);
        case 'S': return(10000 + KeyEvent.VK_S);
        case 'T': return(10000 + KeyEvent.VK_T);
        case 'U': return(10000 + KeyEvent.VK_U);
        case 'V': return(10000 + KeyEvent.VK_V);
        case 'W': return(10000 + KeyEvent.VK_W);
        case 'X': return(10000 + KeyEvent.VK_X);
        case 'Y': return(10000 + KeyEvent.VK_Y);
        case 'Z': return(10000 + KeyEvent.VK_Z);
        case '!': return(KeyEvent.VK_EXCLAMATION_MARK);
        case ' ': return(KeyEvent.VK_SPACE);   
        default: System.out.println("Test"); return 0;
    }
}
Nathan
  • 1,287
  • 6
  • 15
  • 32
  • 1
    Nope, that's just about the only way to do it. Capital's should be generated by pressing the `SHIFT` key first... – MadProgrammer Jul 04 '14 at 03:26
  • Ah that really sucks :/ Oh well! And see the comment just above the code, Im using the +10000 in this method but when I do the typing, I'll hold shift – Nathan Jul 04 '14 at 03:28

1 Answers1

1

Looking at the source code the values for the KeyEvent fields are

public static final int VK_A = 65;
public static final int VK_B = 66;
...
public static final int VK_Z = 90;

which are just ascii values for A-Z. You can cast the char to an int to get its ascii value which means you could do something like this.

public static int charToNum(char inputChar) {
    if(inputChar == '!') {
        return(KeyEvent.VK_EXCLAMATION_MARK);
    } else if (inputChar == ' ') {
        return(KeyEvent.VK_SPACE);
    }

    if (Character.isUpperCase(inputChar)) {
        return (int) inputChar + 10000;
    } else {
        return (int) inputChar - 32;
    }
}

however any non alphabet character will have to have its own special case like the exclamation mark, and space.

Riverchimp
  • 404
  • 5
  • 14