Recent

Author Topic: LAMW and KeyPress. Prevent unwanted characters from being entered.  (Read 223 times)

shyub

  • New Member
  • *
  • Posts: 25
LAMW and KeyPress. Prevent unwanted characters from being entered.
« on: September 15, 2020, 03:09:47 pm »
Hello!
The "LAMW and KeyPress" issue has already been discussed: https://forum.lazarus.freepascal.org/index.php/topic,46596.msg332472.html#msg332472
But how to prevent or override the input of unwanted characters into the jEditText component was not touched upon.

On Windows it can be solved like this:

procedure TForm1.Edit1OnKeyPress (Sender: TObject; var Key: char);
begin
 if (Key in ['A' .. 'F', 'a' .. 'f', '0' .. '9', '', # 8]) = false then Key: = Chr (0);
end;

In LAMW, I tried to do this:

procedure TAndroidModule1.jEditText1Changed (Sender: TObject; txt: string;
  count: integer);
begin
  if count> 0 then
    if (txt [count] in ['A' .. 'F', 'a' .. 'f', '0' .. '9', '', # 8]) = false then begin
      SetLength (txt, count-1);
      jEditText1.Text: = txt;
    end;
end;

But after entering a forbidden character, the cursor jumps to the beginning of the line. Also, this method does not prevent the entry of prohibited characters in the middle or at the beginning of the line.
In principle, it is possible to check and delete prohibited characters anywhere in the string in a loop, this is not a problem. But I can't decide how to hold the cursor at the end of the line.

Does LAMW have a nicer way to solve this problem, or at least the ability to control the position of the cursor?

shyub

  • New Member
  • *
  • Posts: 25
Re: LAMW and KeyPress. Prevent unwanted characters from being entered.
« Reply #1 on: September 15, 2020, 05:56:20 pm »
This is how it works. After entering a prohibited character, the cursor remains at the end of the line.

procedure TAndroidModule1.jEditText1Changed(Sender: TObject; txt: string;
  count: integer);
var
  PosCur: TXY;
begin
  if count> 0 then
    if (txt [count] in ['A'..'F', 'a'..'f', '0'..'9', ' ', #8]) = false then begin
      SetLength (txt, count-1);
      jEditText1.Text:= txt;
      PosCur.X:=count-1;
      PosCur.Y:=count-1;
      jEditText1.CursorPos:=PosCur;
    end;
end;

t's just not clear why PosCur.X and PosCur.Y have the same values. In theory, PosCur.X should show the number of the last familiarity in the line, and PosCur.Y - the line number.

shyub

  • New Member
  • *
  • Posts: 25
Re: LAMW and KeyPress. Prevent unwanted characters from being entered.
« Reply #2 on: September 15, 2020, 06:21:36 pm »
And this is how an error occurs when running in the debugger. I tested it on two different computers with SDK-AVD and Genymotion debuggers.

procedure TAndroidModule1.jEditText1Changed(Sender: TObject; txt: string;
  count: integer);
var
  PosCur: TXY;
  S: String='';
  n: Integer;
begin
  if count>0 then begin
    for n:=1 to count do
      if (txt[n] in ['A'..'F','a'..'f','0'..'9',' ', #8])=true then
        S:=S+txt[n];
//    count:=S.Length;
//    txt:=S;
//    jEditText1.Text:=txt;
//    PosCur.X:=count;
//    PosCur.Y:=count;
    n:=S.Length;
    jEditText1.Text:=S;
    PosCur.X:=n;
    PosCur.Y:=n;
    jEditText1.CursorPos:=PosCur;
  end;
end;

And the error occurs when trying to output a line (jEditText1.Text: = S; or jEditText1.Text: = txt;).
Maybe I'm doing something wrong?

shyub

  • New Member
  • *
  • Posts: 25
Re: LAMW and KeyPress. Prevent unwanted characters from being entered.
« Reply #3 on: September 15, 2020, 07:40:02 pm »
I could not think of a better solution to this problem than the one below ...

procedure TAndroidModule1.jEditText1Changed(Sender: TObject; txt: string;
  count: integer);
begin
  jTimer1.Interval:=50;
  jTimer1.Enabled:=true;
end;

procedure TAndroidModule1.jTimer1Timer(Sender: TObject);
var
  PosCur: TXY;
  S, M: String;
  n: Integer;
begin
  jTimer1.Enabled:=false;
  M:='';
  S:=jEditText1.Text;
  for n:=1 to S.Length do
      if (S[n] in ['A'..'F','a'..'f','0'..'9',' ', #8])=true then
        M:=M+S[n];
  if M<>S then begin
    n:=M.Length;
    PosCur.X:=n;
    PosCur.Y:=n;
    jEditText1.Text:=M;
    jEditText1.CursorPos:=PosCur;
  end;
end;

I ran a few experiments trying to use the txt and count parameters, but the debugger either gave an error or looped. I would like to see an example that uses these parameters. What are they for?

 

TinyPortal © 2005-2018