Recent

Author Topic: Converting scan codes to real ascii character  (Read 344 times)

Milsa

  • Sr. Member
  • ****
  • Posts: 326
Converting scan codes to real ascii character
« on: February 04, 2025, 06:33:30 pm »
Is it possible to convert scan code from FormKeyDown and FormKeyUp to character in FormKeyPressed in real time?

Why?

I am programming retro game Flappy from 8-bit MZ-800 computer and I have game in thread. I am giving input characters to threat by FormKeyDown and FormKeyUp. I need information about key up event. But...

This part is translated with Google Translate:

After pressing a key, I pass this information to the thread. The thread detects the pressed key when it needs it. If some time has passed and the key has been released, the thread does not have this information. Therefore, I decided to use FormKeyDown and FormKeyUp. But here a problem arose. I need to convert the pressed key to a real value, but after it is released, I need to know that the key is no longer pressed. This is not a problem. The problem is that I also need to combine Shift, Caps Lock and so on. And maybe there is already a function in Lazarus for this, but it also has to monitor whether the key has been released. Is it possible to find something like this in the Lazarus libraries?

Thank you for your help.
I work with Lazarus 2.2.2, FPC 3.2.2, date 2022-05-15
This information is actual to: 28st Dec 2022

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1460
    • Lebeau Software
Re: Converting scan codes to real ascii character
« Reply #1 on: February 05, 2025, 06:13:32 pm »
The OnKey[Down|Up] events give you VIRTUAL KEY CODES, not SCAN CODES.  Two different things.  A SCAN CODE is the raw code provided by the keyboard hardware.  Different keyboard providers can use different scan codes for the same key, so the OS translates SCAN CODES into VIRTUAL KEY CODES for easier use in applications.

On Windows, if you want to translate a scan code or a virtual key code into an ASCII character, you can use the Win32 MapVirtualKey/Ex() API:

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mapvirtualkeyw

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mapvirtualkeyexw

However, do be aware that not all characters can easily be translated manually using this API using just the OnKey[Down|Up] events.  Sometimes it takes multiple events to represent a single character correctly. The OnKeyPress event will provide you with the actual translated character after the OS has processed the relevant OnKey[Down|Up] events.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018