Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any equivalent to the old Pascal "keypressed"?

Status
Not open for further replies.

OLVtelephoneman

Technical User
Jan 24, 2005
39
0
0
US
I'm writing an oscilloscope control program. The program is supposed to loop and so continually collect data from the scope and display it. But if the user clicks a button, control needs to go to the button handling routine, then back to the loop. With Turbo Pascal and DOS it was easy to do with the "keypressed" command, , e.g., repeat ... until keypressed. But there doesn't seem to be any equivalent in Delphi, such as "mouse clicked". Any suggestions as to how to do this? Perhaps I'm missing something obvious! Thanks!
 
Is your application windows based (or 'console' running in a 'DOS' window)
If it is a (default) windows form based application, then all delphi forms have mouse capture and keyboard capture events. All buttons have onclick events.

If you are just starting out with Delphi, I suggest that you start by looking at the tutorials and the help files, to get a feel for Windows/Event programming.




Steve
DNA still on the way, as is the new release of Star............
 
Delphi is different from pascal in this way, In TP you have sequencial programming, plain start to finish.

In event based programming the program waits for input of the user.

In your case you have to make use of the timer component.


example:
Code:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  catch_data;
  display_data;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  timer1.Enabled := false
  // will stop the process
end;

Steven van Els
SAvanEls@cq-link.sr
 
Use the KeyPress event in your main form:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key=chr(13){enter} then do_something;
end;
 
OK but I'm still missing something. For a simpler example, suppose I want to write a Windows program which continually draws a circle, but has buttons the user can press to change the line color. I know how to write the button handlers, but how would I create the circle drawing program so that it would start off running and control would return to it after a button was clicked and serviced by the button handling procedure?
 
Search the helpfile for the Application.OnIdle() event, there you should be drawing the circle based on global data, and in the main eventloop (Windows waiting for keyboard/mouse clicks) the parameters and/or data could be manipulated.
This becomes sort of a 'multi-threaded' app, except there is a limit of 1 (semi-)thread.

HTH
TonHu
 
OLVTM:

You could draw your circle continuously by using a timer and vary the interval to vary the speed at which it's drawn.

At that point, onbuttonclicks could be used to change the color etc.

Would advise a

Timer1.Enabled := False;
: button click event code
Timer1.Enabled := True;

in each of the on click events


Regards and HTH,
JGS
 
I experienced similar problems the first time I made the switch from Pascal to Delphi. Maybe I can clarify a few things.

First of all: Delphi handles all keyboard and mouse input for you. To capture a keypress you need to use the form's keypress, keydown or keyup events. Event handlers are found on the object-inspector bar thingy.

Secondly: Using onIdle is probalby your best choice for a small program, but considder creating a thread. This will allow you to write normal pascal-style code to do your loop. Just write your code in the onExecute method of the thread (these things are like totally created for you by delphi, yo)

So basically what you need is a global variable, lets say its a boolean called KeepDrawingMe. Set this to true initially. Then when the keypressed event is triggered just set KeepDrawingMe to false. Alternatively you could set it to false only when esc is pressed (#27) and change the colour of the circle when some other key is pressed (obviously using some more global variables). In the thread's onExecute method thingy

Syncronize(MycircleProc);

and then in a procedure of your own creation for arguments sake called MyCircleProc

While KeepDrawingMe Do
begin
{sircle smircle}
end;
thing.

If this didn't help, due to my poor teaching abilities, I appologise in advance.
 
After an Delphi 'event' code has completed, control passes back to the calling point.

e.g. If you running a loop, start it with another button or in formactivate

repeat
application.processmessages;
<your code>

until the cowscomehome

A button 'onclick' event should interrupt this, execute then drop back into the loop.

If you program is doing nothing else most of the time then you could use OnIdle but Multi-threading hmm.




Steve
DNA still on the way, as is the new release of Star............
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top