Solved

How to do loop -> until keypressed

Posted on 2004-04-21
3
1,483 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
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

14 Experts available now in Live!

Get 1:1 Help Now