[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to do loop -> until keypressed

Posted on 2004-04-21
3
Medium Priority
?
1,717 Views
Last Modified: 2008-01-16
How to do loop until keypressed ?

repeat
memo1.lines.add('Hello!') ;
until keypresses ; // What should I put to here, I would like to use ESC key.



0
Comment
Question by:hsimola
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 10885919
a sample

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    Procedure DoRepeatLoop;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Procedure TForm1.DoRepeatLoop;
var
  keyWasEscape : Boolean;
  KeyBoardState : TKeyBoardState;
begin
  keyWasEscape := false;
  repeat
    //other ops
    memo1.lines.add('In Loop');
    //check
    keyWasEscape := GetAsyncKeyState(VK_ESCAPE) < 0;
  until keyWasEscape;
  memo1.lines.add('Escape-Key was pressed');
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  DoRepeatLoop;
end;

end.

meikl ;-)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10885927
Set the Form's property KeyPreview as True (It will allow it to receive Pressed Keys before controlls placed on it):

unit Unit_Q_20963045;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure SpeedButton1Click(Sender: TObject);
  private{ Private declarations }
    PressedKey: Char;
  public { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  PressedKey := #0;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  PressedKey := Key;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  try
    repeat
      Memo1.SetFocus;
      Memo1.Lines.add('Hello!') ;
      Application.ProcessMessages;
    until (PressedKey=#27);
  finally
    PressedKey := #0;
  end;
end;

end.

//.............................

object Form1: TForm1
  Left = 216
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  KeyPreview = True
  OldCreateOrder = False
  OnCreate = FormCreate
  OnKeyPress = FormKeyPress
  PixelsPerInch = 96
  TextHeight = 13
  object SpeedButton1: TSpeedButton
    Left = 52
    Top = 76
    Width = 135
    Height = 22
    Caption = 'Check Key Press'
    OnClick = SpeedButton1Click
  end
  object Memo1: TMemo
    Left = 286
    Top = 42
    Width = 255
    Height = 317
    TabOrder = 0
  end
end
0
 
LVL 3

Expert Comment

by:Aliev
ID: 10897320
Most easy and primitive variant:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Timer1: TTimer;
    Button1: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Memo1.Lines.Add('Hello!')
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
Timer1.Enabled:=false;
 Button1.Enabled:=true;
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
Timer1.Enabled:=false;
 Button1.Enabled:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled :=true;
 Button1.Enabled:=false;
end;


end.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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