View Single Post
Old Nov 7th, 2006, 12:47 PM   #4
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
Quote:
Originally Posted by grumpy View Post
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, "{|}\"");
                }
      }
}
Nice idea, it'd probably look a little neater with a switch though, IMO.
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote