Solved

Capture keyboard event within a endless loop.

Posted on 1997-10-05
9
285 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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