开发者

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 开发者_Python百科tell if the pressed key is the unicode character i'm looking for?

Thank you


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.


Please pay attention to Hints and Warnings when you compile your apps. To ignore them is to ignore potential problems.

You should be getting warning that "WideChar is reduced to byte char...". Therein lies the problem: the Key is no longer Unicode in your set operation!

The warning advises you to use CharInSet; you can also look at TCharacter (a special class with class functions to identify certain categories of characters). If neither of these suit your requirements, then use a string constant with all the valid characters, and use the Pos function to determine if the Key is present.

Finally, you may rather want to consider excluding specific characters as opposed to trying to think of every specific character you want to include.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜