LAMW and KeyPress. Prevent unwanted characters from being entered. lazarus / 43-Android ---------------------------------------------------------------------- shyub · 2020-09-15 13:09 Hello! The "LAMW and KeyPress" issue has already been discussed: https://foru m.lazarus.freepascal.org/index.php/topic,46596.msg332472.html#msg33247 2 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 · 2020-09-15 15:56 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 · 2020-09-15 16:21 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 · 2020-09-15 17:40 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?