Search code examples
delphiunicodedelphi-2009

How to identify unicode keys on key press?


My application uses unicode characters and i have several text fields where i want to restrict user from inputing special characters like :'";

begin
    if not (Key in ['a'..'z','A'..'Z',' ','0'..'9',#13,#8]) then
        Key := #0;
    if Key = #13 then
        bOk.Click;
end;

So at this point it lets user add spaces and use a backspace key to erase, and of course Enter key to comfirm.

There are few unicode characters i want to let being inputed also. ą, č, ė, į, š, ų, ū, ž and their uppercase alternatives, but just adding them to the set like so...

Key in ['a'..'z','A'..'Z',' ','0'..'9',#13,#8,'ą'..'ž','Ą'..'Ž']

... does nothing and i still can not write these symbols in the text field.

I would like to know, how to fix this problem. Is there a way to tell if the pressed key is the unicode character i'm looking for?

Thank you


Solution

  • If you're on D2009 or later there's a unit named Character containing functions like IsLetterOrDigit, IsLetter, etc. all handling specifically what you need.