加个循环,判断一个变量是否为1,true 时退出循环
另外在keydown事件中将该变量置1。
应该可以解决了/

解决方案 »

  1.   

    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
        Showmessage('ok');
    end;
    好像这样就可以了
      

  2.   

    楼主,你的代码里不能响应KEYDOWN或者MOUSEDOWN之类事件么?
      

  3.   

    read()是Pascal中的在DElphi中你可以拦截WM_KEYDOWN和WM_LBUTTONDOWN,WM_RBUTTONDOWN.
    或重载WinProc函数
    或使用ApplicationEnevnts控件
      

  4.   

    你可以将该过程写在线程中
    procedure TTestThread.Execute();
    begin
      FreeOnTerminate:=true;
      //DoSomething
      while not (Terminated) do
      begin  end;
      //DoOtherthing
    end;
    //在Form里
    procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
    begin
      TestThread.Terminated();
    end;
      

  5.   

    拿去,不用多加分了
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, AppEvnts;type
      TForm1 = class(TForm)
        ApplicationEvents1: TApplicationEvents;
        procedure ApplicationEvents1Message(var Msg: tagMSG;
          var Handled: Boolean);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    begin
      if (Msg.message=WM_KEYDOWN)or(Msg.message=WM_LBUTTONDOWN)or(Msg.message=WM_RBUTTONDOWN)then
      begin
        //   Close;
        //Do something here what you want to do 
      end; 
    end;end.
      

  6.   

    假设我的程序是:
    procedure doit();
    begin
      Label1.caption:='Good';
      //(按任意键继续)
     //这里应该加入什么样的函数
      Label1.caption:='Bad';
    end
      

  7.   

    AllenYao(),我要提醒你,你的目的无法达到的。
    你混淆了Windows编程和Dos编程的概念,Dos只有一个程序可以运行,Windows是靠线程调度的,也就是说,在Dos下面运行程序,所有资源都被程序占用了,可以靠一个死循环判断用户是否按键。但是Windows不行,如果键盘被按下,Windows会发一个消息给相应的程序,然后相应的程序再处理。如果你在函数doit()里无限等待,而且你的程序只有这一个主线程,请问,不跳出这个函数,Windows如何通知你键被按下了?这也就是,如果你在某一个函数里进行了大量的长时间操作,会发现窗口失去了响应,因为只有结束了这个函数程序才能转到处理键盘的子程序中。因此除非使用多线程,你不可能在一个函数中间等待按键,然后恢复执行的。
      

  8.   

    private
     m_key:word;
    formcreate;
    begin
      key :=1;
    end;procedure do
    BEGIN
     while (key<>m_key); 
    END;form.keyDown.....
    begin
     m_key:=key;
    end;procedure doit();
    begin
      Label1.caption:='Good';
      //(按任意键继续)
       do;
     //这里应该加入什么样的函数
      Label1.caption:='Bad';
    end 
      

  9.   

    那么sleep这个函数是如何让程序停止的?
      

  10.   

    Sleep函数只是让调用的线程挂起了一定长度的时间,在这段时间里,线程也是无法接受到任何的输入的键盘和鼠标消息的。不信,你就搞个按钮,按下去,Sleep(10000);也就是10秒钟,然后看看你的程序是不是死掉了?(失去响应了10秒钟)
    我已经说了,如果你希望程序在函数中间能停止,响应消息再继续,必须使用线程,一个线程运行函数,另一个负责消息循环,这样才能实现你要的功能。
      

  11.   

    为什么不合道理?是jarjarbink说的理由吗?
      

  12.   

    jarjarbink兄:因此说,这个问题除用线程之外没有其它解决方法了是吧?
      

  13.   

    可以这样试一下
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, AppEvnts;type
      TForm1 = class(TForm)
        ApplicationEvents1: TApplicationEvents;
        procedure ApplicationEvents1Message(var Msg: tagMSG;
          var Handled: Boolean);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      F:boolean;//在你的OnCreate事件中付值为False;implementation{$R *.dfm}procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
    begin
      if f then//新添上的
      if (Msg.message=WM_KEYDOWN)or(Msg.message=WM_LBUTTONDOWN)or(Msg.message=WM_RBUTTONDOWN)then
      begin
        //  Close;
        //Do something here what you want to do 
      end; 
    end;procedure doit();//你的doit函数
    begin
      f:=Ture;
      Label1.caption:='Good';
      //(按任意键继续)
      do;
     //这里应该加入什么样的函数
      Label1.caption:='Bad';
      f:=False;
    end 
    end.
      

  14.   

    应该是不可以的,原因就是程序只有一个线程,你在do;这里死循环,怎么可能让程序去响应消息,还同时执行ApplictionEvents1Message函数呢?
      

  15.   

    哈哈,在delphi好象真的没有象getch()的函数!不过还是好好想想吧!
      

  16.   

    楼上的,在pascal中不是可以插汇编语言吗? 所谓的c语言中的getch0不就是个简单的dos
    0x21中断的调用吗?