Recent

Author Topic: Uppercase in Keypress  (Read 567 times)

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 99
Uppercase in Keypress
« on: April 24, 2025, 12:22:09 pm »
Greetings

I have a little procedure that ensure Tracking numbers are correctly entered into a TEDIT.
(Two LETTERS, 9Digits, Two letters e.g.AA952800068US).
It works perfectly.

The only issue is that I am missing something simple as it only works when the CAPS-LOCK is on.
Changing the Charcase of the Tedit to Uppercase has no effect. Getting the user to constantly check his Caps-lock is not viable.

I have also tried using Uppercase, upcase, ansiuppercase etc to change the inputtext, to no avail.
The only option left is to add provision for lowercases in the IF statements, but I am busy fumbling it and would really like some help to ensure the characters entered ARE capitals even if the use has the caps-lock off.
(I assume setting the Uppercase changes in Charcase happens after the Keypress event fires in the program)

If any of my explaining makes any sense.
Code: Pascal  [Select][+][-]
  1.  var
  2.   InputText: string;
  3. begin
  4.   if not (Key in [#8, #9]) then // Allow Backspace and Tab
  5.   begin
  6.  
  7.     InputText := TEdit(Sender).Text + Key;
  8.  
  9.     // Check length
  10.     if Length(InputText) > 13 then
  11.     begin
  12.       Key := #0; // Prevent input if length exceeds 13
  13.       Exit;
  14.     end;
  15.  
  16.     // Check format: Two letters, nine digits, two letters
  17.     if not ((Length(InputText) <= 2) and (Key in ['A'..'Z'])) and
  18.        not ((Length(InputText) > 2) and (Length(InputText) <= 11) and (Key in ['0'..'9'])) and
  19.        not ((Length(InputText) > 11) and (Length(InputText) <= 13) and (Key in ['A'..'Z'])) then
  20.     begin
  21.       Key := #0; // Prevent input if format is incorrect
  22.     end;
  23.   end;
  24.  
  25.  
  26. end;                        

I am certain its right there before me and I am just overlooking it.

-Regards, Peter

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 99
Re: Uppercase in Keypress
« Reply #1 on: April 24, 2025, 12:23:45 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  2.   var
  3.   CurrentText: string;
  4.    var
  5.   InputText: string;
  6. begin
  7.   if not (Key in [#8, #9]) then // Allow Backspace and Tab
  8.   begin
  9.     InputText := TEdit(Sender).Text + Key;
  10.  
  11.     // Check length
  12.     if Length(InputText) > 13 then
  13.     begin
  14.       Key := #0; // Prevent input if length exceeds 13
  15.       Exit;
  16.     end;
  17.  
  18.     // Check format: Two letters, nine digits, two letters
  19.     if not ((Length(InputText) <= 2) and (Key in ['A'..'Z'])) and
  20.        not ((Length(InputText) > 2) and (Length(InputText) <= 11) and (Key in ['0'..'9'])) and
  21.        not ((Length(InputText) > 11) and (Length(InputText) <= 13) and (Key in ['A'..'Z'])) then
  22.     begin
  23.       Key := #0; // Prevent input if format is incorrect
  24.     end;
  25.   end;
  26.  
  27.  
  28. end;                              

paweld

  • Hero Member
  • *****
  • Posts: 1368
Re: Uppercase in Keypress
« Reply #2 on: April 24, 2025, 12:27:39 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  2. var
  3.   InputText: String;
  4. begin
  5.   if not (Key in [#8, #9]) then // Allow Backspace and Tab
  6.   begin
  7.     InputText := TEdit(Sender).Text + Key;
  8.  
  9.     // Check length
  10.     if Length(InputText) > 13 then
  11.     begin
  12.       Key := #0; // Prevent input if length exceeds 13
  13.       Exit;
  14.     end;
  15.  
  16.     // Check format: Two letters, nine digits, two letters
  17.     if not ((Length(InputText) <= 2) and (Key in ['a'..'z', 'A'..'Z'])) and
  18.       not ((Length(InputText) > 2) and (Length(InputText) <= 11) and (Key in ['0'..'9'])) and
  19.       not ((Length(InputText) > 11) and (Length(InputText) <= 13) and (Key in ['a'..'z', 'A'..'Z'])) then
  20.     begin
  21.       Key := #0; // Prevent input if format is incorrect
  22.     end;
  23.   end;
  24. end;                      
Best regards / Pozdrawiam
paweld

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 99
Re: Uppercase in Keypress
« Reply #3 on: April 24, 2025, 12:35:37 pm »
Thank you.

I was right there, but probably typo's something and got frustrated.
Usually the small things that throws one for a loop.

Much appreciated.

-Peter

CM630

  • Hero Member
  • *****
  • Posts: 1339
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Uppercase in Keypress
« Reply #4 on: April 24, 2025, 12:36:27 pm »
I use this in an app, which accepts Hex values and "X" for some special cases.
I have found no problems  with it, I use it for a year at least:

Code: Pascal  [Select][+][-]
  1. //Convert kirrilitsa to Latin, if the keyboard layout is Bulgarian.
  2. procedure HexCyrToLat(var UTF8Key:TUTF8Char);
  3. begin
  4.   if (UTF8Key = 'А') or (UTF8Key = 'а') then UTF8Key :='A';
  5.   if (UTF8Key = 'Б') or (UTF8Key = 'б') then UTF8Key :='B';
  6.   if (UTF8Key = 'Ц') or (UTF8Key = 'ц') then UTF8Key :='C';
  7.   if (UTF8Key = 'Д') or (UTF8Key = 'д') then UTF8Key :='D';
  8.   if (UTF8Key = 'Е') or (UTF8Key = 'е') then UTF8Key :='E';
  9.   if (UTF8Key = 'Ф') or (UTF8Key = 'ф') then UTF8Key :='F';
  10.   if (UTF8Key = 'ѝ') or (UTF8Key = 'ь') or (UTF8Key = 'Ь') then UTF8Key :='X';
  11. end;  
  12.  
  13. //Use AcceptX when expected data is in format 0xWXYZ
  14. //function InputIsHex (UTF8Key: TUTF8Char; AcceptX: boolean = false): boolean;
  15. //UTF8Key is a string, containing a Single character. If it contains more chars, only the first one will be used.
  16. function InputIsHex (UTF8Key: string; AcceptX: boolean = false; AcceptWildCard: boolean = false): boolean;
  17. const
  18.   ValidChars = ['0'..'9','A'..'F',char(VK_BACK),char(VK_RETURN),char(VK_SPACE),#3,#22,#24]; //for some reason ctrl+c,v,x provide key #3,#22,#24
  19. var
  20.   key : char;
  21. begin
  22.   key :=  UTF8UpperCase(UTF8Key)[1];
  23.   if key = 'X' then exit (AcceptX);
  24.   if key = '?' then exit (AcceptWildCard);
  25.   if key in ValidChars then result := true else result :=  false;
  26. end;
  27.  
  28. procedure TMainForm.edtWatchLineIDUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
  29. begin
  30.   HexCyrToLat(UTF8Key);
  31.   if InputIsHex(UTF8Key,false,true) = false
  32.     then UTF8Key := ''
  33.     else UTF8Key := UTF8UpperCase(UTF8Key);
  34.   if UTF8Key <> '' then cboWatchPatternName.ItemIndex := -1;
  35. end;  
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

Bart

  • Hero Member
  • *****
  • Posts: 5563
    • Bart en Mariska's Webstek
Re: Uppercase in Keypress
« Reply #5 on: April 24, 2025, 04:11:53 pm »
Code: Pascal  [Select][+][-]
  1.  var
  2. [snip]
  3.     // Check length
  4.     if Length(InputText) > 13 then
  5.     begin
  6.       Key := #0; // Prevent input if length exceeds 13
  7.       Exit;
  8.     end;
You can set MaxLength of the TEdit to 13.
Makes a little more sense to me.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5563
    • Bart en Mariska's Webstek
Re: Uppercase in Keypress
« Reply #6 on: April 24, 2025, 04:14:08 pm »
Changing the Charcase of the Tedit to Uppercase has no effect.

What OS and which WS.
This (setting CharCase) should work out of the box.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: Uppercase in Keypress
« Reply #7 on: April 24, 2025, 04:39:11 pm »
Better ask @Joanna who is the capslock queen.  :D
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

CM630

  • Hero Member
  • *****
  • Posts: 1339
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Uppercase in Keypress
« Reply #8 on: April 24, 2025, 09:27:49 pm »
I know two ways to handle CAPS LOCK:
1. Windows Only - disable from registry.
2. Multiplatform - remove it with a screwdriver. >:D
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

Bart

  • Hero Member
  • *****
  • Posts: 5563
    • Bart en Mariska's Webstek
Re: Uppercase in Keypress
« Reply #9 on: April 24, 2025, 10:32:13 pm »
2. Multiplatform - remove it with a screwdriver. >:D
:D  :D

Bart

Handoko

  • Hero Member
  • *****
  • Posts: 5425
  • My goal: build my own game engine using Lazarus
Re: Uppercase in Keypress
« Reply #10 on: April 25, 2025, 09:14:22 pm »
I love multiplatform solution.  :D

 

TinyPortal © 2005-2018