Detect Unicode Keyboard in Lazarus all Language
Set Windows keyboard and language.
Go to Control Panel > Time & Language > Language & Region > Add a language.
Add Keyboard, language as we want instance Thai, which in this example would use Thai charactor.
Switch to the Thai keyboard layout when testing.
Check Windows Locale and Keyboard
Go to Control Panel > Region:
Under the Formats tab, set the format to Thai.
Under the Administrative tab, set the Language for non-Unicode programs to Thai.
In Lazarus Project before build
Enable Full UTF-8 Support in Lazarus
Go to Project > Project Options > Application.
Ensure Use ANSI code page as UTF-8 is checked.
Set the Font property of Form, Edit1:
CharSet : set to UNICODE
Font Name to one that supports Thai characters, such as:
Tahoma
Arial Unicode MS
Noto Sans Thai
* Note: Locale language as we want, for this example use Thai character to be reference
Starting Code
Use unit part:
LazUTF8,LCLProc, LConvEncoding
Create function to convert integer to binary
function IntToBin(Value: LongInt; Digits: Integer): string;
var
i: Integer;
begin
Result := '';
for i := Digits - 1 downto 0 do
begin
if (Value and (1 shl i)) <> 0 then
Result := Result + '1'
else
Result := Result + '0';
end;
end;
Create Function convert or Decode UTF8 to Unicode
function DecodeUTF8ToUnicode(const Utf8Str: string): LongInt;var Byte1, Byte2, Byte3, Byte4: Byte; Len: Integer;begin Result := -1; // Default to invalid Len := Length(Utf8Str);
if Len = 1 then begin Byte1 := Byte(Utf8Str[1]); Result := Byte1; // ASCII or single-byte UTF-8 end else if Len = 2 then begin Byte1 := Byte(Utf8Str[1]); Byte2 := Byte(Utf8Str[2]); Result := ((Byte1 and $1F) shl 6) or (Byte2 and $3F); end else if Len = 3 then begin Byte1 := Byte(Utf8Str[1]); Byte2 := Byte(Utf8Str[2]); Byte3 := Byte(Utf8Str[3]); Result := ((Byte1 and $0F) shl 12) or ((Byte2 and $3F) shl 6) or (Byte3 and $3F); end else if Len = 4 then begin Byte1 := Byte(Utf8Str[1]); Byte2 := Byte(Utf8Str[2]); Byte3 := Byte(Utf8Str[3]); Byte4 := Byte(Utf8Str[4]); Result := ((Byte1 and $07) shl 18) or ((Byte2 and $3F) shl 12) or ((Byte3 and $3F) shl 6) or (Byte4 and $3F); end;end;
Edit1 OnUTF8KeyPress Events
procedure TForm1.Edit1UTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);var UnicodeValue: LongInt; Utf8Str: string;begin // UTF8Key contains the full UTF-8 character Utf8Str := UTF8Key;
// Decode the Unicode code point UnicodeValue := DecodeUTF8ToUnicode(Utf8Str);
// Display debug information //ShowMessage('Key: ' + Utf8Str); //ShowMessage('Unicode Value: ' + IntToStr(UnicodeValue));
// Display Unicode values if UnicodeValue > 0 then begin Declbl.Caption := IntToStr(UnicodeValue); // Decimal representation Hexlbl.Caption := IntToHex(UnicodeValue, 4); // Hexadecimal representation Binlbl.Caption := IntToBin(UnicodeValue, 16); // Binary representation end else begin Declbl.Caption := 'Error'; Hexlbl.Caption := 'Error'; Binlbl.Caption := 'Error'; end;end;
Debugging Trick
ShowMessage to confirm the contents of Utf8Str and UnicodeValue.
The Result After comply Lazarus project to detect unicode key code all language as below: