Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:22:49+00:00 2026-06-05T07:22:49+00:00

I am using this code to convert a virtual key to WideString: function VKeytoWideString

  • 0

I am using this code to convert a virtual key to WideString:

function VKeytoWideString (Key : Word) : WideString; 
var 
 WBuff         : array [0..255] of WideChar; 
 KeyboardState : TKeyboardState; 
 UResult       : Integer;
begin 
 Result := '';
 GetKeyBoardState (KeyboardState); 
 ZeroMemory(@WBuff[0], SizeOf(WBuff));
 UResult := ToUnicode(key, MapVirtualKey(key, 0), KeyboardState, WBuff, Length(WBuff), 0); 
 if UResult > 0 then
  SetString(Result, WBuff, UResult)
 else if UResult = -1 then
  Result := WBuff;
end; 

It works fine on my PC, but on a Chinese PC I get this:

foo

It converts the Chinese chars to Hanyu Pinyin. I think the function actually returns the raw input of the keyboard and not what the user actually wants to type in.

How should I handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:22:51+00:00Added an answer on June 5, 2026 at 7:22 am

    As per the comments, here is an example of how you can avoid the problem by handling KeyPress events instead of manually converting KeyDown events. The TNT controls don’t provide a WideChar KeyPress event, but it’s fairly easy to add. Ideally, you should not put the extensions to TTntMemo and TTntForm in derived classes as I’ve done here, but instead modify the TNT source code.

    The form contains two TTntMemo controls. Pressing keys in the first will log the events in the second.

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, TntForms, StdCtrls, TntStdCtrls;
    
    type
      TKeyPressWEvent = procedure(Sender: TObject; var Key: WideChar) of object;
    
      TTntMemo = class(TntStdCtrls.TTntMemo)
      private
        FOnKeyPressW: TKeyPressWEvent;
        procedure WMChar(var Msg: TWMChar); message WM_CHAR;
      protected
        function DoKeyPressW(var Message: TWMKey): Boolean;
        procedure KeyPressW(var Key: WideChar);
      published
        property OnKeyPressW: TKeyPressWEvent read FOnKeyPressW write FOnKeyPressW;
      end;
    
      TTntForm = class(TntForms.TTntForm)
      private
        FOnKeyPressW: TKeyPressWEvent;
        procedure WMChar(var Msg: TWMChar); message WM_CHAR;
      protected
        function DoKeyPressW(var Message: TWMKey): Boolean;
        procedure KeyPressW(var Key: WideChar);
      published
        property OnKeyPressW: TKeyPressWEvent read FOnKeyPressW write FOnKeyPressW;
      end;
    
      TForm1 = class(TTntForm)
        TntMemo1: TTntMemo;
        TntMemo2: TTntMemo;
        procedure FormCreate(Sender: TObject);
        procedure FormKeyPressW(Sender: TObject; var Key: WideChar);
        procedure TntMemo1KeyPressW(Sender: TObject; var Key: WideChar);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    uses
      TntControls;
    
    {$R *.dfm}
    
    type
      TWinControlAccess = class(TWinControl);
      TTntFormAccess = class(TTntForm);
    
    function TntControl_DoKeyPressW(Self: TWinControl; var Message: TWMKey;
      KeyPressW: Pointer): Boolean;
    type
      TKeyPressWProc = procedure(Self: TWinControl; var Key: WideChar);
    var
      Form: TCustomForm;
      Ch: WideChar;
    begin
      Result := True;
      Form := GetParentForm(Self);
      if (Form <> nil) and (Form <> Self) and Form.KeyPreview then
      begin
        if (Form is TTntForm) and TTntFormAccess(Form).DoKeyPressW(Message) then Exit;
        if TWinControlAccess(Form).DoKeyPress(Message) then Exit;
      end;
      if not (csNoStdEvents in Self.ControlStyle) then
      begin
        Ch := GetWideCharFromWMCharMsg(Message);
        TKeyPressWProc(KeyPressW)(Self, Ch);
        SetWideCharForWMCharMsg(Message, Ch);
        if Ch = #0 then Exit;
      end;
      Result := False;
    end;
    
    { TTntMemo }
    
    function TTntMemo.DoKeyPressW(var Message: TWMKey): Boolean;
    begin
      Result := TntControl_DoKeyPressW(Self, Message, @TTntMemo.KeyPressW);
    end;
    
    procedure TTntMemo.KeyPressW(var Key: WideChar);
    begin
      if Assigned(FOnKeyPressW) then FOnKeyPressW(Self, Key);
    end;
    
    procedure TTntMemo.WMChar(var Msg: TWMChar);
    begin
      if not DoKeyPressW(Msg) then inherited;
    end;
    
    { TTntForm }
    
    function TTntForm.DoKeyPressW(var Message: TWMKey): Boolean;
    begin
      Result := TntControl_DoKeyPressW(Self, Message, @TTntForm.KeyPressW);
    end;
    
    procedure TTntForm.KeyPressW(var Key: WideChar);
    begin
      if Assigned(FOnKeyPressW) then FOnKeyPressW(Self, Key);
    end;
    
    procedure TTntForm.WMChar(var Msg: TWMChar);
    begin
      if not DoKeyPressW(Msg) then inherited;
    end;
    
    { TForm1 }
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Self.OnKeyPressW := FormKeyPressW;
      TntMemo1.OnKeyPressW := TntMemo1KeyPressW;
    end;
    
    procedure TForm1.FormKeyPressW(Sender: TObject; var Key: WideChar);
    begin
      TntMemo2.Lines.Add(WideString('FormKeyPress: ') + Key);
    end;
    
    procedure TForm1.TntMemo1KeyPressW(Sender: TObject; var Key: WideChar);
    begin
      TntMemo2.Lines.Add(WideString('TntMemo1KeyPress: ') + Key);
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this code to attempt to convert a float to an int, then
I am using this code for on android just trying to convert string to
Using this code var html='<div class=somediv></div>'; var target=document.getElementById('contentArea'); target.appendChild(html); I'm getting Uncaught Error: NOT_FOUND_ERR:
Im using this code for mysql connection $con = mysql_connect(localhost:/var/lib/mysql/mysql.sock, abc , xyz); if
While using this code to serialize an object: public object Clone() { var serializer
When using this code (simplified for asking): var rows1 = (from t1 in db.TABLE1
I am using this code to convert a Set to a List : Map<String,
i am using this jquery function to display a pop window. in this code
I'm using this code to convert a jpg image into a png 8. The
I'm using this code to convert bytes into more readable format, e.g. 155K, 1.5M,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.