Recent

Author Topic: OnKeyPress won't work  (Read 1241 times)

user5

  • Sr. Member
  • ****
  • Posts: 290
OnKeyPress won't work
« on: November 29, 2020, 12:20:01 am »
    OnKeyPress suddenly stopped working. KeyPreview is set to true and OnKeyPress has the code below. I tried a different ordinal number (127 for the Delete key) but it still doesn't work. I've experienced this before in other programs in which OnKeyPress won't work but I can't explain it. It used to work fine. Can anyone help?

   
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
  2. begin
  3.  if ord(Key) = 27 then
  4.   begin
  5.    form8.close;
  6.    application.processmessages;
  7.    escpressed := true;
  8.    sound(350);
  9.   end;
  10. end;


Sieben

  • Full Member
  • ***
  • Posts: 196
Re: OnKeyPress won't work
« Reply #1 on: November 29, 2020, 12:38:17 am »
You'd better use OnKeyDown for processing non-character keys:

Code: Pascal  [Select][+][-]
  1.   if (Key = VK_Escape) then
  2.     ...

Include LCLType in your uses clause for this.
« Last Edit: November 29, 2020, 12:47:03 am by Sieben »
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

dseligo

  • New Member
  • *
  • Posts: 37
Re: OnKeyPress won't work
« Reply #2 on: November 29, 2020, 12:43:13 am »
Put a memo on form and then at the beginning of event (above "if"):
Code: Pascal  [Select][+][-]
  1. Memo1.Append(IntToStr(Ord(Key)));
  2.  

And then watch if you are getting something.

winni

  • Hero Member
  • *****
  • Posts: 2122
Re: OnKeyPress won't work
« Reply #3 on: November 29, 2020, 12:54:19 am »
Hi!

Konsole issue??

Since the last Millenium this works on every (true) konsole/terminal:

CTRL-S stops the output.
CTRL-Q enables it again.

Winni


lucamar

  • Hero Member
  • *****
  • Posts: 3454
Re: OnKeyPress won't work
« Reply #4 on: November 29, 2020, 01:01:22 am »
Konsole issue??

Since the last Millenium this works on every (true) konsole/terminal:

CTRL-S stops the output.
CTRL-Q enables it again.

Its a GUI application, so no XON/XOFF issues, IMHO ;)

I just tested in this box (Linux-gtk2 x86_64) and it works. This is the (simple) code I used, after setting KeyPreview to True:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   Memo1.Lines.Add('Pressed Key: #%d', [Ord(Key)]);
  4.   if Key = #27 then begin
  5.     ShowMessage('See you soon!');
  6.     Close;
  7.   end;
  8. end;

and the result, after pressing <Esc> is as shown in the attached image.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

user5

  • Sr. Member
  • ****
  • Posts: 290
Re: OnKeyPress won't work
« Reply #5 on: November 29, 2020, 01:55:04 am »
    Thanks for taking the effort. When nothing was being done then (Key = VK_Escape) worked fine to make a sound and OnKeyPress put "27" in the memo box but when a loop was running then nothing happened. OnKeyPress used to work while the loop was running. CTRL-S didn't do anything. I don't want to pause the loop. I want to end it. I also put KeyBoard in the uses clause and tried using a Windows callback as shown below but it didn't work either. Perhaps I'm leaving something out.

   
Code: Pascal  [Select][+][-]
  1. function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;
  2. begin
  3.   if (uMsg = VK_Escape) then
  4.    sound(350);
  5.  
  6.   //Result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);
  7.  
  8. end;

user5

  • Sr. Member
  • ****
  • Posts: 290
Re: OnKeyPress won't work
« Reply #6 on: November 29, 2020, 01:58:26 am »
    I do have a ProgressBar running while the loop is going and if all else fails then I can put a Cancel button on the form that contains the ProgressBar but I why the other stuff won't work in the loop.

dseligo

  • New Member
  • *
  • Posts: 37
Re: OnKeyPress won't work
« Reply #7 on: November 29, 2020, 03:41:37 am »
Is it possible that you somehow delete OnKeyPress event when executing program?
Did you try same program on different computer?
Can you make test project and upload it so we can test it?

lucamar

  • Hero Member
  • *****
  • Posts: 3454
Re: OnKeyPress won't work
« Reply #8 on: November 29, 2020, 04:12:35 am »
Yes, I think at this point we need a test project that fails to see why it's happening.

And making such test program will probably let you see where the problem is, too. It's what happens usually :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jcmontherock

  • New Member
  • *
  • Posts: 46
Re: OnKeyPress won't work
« Reply #9 on: December 03, 2020, 11:46:07 am »
It seems that keypress event is catched by SynEdit. So if you have it in your application use KeyDown.

Bart

  • Hero Member
  • *****
  • Posts: 4113
    • Bart en Mariska's Webstek
Re: OnKeyPress won't work
« Reply #10 on: December 03, 2020, 12:15:56 pm »
. OnKeyPress used to work while the loop was running.

Inside the loop, if there is no code to let the form process it's message queue, the form will not respond to any input at all.
While it's considered bad practice you can do a Application.ProcessMessages inside your loop.

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 3454
Re: OnKeyPress won't work
« Reply #11 on: December 03, 2020, 12:33:36 pm »
It seems that keypress event is catched by SynEdit. So if you have it in your application use KeyDown.

Nope. The whole point of KeyPreview is to let the form see each keypress before any other control; it would make no sense if controls could break this. Sure, it can (probably) be done but it's not done by default. See attached image that demonstrates it. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jcmontherock

  • New Member
  • *
  • Posts: 46
Re: OnKeyPress won't work
« Reply #12 on: December 15, 2020, 04:02:07 pm »
I finally found what it happen: SynEdit is not concerned, it's TStringGrid. When StringGrid has the focus the Form1 Keypress event is catched. Who did that: I don't know.

winni

  • Hero Member
  • *****
  • Posts: 2122
Re: OnKeyPress won't work
« Reply #13 on: December 15, 2020, 07:30:37 pm »
Hi!

The keypress flow goes this way:

KeyPreview enabled: NO --> DeadEnd

YES:

Is there one of my Components active and has component a procedure for the keypress event?

No: Form is owner of keypress. The procedure connected to onKeypress is executed.
(If there is one)

Yes:  This component is owner of the keypress event.
The related procedure is executed


Winni

lucamar

  • Hero Member
  • *****
  • Posts: 3454
Re: OnKeyPress won't work
« Reply #14 on: December 15, 2020, 08:56:50 pm »
That's not quite so: the flow should be something like:

Code: Text  [Select][+][-]
  1. if (Form.KeyPreview) and Assigned(Form.OnKeyPress) then
  2.   Form.OnKeyPress();
  3. if Assigned(Control.OnKeyPress) then
  4.   Control.OnKeyPress();

But it doesn't matter, because the problem here is other: the grid is apparently gobbling up some key messages (<Esc>, in this case) before the form has an oportunity to see them, and that is contrary to what KeyPreview is for.

It's easy to see this:  You just have to modify jcmontherock's example like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   if Ord(Key) > 32 then
  4.     ShowMessage('Form KeyPress: ' + Key)
  5.   else
  6.     ShowMessageFmt('Form KeyPress: #%d', [Ord(Key)]);
  7.   if Key = #$1B {#27} then Close;
  8. end;

and you'll see that SynEdit, even though it is a known "glutton" for key presses, lets the form see all keys before it does, while the grid doesn't, which means the grid is somehow bypassing the normal mechanisms to set itself as target for key messages.
« Last Edit: December 15, 2020, 08:58:52 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018