Capture keyboard event within a endless loop.

for example I have a for..next loop. normally I can solve this by using the code below
while true do
begin
     ....
     Application.ProcessMessages;
end;
But using ProcessMessages method will slow down the speed.
How can I capture the keyboard event without using Application.ProcessMessages.
ccchouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DippenCommented:
Hi!

Generally, creating endless loops in an event-oriented language is equal to asking for trouble, unless you create a new thread and put the loop there. In my opinion, threads are not very simple to manage and should be avoided if not really needed.
   However, if you write more specifically what your application is supposed to accomplish, I might be able to help you further.

//Dippen
0
mivCommented:
The only thing you can do is to call ProcessMessages only once a second or for every 10.000 loops or so...(depending on the processing taking place in the loop)
0
dionysos_swampCommented:
one word: Tform.onKeyPress
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

ccchouAuthor Commented:
hi..
    I have to say sorry for your answer. Without the Application.ProcessMessages method, any window message can not be received until end of loop. So it can not work.
0
ccchouAuthor Commented:
My application is real time  graphically display A/D converted data. Speed is critical.
I used Convas for drawing the graph.
More specifically, I want to break the loop when Esc key been pressed.
If I use ProcessMessages in the loop and with  a button.(for say STOP button)
The speed will be only 1/10.

Thanks for everyone who answer my question.
0
GaborCommented:
I think, you have to use a thread to display the data, and leave the application to run as usual. This solution will be the correct and fastest one.
0
Pegasus100397Commented:
I'd approach this similiar to what Gabor recommended, but with a twist.

First, in the FormActive set a (form) global variable, like bRunDisplay to true.

Second, create a thread that sits in a loop with an Application.ProcessMessages loop looking for the user to press the [Esc] key. Set the thread to a very low priority to allow the data display to have more runtime. Once the thread is created it will run by itself independent of the A/D data display. The only purpose of this thread is to capture the mysterious [Esc] key. If the key is hit, then bRunDisplay is set to FALSE and the thread terminates.

Meanwhile, in a land, far far away the A/D Display is running with something like:

While bRunDisplay = True then
      {Do nifty Graphical stuff here}
else
     {STOP doing nifty Graphical stuff}

The keyboard checker thread and the A/D display are running together and the only thing in common is that they share the bRunDisplay variable which determines if the Display should end.

The only advantage to doing it this way as opposed to just using Application.Processmessages in your main loop is that you are able to set a lower priority for the keyboard scanning loop and it when IT "ProcessesMessages" and the A/D is in a critical need of CPU time, the keyboard scanner will yield (because of it's lower priority) to the main loop.

Sorry for the lengthy spiel and hope this helps :)
Pegasus
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ccchouAuthor Commented:
Hi, Pegasus
Thanks for your answer. I think this will work.
But right now this project I am working on is using Delphi 1.0.
Maybe my application must work on Window 3.1.
I'd like to know if you have another method.
Thank you.
0
Pegasus100397Commented:
cchou,
Glad to be of help!

The main problem in using Win3.1 with an app like this is that that OS does not support pre-emptive multitasking, a *major* problem for real-time applications! With 3.1 not only do you have to worry about your own application's "timeliness" but also how other CPU processes "hog" the processor, thereby slowing your application.

Unfortunately, without using threads (Win 95/NT, etc) when your application does an Application.ProcessMessages, your application is yielding not only to itself, but also to any other tasks that are running under 3.1

My advice, if you can avoid it, try and use at least Delphi 2.0 and design your application for use with 32 bit O/Ss *only*. Windows 3.1 and Workgroups is slowly going to go away and, unless you have a compelling need, don't expend the effort to accomidate a dying operating system.

Good luck with your project, sounds like fun :)
Pegasus
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.