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

How to translate a virtual-key to ASCII code

By Zarko Gajic, About.com

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

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. 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.