For a Delphi form, I'm using OnKeyDown to trap the key that's pressed, and then doing various actions. However, if (key >= 32) and (key <=126) then I need to append the key to a string. Since OnKeyDown returns a Word rather than a char, I need to convert the Word to the ASCII char equivalent (e.g., 65 -> A, etc.) Not wanting to manually write something that manually addresses each possible value (which would work buy which would be kludgey), and not knowing how to do this, I looked online and "borrowed" something like this:
function KeyToStr(Key: Word): string;
asciiResult := ToAscii(key, MapVirtualKey(key, 0), keyboardState, @Result, 0);
case asciiResult of
1: SetLength(Result, 1);
else Result := '';
This mostly works (though I confess to not understanding all of what it's doing), but when I press certain keys (e.g. Backspace consistently does it), when I append to the value to something I can display it in (using a DevExpress TcxEdit... though don't see why that would have a bearing), the characters turn to some seeming random foreign character set (often Chinese but not always).
Is there a better way to determine the character value of the key and/or can someone tell me how to keep this from picking some other set of characters?