我如何在delphi控制台应用程序中实现IsKeyPressed函数?

发布于 2024-11-04 16:09:50 字数 359 浏览 0 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被翻牌 2024-11-11 16:09:50

您可以编写一个函数来检测是否按下了某个键,检查控制台输入缓冲区

每个控制台都有一个输入缓冲区
包含输入事件队列
记录。当控制台的窗口有
键盘焦点,控制台格式
每个输入事件(例如单个
击键、鼠标移动,或
单击鼠标按钮)作为输入
记录它放置在控制台的
输入缓冲区。

首先,您必须调用 GetNumberOfConsoleInputEvents 函数来获取events,然后使用 PeekConsoleInput 函数并检查事件是否为 KEY_EVENT 最后使用 FlushConsoleInputBuffer

检查这个样本

function KeyPressed:Boolean;
var
  lpNumberOfEvents     : DWORD;
  lpBuffer             : TInputRecord;
  lpNumberOfEventsRead : DWORD;
  nStdHandle           : THandle;
begin
  Result:=false;
  //get the console handle
  nStdHandle := GetStdHandle(STD_INPUT_HANDLE);
  lpNumberOfEvents:=0;
  //get the number of events
  GetNumberOfConsoleInputEvents(nStdHandle,lpNumberOfEvents);
  if lpNumberOfEvents<> 0 then
  begin
    //retrieve the event
    PeekConsoleInput(nStdHandle,lpBuffer,1,lpNumberOfEventsRead);
    if lpNumberOfEventsRead <> 0 then
    begin
      if lpBuffer.EventType = KEY_EVENT then //is a Keyboard event?
      begin
        if lpBuffer.Event.KeyEvent.bKeyDown then //the key was pressed?
          Result:=true
        else
          FlushConsoleInputBuffer(nStdHandle); //flush the buffer
      end
      else
      FlushConsoleInputBuffer(nStdHandle);//flush the buffer
    end;
  end;
end;

You can write a function to detect if a key was pressed checking the console input buffer.

Each console has an input buffer that
contains a queue of input event
records. When a console's window has
the keyboard focus, a console formats
each input event (such as a single
keystroke, a movement of the mouse, or
a mouse-button click) as an input
record that it places in the console's
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

function KeyPressed:Boolean;
var
  lpNumberOfEvents     : DWORD;
  lpBuffer             : TInputRecord;
  lpNumberOfEventsRead : DWORD;
  nStdHandle           : THandle;
begin
  Result:=false;
  //get the console handle
  nStdHandle := GetStdHandle(STD_INPUT_HANDLE);
  lpNumberOfEvents:=0;
  //get the number of events
  GetNumberOfConsoleInputEvents(nStdHandle,lpNumberOfEvents);
  if lpNumberOfEvents<> 0 then
  begin
    //retrieve the event
    PeekConsoleInput(nStdHandle,lpBuffer,1,lpNumberOfEventsRead);
    if lpNumberOfEventsRead <> 0 then
    begin
      if lpBuffer.EventType = KEY_EVENT then //is a Keyboard event?
      begin
        if lpBuffer.Event.KeyEvent.bKeyDown then //the key was pressed?
          Result:=true
        else
          FlushConsoleInputBuffer(nStdHandle); //flush the buffer
      end
      else
      FlushConsoleInputBuffer(nStdHandle);//flush the buffer
    end;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文