Pause command like in Windows batch files

Share your advanced PureBasic knowledge/code with the community.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Pause command like in Windows batch files

Post by Quin »

This is a very small piece of code, but I needed it for an application so figured I'd share it here. It simulates something like the pause command in Windows batch scripts, which prompts the user with "Press any key to continue . . ." and halts the script until they press a key.

Code: Select all

Macro Pause
  PrintN("Press any key to continue . . .")
  Repeat
    Delay(10)
  Until Inkey() <> "" Or RawKey()
EndMacro

OpenConsole()
Pause
Enjoy :)
Last edited by Quin on Tue Feb 25, 2025 1:35 am, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Pause command like in Windows batch files

Post by BarryG »

Nice. :) But be aware that the Repeat/Until loop uses a lot of CPU (10% on my PC), so better to stick Delay(50) after Repeat to stop that. The keystroke will still respond immediately despite this, and the CPU drops to 0%, which is better.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Pause command like in Windows batch files

Post by Quin »

Hi Barry,
Thanks for this! I haven't needed to write a loop like this in a few years, so this slipped my mind. Thanks for keeping me in shape :D
I did Delay(10). This is enough on my 16-core Ryzen and should hopefully be enough for all cases, let me know if you get other results, I'd be happy to tweak it if so. :)
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Pause command like in Windows batch files

Post by Quin »

This little bit of code is now in my ConsoleTools Module.
Post Reply