1. Home
  2. Computing & Technology
  3. Delphi Programming

How to translate a virtual-key to ASCII code

By , About.com Guide

Here's how to get the character from the virtual key:

~~~~~~~~~~~~~~~~~~~~~~~~~
function GetCharFromVKey(vkey: Word): string;
var
   keystate: TKeyboardState;
   retcode: Integer;
begin
   Win32Check(GetKeyboardState(keystate)) ;
   SetLength(Result, 2) ;
   retcode := ToAscii(vkey,
     MapVirtualKey(vkey, 0),
     keystate, @Result[1],
     0) ;
   case retcode of
     0: Result := '';
     1: SetLength(Result, 1) ;
     2: ;
     else
       Result := '';
   end;
end;

{
Usage:
procedure TForm1.Edit1KeyDown
   (Sender: TObject; var Key: Word;
   Shift: TShiftState) ;
begin
   ShowMessage(GetCharFromVKey(Key)) ;
end;
}
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to place a TCheckBox inside a TRichEdit
« Delphi Split / Tokenizer functions

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2002 Delphi Tips
  7. How to translate a virtual-key to ASCII code

©2009 About.com, a part of The New York Times Company.

All rights reserved.