How can I get the key text name?
Applies To: C++Builder 1 or higher
Category: Knowledge Base
- Using the OnKeyPress event I can obtain the name of any character keys. But other special keys such as Ctrl or Backspace can only be handled by OnKeyDown. Unfortunately, the Key parameter in OnKeyDown is a virtual-code. Can I get the real key name?
-

-
- Start a new project using File | New Application.
-
- Open the header file of your form and add the following under
- public:
-
- MESSAGE void __fastcall Key(TMessage &message);
-
- BEGIN_MESSAGE_MAP
- MESSAGE_HANDLER(WM_KEYDOWN, TMessage, Key);
- END_MESSAGE_MAP(TForm);
-
- Insert this in your source code:
-
- MESSAGE void __fastcall TForm1::Key(TMessage &message)
- {
- char name[30];
- GetKeyNameText(message.LParam, name, 30);
- ShowMessage("You pressed " + String(name));
- }
-
- The Win32 API function GetKeyNameText shows the name of the key. However this function requires that you catch the WM_KEYDOWN message.
C++Builder Developer's Network
Copyright © Yoto Yotov