I'm trying to figure out a way to reduce the repetitiveness of this code. I see a couple options: load an array of all the cases from a simplified data file, or declare an array of all the cases.
I could change it to a switch, but I don't feel like having 50 break statements clogging up my source.
The code converts an integer that represents the event of an asynchronous key's state, to a string representing the event's signal in human-readable form. Only the relevant signals are being handled, nothing like mouse clicks, F keys, arrows, etc...
There is no coreleation between the number and the ASCII value for those characters, only to the extent of how it is defined by the Windows operating system. If only there was a function that could do this conversion for me. Any ideas?
boolean getShift () {
return (GetAsyncKeyState (160) || GetAsyncKeyState (161));
}
boolean getCase () {
return (GetKeyState (20) & 1 ? getShift () == false : getShift () == true);
}
void keyToString (unsigned char key, char *result) {
boolean isUpper = getCase ();
result[0] = 0;
if (key >= 'A' && key <= 'Z') {
result[0] = key + (isUpper ? 0 : 32);
result[1] = 0;
}
else 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 if (key == 48) {
strcpy (result, isUpper ? ")" : "0");
}
else if (key == 49) {
strcpy (result, isUpper ? "!" : "1");
}
else if (key == 50) {
strcpy (result, isUpper ? "@" : "2");
}
else if (key == 51) {
strcpy (result, isUpper ? "#" : "3");
}
else if (key == 52) {
strcpy (result, isUpper ? "$" : "4");
}
else if (key == 53) {
strcpy (result, isUpper ? "%" : "5");
}
else if (key == 54) {
strcpy (result, isUpper ? "^" : "6");
}
else if (key == 55) {
strcpy (result, isUpper ? "&" : "7");
}
else if (key == 56) {
strcpy (result, isUpper ? "*" : "8");
}
else if