我如何在delphi控制台应用程序中实现IsKeyPressed函数?
我有一个delphi控制台应用程序,当用户按任意键时我需要终止它,问题是我不知道如何实现一个函数来检测何时按下一个键,我想做这样的事情。
{$APPTYPE CONSOLE}
begin
MyTask:=MyTask.Create;
try
MyTask.RunIt;
while MyTask.Running and not IsKeyPressed do //how i can implement a IsKeyPressed function?
MyTask.SendSignal($56100AA);
finally
MyTask.Stop;
MyTask.Free;
end;
结尾。
I have a delphi console application which i need terminate when the user press any key, the problem is which i don't know how implement a function to detect when a key is pressed , i want to do something like so.
{$APPTYPE CONSOLE}
begin
MyTask:=MyTask.Create;
try
MyTask.RunIt;
while MyTask.Running and not IsKeyPressed do //how i can implement a IsKeyPressed function?
MyTask.SendSignal($56100AA);
finally
MyTask.Stop;
MyTask.Free;
end;
end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个函数来检测是否按下了某个键,检查
控制台输入缓冲区
。首先,您必须调用 GetNumberOfConsoleInputEvents 函数来获取events,然后使用 PeekConsoleInput 函数并检查事件是否为
KEY_EVENT
最后使用 FlushConsoleInputBuffer。检查这个样本
You can write a function to detect if a key was pressed checking the
console input buffer
.First you must call the GetNumberOfConsoleInputEvents function to get the number of events, then retrieve the event using the PeekConsoleInput function and check if the event is a
KEY_EVENT
finally flush the console input buffer using FlushConsoleInputBuffer.Check this sample