Board index » delphi » How Detect Keypress in a Non-Form Unit?

How Detect Keypress in a Non-Form Unit?

My project has a unit containing procedures and functions used by some
of the form units. One of the procedures has a long running time while
one of the calling forms is on the screen. How can pressing of the
Enter or Escape key be detected while the procedure is running?

I have scattered Application.ProcessMessages statements in various
places, but the form's OnKeypress event is not triggered by a
keypress.

 

Re:How Detect Keypress in a Non-Form Unit?


Quote
"Sheldon Shallon" <s...@ieee.org> wrote in message

news:3be71ecf.23807245@news.earthlink.net...

Quote
> My project has a unit containing procedures and functions used by some
> of the form units. One of the procedures has a long running time while
> one of the calling forms is on the screen. How can pressing of the
> Enter or Escape key be detected while the procedure is running?

> I have scattered Application.ProcessMessages statements in various
> places, but the form's OnKeypress event is not triggered by a
> keypress.

For this to make sense I believe your long running procedure must be
called from one of the event handlers associated with your form? In
that case the fact that your process is in a non-form unit should be
irrelevant because it is invoked under the applicable form.

If this handler should happen to be triggered by any kind of a keypress
activity I am very much in doubt if it will react to another keypress event
even if it suspends itself by a call to Application.ProcessMessages

I do not really know, but I would expect the events to be queued up
until the handler is ready for the next keypress event, and maybe this is
the
case even if the event that started the long running process was an event
even completely different from keypress.

I have a strong feeling that your problem is a typical case for
multithreading,
keep your VCL thread (with the form) free to process whatever keys or
mouse activities you may need and have a lower priority thread do the
processing. I have found
http://www.pergolesi.demon.co.uk/prog/threads/ToC.html
a very useful introduction to such programming - do take a look!

regards Sven

Re:How Detect Keypress in a Non-Form Unit?


In article <3be71ecf.23807...@news.earthlink.net>, s...@ieee.org (Sheldon

Quote
Shallon) writes:
>My project has a unit containing procedures and functions used by some
>of the form units. One of the procedures has a long running time while
>one of the calling forms is on the screen. How can pressing of the
>Enter or Escape key be detected while the procedure is running?

>I have scattered Application.ProcessMessages statements in various
>places, but the form's OnKeypress event is not triggered by a
>keypress.

I assume that the form's OnKeyPress event handler is properly allocated (and it
will be if you have let Delphi declare the event handler by your
double-clicking in the Object Inspector Events page).

Then if the procedure is called by a control on the form (ie by a button press
or other control) then that control will have focus and the keypress messages
will be sent to that control and the form's event handler will not be
triggered.

If that is the situation then you must set the form's KeyPreview property to
true. The keypress windows' message will then trigger the form's event handler
before being passed to the focussed control's event handler

Alan Lloyd
alangll...@aol.com

Other Threads