Solved

Capture keyboard event within a endless loop.

Posted on 1997-10-05
9
281 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:ccchou
9 Comments
 

Expert Comment

by:Dippen
ID: 1346811
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
 
LVL 1

Expert Comment

by:miv
ID: 1346812
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
 
LVL 1

Expert Comment

by:dionysos_swamp
ID: 1346813
one word: Tform.onKeyPress
0
 

Author Comment

by:ccchou
ID: 1346814
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ccchou
ID: 1346815
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
 
LVL 1

Expert Comment

by:Gabor
ID: 1346816
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
 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 200 total points
ID: 1346817
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
 

Author Comment

by:ccchou
ID: 1346818
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
 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1346819
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now