在控制台程序中,为了方便观察结果,程序需要暂停,实现“按任意键继续”的效果,但以下代码只能按回车键继续。
begin
writeln('press [Enter] key to continue...');
readln;
end;
如何办?
begin
writeln('press [Enter] key to continue...');
readln;
end;
如何办?
解决方案 »
- 谁对东进语音卡比较熟悉
- AdvColumnGrid 如何设置可以让行高自适应文字内容
- vb的Me.hwnd
- 点击ListView出错.....高手请进....在线等......
- 时间延迟的问题,谁写一个时候延迟的方法吗?不要占用cup的使用率。
- ◆◆fastreport报表版本转换问题◆◆
- 考勤程序的關建問題?
- 诸位兄弟,本人以前是用VB的,现在想学DELPHI,这里问一个简单问题
- 求救:一个关于操纵Word里表格的问题
- 本人做了一个打印报表程序,出现了一个问题,请朋友帮忙,在下不胜感激!
- TListView的FindData的用法!
- 我用sakmail做邮件发送程序的时候遇到的问题,在线等待
writeln('press [Enter] key to continue...');
read; {这样试试,未经测试}
end;
writeln('Press Any Key to continue...');
if KeyPressed then
但我在Delphi下查不到有这个方法
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
但我还是不行,因为我用主程序调用Dos控制台,并不知如何获取当前控制台的Handle???
h:=GetStdHandle(STD_INPUT_HANDLE);
system("pause");
我不知道用delphi如何实现.
其实就是调用内部命令pause.
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;
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.
在2002年年初的程序员杂志上还有别的办法,乱七八糟一大堆。
谢楼上的。