Filtering keystrokes using FMX

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
LexRutter
Active Poster
Active Poster
Posts: 14
Joined: Sat Dec 05, 2020 9:52 am

Filtering keystrokes using FMX

Post by LexRutter »

Working on Multi-Platform programming using C++Builder 10.4 and the FMX package.

UPDATE:

Through quite a bit of reading I have discovered that soft-keyboards rarely create a KeyEvent (in fact, it appears to be discouraged).



I need to build a series of filters for input to a TEdit box, intercepting the input and filtering it appropriately before it is sent to the TEdit box itself.

The following code runs very nicely when there is a HARDWARE keyboard. Thus far I can find nothing that seems to work when a SOFTWARE keyboard is used (Android OS is the target - however I get the impression that this issue exists on Windows and Apple OS also).

if (ListBox1->ItemIndex==1) //Selects the number base
{ //---------This section is for HEX entries-----------
if (!(((Key >= L'0')&&(Key <= L'9'))||(Key == vkBack )||((Key>='a')&&(Key<='f'))
||((Key>='A')&&(Key<='F'))))
{
Key = L'\0'; //Ignore key
}
}

Along with the OnKeyUp event, I've tried OnChange and OnTyping events. No luck.

I have fond no C++Builder solutions (one gent seems to have created a Delphi add-in class for part of this, but that is not really a viable option for me).

Has anyone found a way to address capturing input to a TEdit box, with the intent of modifying/filtering the character that actually gets sent to the TEdit, specifically for a SOFT keyboard (i.e. no KeyEvent message created) and using C++Builder?

Thanks
Last edited by LexRutter on Wed Feb 17, 2021 2:26 pm, edited 1 time in total.
HsiaLin
BCBJ Master
BCBJ Master
Posts: 344
Joined: Sun Jul 08, 2007 6:29 pm

Re: Filtering keystrokes using FMX

Post by HsiaLin »

Does Key = 0;
give an error?
rlebeau
BCBJ Author
BCBJ Author
Posts: 1776
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Filtering keystrokes using FMX

Post by rlebeau »

LexRutter wrote: Tue Feb 16, 2021 11:18 am The following code runs very nicely when used in a VCL based program. It compiles nicely under FMX, the program loads and runs in both Windows and Android, but this code is completely ignored when running on either platform.
Which event are you actually using?
LexRutter wrote: Tue Feb 16, 2021 11:18 am In one place I trap on Key == vkReturn and that functions perfectly. I just can't seem to be able to intercept keystrokes going to the TEdit box in FMX like it works in VCL.
Key handling is a bit different in FMX than it is in VCL.

You are looking for ASCII characters in the Key parameter, did you try looking for them in the KeyChar parameter instead? The Key parameter is more for detecting virtual/hardware keys (backspace, function keys, etc), whereas the KeyChar parameter is more for detecting the translated textual characters that the keys produce. Usually one or the other parameter will be zero, ie KeyChar is 0 when Key is not 0, and vice versa.

Try this:

Code: Select all

if ((Key == vkBack) || ((KeyChar >= L'0') && (KeyChar <= L'9')) || ((KeyChar >= 'a') && (KeyChar <= 'f')) || ((KeyChar >= 'A') && (KeyChar <= 'F')))
LexRutter wrote: Tue Feb 16, 2021 11:18 am Key = Key; //Pass key to edit box
That is redundant. You can remove that completely.
LexRutter wrote: Tue Feb 16, 2021 11:18 am Key = L'\0'; //Ignore key
Key is an 'unsigned short', not a 'wchar_t'.

Set Key to 0 when discarding hardware keys.

Set KeyChar to L'\0' when discarding textual characters.
LexRutter wrote: Tue Feb 16, 2021 11:18 am using VCL I use Key = NULL
NULL is usually just an alias for 0. But you really should not use NULL for any non-pointer types. Treat it as you would treat C++11's nullptr.
LexRutter wrote: Tue Feb 16, 2021 11:18 am Unfortunately, FMX will error out on the use of NULL unless it is used with a pointer.
As it should be. Under C++11 and later, NULL is allowed to be an alias for nullptr, which CAN'T be used with non-pointer types (unless they explicitly implement support for nullptr_t input). All the more reason NOT to use NULL for non-pointer types.
Remy Lebeau (TeamB)
Lebeau Software
LexRutter
Active Poster
Active Poster
Posts: 14
Joined: Sat Dec 05, 2020 9:52 am

Re: Filtering keystrokes using FMX

Post by LexRutter »

Thanks for the info - great help.

(By the way, Yes, I know Key = Key is redundant and not doing anything, but in this case it's helping me track where I am & what's going on. Does not affect the compiler, but it really works to help the human.)

It's taking some getting used to, trying to decipher the differences between the compiler requirements in VCL vs FMX. I've been told that they are the same, but experience seems to say different.

I'll give you suggestions a shot.
LexRutter
Active Poster
Active Poster
Posts: 14
Joined: Sat Dec 05, 2020 9:52 am

Re: Filtering keystrokes using FMX

Post by LexRutter »

Quick follow-up: Thanks for the pointers - it did (sort of)work. Eventually realized that I also needed to finish it of by changing " Key = L'\0' " to "KeyChar = L'\0' ".

One problem, this works fine on Windows. The same code, same compiler, same session, but with the target switched to Android - it's just ignored. Has no effect at all. I did run across an article (just glanced at it) about issues with "virtual keyboards" on mobile devices.

I'd certainly appreciate any suggestions on how to get this to function under Android.

There are quite a few changes to wrap around between the VCL package and FMX. Interesting learning curve, but the feedback hear is invaluable.

Thanks.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1776
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Filtering keystrokes using FMX

Post by rlebeau »

LexRutter wrote: Tue Feb 16, 2021 9:42 pm I'd certainly appreciate any suggestions on how to get this to function under Android.
Sorry, I can't help with that. I'm not an Android developer, and I don't have FMX's source code for Android to look at.
Remy Lebeau (TeamB)
Lebeau Software
LexRutter
Active Poster
Active Poster
Posts: 14
Joined: Sat Dec 05, 2020 9:52 am

Re: Filtering keystrokes using FMX

Post by LexRutter »

I'm going to post an answer to my own question, and it appears it's actually pretty trivial.

FMX has a property in Object Inspector called FilterChar that is built to handle this issue directly. Unfortunately it is almost not cross-referenced to the act of intercepting keystrokes. It was a serendipitous search result that pointed me in that direction. Even then, it was not a direct answer.

The following code is used to filter input and restrict it to just the characters that are allowed to create a Hex number:
Edit1->FilterChar = "0123456789abcdefABCDEF";

I did not attempt to prove the following: Some references indicate that there are difficulties with the "Default" property KeyboardType, and that the FilterChar works more reliable when setting a specific keyboard type. I set it to NumberPad and this works well (even when I switch to the letters).

It does not interfere with standard keydown/keyup messages and hardware keyboard filtering.

This worked well on both Windows10 and Android.
Post Reply