[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Converting key value in OnKeyDown from Word to Char

Posted on 2012-09-14
Medium Priority
Last Modified: 2012-09-17
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;
    keyboardState: TKeyboardState;
    asciiResult: Integer;
    SetLength(Result, 2);
    asciiResult := ToAscii(key, MapVirtualKey(key, 0), keyboardState, @Result[1], 0);
    case asciiResult of
      1: SetLength(Result, 1);
      else Result := '';

Open in new window

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?
Question by:EricTaylor
  • 2
  • 2
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 38400612
You could use the OnkeyPress and the OnkeyDOwn combined togeter

OnKeyDown is trapped before of OnKeyPress, so you could just check in the OnKeyPress event any char key and let the other job for OnKeyDown

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  Showmessage(Format('KeyValue = %d', [Key]));

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
  // check if the key is a valid keyboard char
  if (ord(Key) >= 32) and (ord(Key) <=126) then

Author Comment

ID: 38400674
In this case, since OnKeyDown has information about the TshiftState and OnKeyPress doesn't, Whether I want to take action for example, if the A key is pressed, depends on whether ssCtrl or ssAlt are combined with it, so I think I need OnKeyDown rather than OnKeyPress.
LVL 23

Accepted Solution

Ferruccio Accalai earned 2000 total points
ID: 38401170
So if you don't need to check the case just use CHAR(KEy) in OnKeyDown to get the char value of the key
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  s, k: String;
  if ssShift in Shift then
    s := 'SHIFT pressed - ';
  if ssCTRL in Shift then
    s := s+ 'CTRL pressed - ';
  if ssALT in Shift then
    if ssShift in Shift then
      s := s+'ALT pressed - ';
  if (Key >= 32) and (Key <=126) then
    k := char(Key);
    s := s+(Format('Char = %s - ', [k]));
  s := s+Format('KeyValue = %d', [Key]);

Author Closing Comment

ID: 38407032
Thanks... didn't know one could cast the key as Char. That did the trick. I knew about handling the shift state, but appreciate the completeness of covering that in your example just in case.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question