Tempo - delay ou inkey

Top  Previous  Next

Esta função funciona igual ao Delay/Sleep porém ela sai se alguém pressionar uma tecla ou clicar o mouse - igual a Inkey do Clipper

procedure WaitUntilKeyPressed(seconds: Integer);

var

  i: Byte;

  j: integer;

  KeyboardState: TKeyboardState;

  keypress: Boolean;

  Msg: TMsg;

begin

  // Limpa buffer do teclado/mouse

  while PeekMessage(Msg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE or PM_NOYIELD) do;

  while PeekMessage(Msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE or PM_NOYIELD) do;

  // aguarda n segundos...

  for j := 0 to (seconds * 100do

  begin

    keypress := False;

    GetKeyboardState(KeyboardState);

    for i := 0 to 255 do

    begin

      if (KeyboardState[i] and 128) = 128 then

      begin

        // ...sai se houve o pressionamento de uma tecla ou clique do mouse

        keypress := True;

        Break;

      end;

    end;

    if keypress then

      Break;

    Sleep(10);

    Application.ProcessMessages;

  end;

end