Recent

Author Topic: Key Word vs Key Char  (Read 686 times)

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Key Word vs Key Char
« on: September 10, 2024, 02:49:32 pm »
Last question for a while gents.

This is where the beginner doesnt understand.

I want to limit the user from capturing anything else but NUMBERS in a Tcombobox.
From my reading, that must be done in the KEYDOWN event and not the Keypress like in the case of a Tedit control.

The Keydown event :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox4KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);  

VS

The Keypress event:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox4KeyPress(Sender: TObject; var Key: char);

In the first line the KEY : Word and the second one Key:Char.
I know how to limit the text input using the CHar :
Code: Pascal  [Select][+][-]
  1. if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;

I still need to learn how to adapt that to work with the first (keydown) event.

If someone can show me please.


-Peter
« Last Edit: September 10, 2024, 02:51:19 pm by Petrus Vorster »

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 69
Re: Key Word vs Key Char
« Reply #1 on: September 10, 2024, 03:18:22 pm »
Code: Pascal  [Select][+][-]
  1.  if not (Key in [8..9, 96..105]) then Key := 0;  

Thanks great people.

-Peter

paweld

  • Hero Member
  • *****
  • Posts: 1217
Re: Key Word vs Key Char
« Reply #2 on: September 10, 2024, 03:19:06 pm »
Code: Pascal  [Select][+][-]
  1. if not (Key in [48..57, VK_OEM_PERIOD, VK_BACK, VK_TAB]) then Key := 0; //48: VK_0, 57: VK_9
  more codes find in LCLType unit 
   
Edit: The key codes that @Petrus Vorster provided only work when NumLock is pressed.
« Last Edit: September 10, 2024, 03:22:00 pm by paweld »
Best regards / Pozdrawiam
paweld

Bart

  • Hero Member
  • *****
  • Posts: 5365
    • Bart en Mariska's Webstek
Re: Key Word vs Key Char
« Reply #3 on: September 10, 2024, 06:18:10 pm »
I know how to limit the text input using the CHar :
Code: Pascal  [Select][+][-]
  1. if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;

I still need to learn how to adapt that to work with the first (keydown) event.

For what you are doing here, OnKeyPress is the right place.
Notice that a give char can be generated using different Keys (e.g. I can type a capital A using ALT+0+0+6+5, which gives multiple OnKeyDowns, but 1 OnKeyPress), and if you are only interested in what char was eventually generated, use OnKeyPress.
If you want to e.g. react on the Left or Right arrow key (or any other function key) or need to know wether or not Shift/Ctrl/Alt is pressed, you need to use the OnKeyDown, since that cannot be done in OnKeyPress.

Bart

paweld

  • Hero Member
  • *****
  • Posts: 1217
Re: Key Word vs Key Char
« Reply #4 on: September 10, 2024, 06:26:28 pm »
Quote from: Bart
For what you are doing here, OnKeyPress is the right place.
Notice that a give char can be generated using different Keys (e.g. I can type a capital A using ALT+0+0+6+5, which gives multiple OnKeyDowns, but 1 OnKeyPress), and if you are only interested in what char was eventually generated, use OnKeyPress.
But, if your codepage is another than cp1252 then use OnUtf8KeyPress event.
Best regards / Pozdrawiam
paweld

Bart

  • Hero Member
  • *****
  • Posts: 5365
    • Bart en Mariska's Webstek
Re: Key Word vs Key Char
« Reply #5 on: September 10, 2024, 11:21:51 pm »
But, if your codepage is another than cp1252 then use OnUtf8KeyPress event.
You should use that whenever you want to control any "character" that's not lower ascii, regardless of your (windows) codepage.

Bart

 

TinyPortal © 2005-2018