View Single Post
Old Nov 4th, 2006, 9:56 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
I haven't tested this with a compiler, but this should be enough to get you started.

Basic notion is that you have some ranges that you are mapping, and finding a technique to map anything in that range.
boolean getShift () {
        return (GetAsyncKeyState (160) || GetAsyncKeyState (161));
}

boolean getCase () {
        return (GetKeyState (20) & 1 ? !getShift () : getShift ());
}

char ExtractChar(unsigned char key, int lower, int upper, const char *data)
{
     /* return mapped result, or zero if we can't do the mapping */

     return (key >= lower && key <= upper) ? data[key - lower]: '\0';
}

void keyToString (unsigned char key, char *result) {         
        boolean isUpper = getCase ();
     
        if (key >= 96 && key <= 105) {
                result[0] = key-48;
                result[1] = 0;
        }
        else if (key == 8) {
                strcpy (result, "^backspace^");
        }
        else if (key == 9) {
                strcpy (result, "^tab^");
        }
        else if (key == 13) {
                strcpy (result, "\n");
        }
        else if (key == 17) {
                strcpy (result, "^ctrl^");
        }
        else if (key == 18) {
                strcpy (result, "^alt^");
        }
        else if (key == 32) {
                strcpy (result, " ");
        }
        else if (key == 46) {
                strcpy (result, "^delete^");
        }
        else {
                result[1] = 0;
                if (!isUpper) {
                        if (key >= 'A' && key <= 'Z')
                               result[0] = key + 32;
                        else
                               result[0] = ExtractChar(key, 48, 57, "0123456789") ||
                                           ExtractChar(key, 186, 192, ";=,-./`") ||
                                           ExtractChar(key, 219, 222, "[\\]'");
                }
                else if (isUpper) {
                          if (key >= 'A' && key <= 'Z')
                                result[0] = key;
                          else
                                result[0] = ExtractChar(key, 48, 57, ")!@#$%^&*(") ||
                                            ExtractCha(key, 186, 192, ":+<_>?~") ||
                                            Extract(key, 219, 222, "{|}\"");
                }
      }
}
grumpy is offline   Reply With Quote