在控制台程序中,为了方便观察结果,程序需要暂停,实现“按任意键继续”的效果,但以下代码只能按回车键继续。
begin
  writeln('press [Enter] key to continue...');
  readln;
end;
如何办?

解决方案 »

  1.   

    begin
      writeln('press [Enter] key to continue...');
      read;  {这样试试,未经测试}
    end;
      

  2.   

    我记得在TP7时用
    writeln('Press Any Key to continue...');
    if KeyPressed then
      
    但我在Delphi下查不到有这个方法
      

  3.   

    等待键盘事件:uses windows;VAR
      h:THandle;
      buf:INPUT_RECORD;
      n:DWORD;h:=GetStdHandle(STD_INPUT_HANDLE);
    while true do
     begin
     ReadConsoleInput(h,buf,1,n);
     if (n=1) and (buf.EventType=KEY_EVENT) then
      break;
     Sleep(100);
     end;//注意 h 不必CloseHandle
      

  4.   

    TP中有“KeyPressed”?!,没见过。关注
      

  5.   

    谢谢 cg1120,
    但我还是不行,因为我用主程序调用Dos控制台,并不知如何获取当前控制台的Handle???
    h:=GetStdHandle(STD_INPUT_HANDLE);
      

  6.   

    c代码可以吗?
    system("pause");
    我不知道用delphi如何实现.
    其实就是调用内部命令pause.
      

  7.   

    function ReadKey: Char;
    var
      NumRead: Cardinal;
      InputRec: TInputRecord;
    begin
      while not ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), InputRec, 1, NumRead) or
              (InputRec.EventType <> KEY_EVENT) do;
      Result := InputRec.Event.KeyEvent.AsciiChar;
    end;
      

  8.   

    你可以写一个while 循环,
      

  9.   

    大家没见过我以前的回答吗?早就说了的了呀比楼上的都好用!还可以知道你按了那一个键program ReadKeyApp;{$APPTYPE CONSOLE}uses Windows, SysUtils;// ReadKey for Win32 consoletype TKeyShiftState = (kssRightAlt, kssLeftAlt,
       kssRightCtrl, kssLeftCtrl, kssShift, kssNumLock,
       kssScrollLock, kssCapsLock, kssEnhancedKey);type TKeyShiftSet = set of TKeyShiftState;type
       PKeyEventRecordFix = ^TKeyEventRecordFix;
       TKeyEventRecordFix = packed record
          bKeyDown: BOOL;
          wRepeatCount: Word;
          wVirtualKeyCode: Word;
          wVirtualScanCode: Word;
          KeyShiftState: TKeyShiftSet;
          case Integer of
     0: (
        UnicodeChar: WCHAR;
        dwControlKeyState: DWORD);  // Fix an error in Windows.pas
     1: (
        AsciiChar: CHAR)
       end;var
       VirtualKeyCode: Word;
       VirtualScanCode: Word;{
      This function checks whether we are interested in this particular
      keypress - we want to ignore Shift,Alt,Ctrl and the ??-Lock keys.  Use the KeyShiftState field to test for whether anyone is pressing
      these keys ...
    }
    function VetoOnVKCode(const VKCode: Word): Boolean;
    const
      NumVetoKeys = 6;
      VetoKey: array[1..NumVetoKeys] of Word = (VK_CONTROL, VK_SHIFT,
        VK_MENU, VK_CAPITAL,
        VK_NUMLOCK, VK_SCROLL);
    var
      iKey: Integer;
    begin
       for iKey := 1 to NumVetoKeys do
          if VKCode = VetoKey[iKey] then
          begin
     Result := True;
     Exit
          end;
       Result := False
    end;function ReadKey: Char;
    var
       NumRead:   Cardinal;
       HConsoleInput: THandle;
       InputRec:   TInputRecord;
    begin
    {
      Get Input-handle for the console ...
    }
       HConsoleInput := GetStdHandle(STD_INPUT_HANDLE);
    {
      Put thread to sleep until Console-Input is available- don't waste CPU
      cycles deadlocking the machine ...
    }
       repeat
          if WaitForSingleObject(HConsoleInput,INFINITE) <>
     WAIT_OBJECT_0 then
     raise Exception.Create('Invalid handle for console input');
    // Input buffer destroyed during Wait
          if ReadConsoleInput(HConsoleInput,
    InputRec,
    1,
    NumRead) and
     (InputRec.EventType = KEY_EVENT) then
          begin
     with PKeyEventRecordFix(@InputRec.Event.KeyEvent)^ do
     if bKeyDown and   // Input buffer contains key-presses AND
       // key-releases. We only want the presses.
        not VetoOnVKCode(wVirtualKeyCode) then
     begin
        VirtualScanCode := wVirtualScanCode;
        VirtualKeyCode := wVirtualKeyCode;
        KeyShiftState := TKeyShiftSet(LongRec(dwControlKeyState).Lo);
        Result := AsciiChar;
        Exit;
     end;
          end;
       until False;
    end;begin
      Writeln('Press any key to continue ...');
      ReadKey;
      Writeln('Press any key to Exit ...');
      ReadKey;
    end.
      

  10.   

    楼上的是好用(试过了),readln会让用户乱输入;read (没有参数的)不管用;
    在2002年年初的程序员杂志上还有别的办法,乱七八糟一大堆。
    谢楼上的。