Recent

Author Topic: Using KeyPress event, how to get subsequent key presses?  (Read 1285 times)

new2thisagain395

  • New Member
  • *
  • Posts: 19
Using KeyPress event, how to get subsequent key presses?
« on: March 28, 2024, 01:14:34 am »
In a TMemo, during the KeyPressed event how do you get the key pressed to be "disposed of" (or dropped is maybe a better term) once you do what you want with it, so that the next keystroke can be captured?  I'm using this event code:

Code: [Select]
procedure TSCU.Memo1KeyPress(Sender: TObject; var Key: char);
var
  DateString: string;
  TimeString: string;
  InReadPW:   boolean = false;
  R:          Longint;

begin
     KeyPressed := UpCase(Key);
                                             
and while it reads the first key pressed, subsequent key presses leave KeyPressed stuck at the first character that was typed (viewing in the Watch window while debugging).  Once I grab that key (I am adding it to a buffer) how do I get the next key pressed to be captured by this function?

jamie

  • Hero Member
  • *****
  • Posts: 6219
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #1 on: March 28, 2024, 01:38:55 am »
Code: Pascal  [Select][+][-]
  1. procedure TSCU.Memo1KeyPress(Sender: TObject; var Key: char);
  2. var
  3.   DateString: string;
  4.   TimeString: string;
  5.   InReadPW:   boolean = false;
  6.   R:          Longint;
  7.  
  8. begin
  9.      KeyPressed := UpCase(Key);
  10.                                              
  11.  

You don't stay in this event, you allow it to exit, the next key you press will trigger this event again.
you need to use an external buffer to this event.
The only true wisdom is knowing you know nothing

Joanna

  • Hero Member
  • *****
  • Posts: 802
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #2 on: March 28, 2024, 03:07:47 am »
Would it be better to use keydown event instead for this?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Zvoni

  • Hero Member
  • *****
  • Posts: 2362
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #3 on: March 28, 2024, 09:13:47 am »
Would it be better to use keydown event instead for this?
rather KeyUp
KeyDown fires permanently, when you hold a key down.
KeyUp only fires once per "Key-Press"
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

alpine

  • Hero Member
  • *****
  • Posts: 1110
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #4 on: March 28, 2024, 10:02:15 am »
*snip*
and while it reads the first key pressed, subsequent key presses leave KeyPressed stuck at the first character that was typed (viewing in the Watch window while debugging).  Once I grab that key (I am adding it to a buffer) how do I get the next key pressed to be captured by this function?
I'm not experiencing that behavior with TMemo.OnKeyPress, probably your problem lies somewhere else.

Lazarus 2.2.4 (rev lazarus_2_2_4) FPC 3.2.2 x86_64-linux-gtk2
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Bart

  • Hero Member
  • *****
  • Posts: 5296
    • Bart en Mariska's Webstek
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #5 on: March 28, 2024, 11:32:53 am »
In a TMemo, during the KeyPressed event how do you get the key pressed to be "disposed of" (or dropped is maybe a better term) once you do what you want with it
Set Key to #0.

Bart

Joanna

  • Hero Member
  • *****
  • Posts: 802
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #6 on: March 28, 2024, 02:15:13 pm »
Would it be better to use keydown event instead for this?
rather KeyUp
KeyDown fires permanently, when you hold a key down.
KeyUp only fires once per "Key-Press"

Interesting, so the key up event isn’t too late to set the key to #0 ?
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Zvoni

  • Hero Member
  • *****
  • Posts: 2362
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #7 on: March 28, 2024, 02:24:42 pm »
Would it be better to use keydown event instead for this?
rather KeyUp
KeyDown fires permanently, when you hold a key down.
KeyUp only fires once per "Key-Press"

Interesting, so the key up event isn’t too late to set the key to #0 ?
Depends on the use-case.
Just create all three events and put some Writeln's in.
You'll see what's "on time" and what's too late
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2220
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using KeyPress event, how to get subsequent key presses?
« Reply #8 on: March 29, 2024, 10:44:31 am »
I also suggest KeyUp event for logging a released key, if its just pure character keys you are hunting at, eg for some undo, you could also use OnChange from TMemo by logging position and the change, ie insert/remove/replace action ...
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018