36
\$\begingroup\$

Your task is to create a program that will display the following text, wait for the user to press a key (it is okay to ignore keys like ctrl, alt, caps lock, etc., as long as keys like letters, numbers, symbols, and enter are not ignored), and then terminate the program:

Press any key to continue... 

Trailing newlines are allowed. The program must exit immediately after a key is pressed. Also, the program must be fairly portable (i.e no OS-specific headers or modules, runs outside of an IDE, etc.).


The prompt must be exactly as shown above, unless a trailing newline cannot be avoided.


This is , so the shortest answer in bytes wins. This is also my first code-golf question, so I apologize if I do not know the rules on PPCG.

\$\endgroup\$
16
  • 4
    \$\begingroup\$ Is the output text case sensitive? Would WinBatch PAUSE be a valid answer (ellipsis is spread from ... to . . .)? \$\endgroup\$
    – FlipTack
    Jan 22, 2017 at 18:40
  • 13
    \$\begingroup\$ Also I'd suggest waiting for a week or 2 before accepting any answer \$\endgroup\$
    – user41805
    Jan 22, 2017 at 18:59
  • 3
    \$\begingroup\$ Most language's pause functions are probably insensitive to keys like Caps-Lock or Control (pressed on its own). Maybe you should clarify if the program is allowed not to notice those keys \$\endgroup\$
    – Luis Mendo
    Jan 22, 2017 at 19:11
  • 15
    \$\begingroup\$ You should wait for a few weeks before accepting an answer, as countless others have advised you (yet you've chosen to ignore their advice) \$\endgroup\$
    – FlipTack
    Jan 22, 2017 at 19:12
  • 12
    \$\begingroup\$ @JungHwanMin I agree with others. Please wait to accept a solution until entries have stopped rolling in. I really don't enjoy riding the Reputation Roller Coaster \$\endgroup\$
    – Suever
    Jan 22, 2017 at 19:13

34 Answers 34

20
\$\begingroup\$

Batch, 46 bytes

@echo Press any key to continue...
@pause>nul

Because pause's output contains a space before each ..

\$\endgroup\$
5
  • 5
    \$\begingroup\$ Nice redirect. Could you also send the output to, say, x? Then a file x would be created that holds the text Press any key to continue . . .. Saves 2 bytes, but I'm not sure if that goes against this challenge or any standard loopholes. \$\endgroup\$
    – steenbergh
    Jan 22, 2017 at 19:23
  • \$\begingroup\$ I'm on choice.exe-free system since a while, but I have a feeling @choice /n /m Press any key to continue... was also possible some decades ago. \$\endgroup\$
    – manatwork
    Jan 23, 2017 at 14:16
  • \$\begingroup\$ Choice has default values as J and N, so the program would only go on when J or N are pressed if I remember correctly. \$\endgroup\$ Jan 23, 2017 at 16:33
  • \$\begingroup\$ @geisterfurz007 Also, the default value is localized. For example, to me choice is equivalent to choice /c SN (for sim (yes) and não (no) in Portuguese). \$\endgroup\$ Jan 23, 2017 at 16:38
  • \$\begingroup\$ Ah, you are right, @geisterfurz007. Now I remember how silly looked my batch files having the entire alphabet enumerated. Ok, then this was a dead idea. \$\endgroup\$
    – manatwork
    Jan 23, 2017 at 16:48
15
\$\begingroup\$

HTML + JavaScript (ES6), 36 + 25 = 61 bytes

You can't really exit a JavaScript program, so clearing the webpage is the best I can think of.

onkeyup=_=>a.innerHTML=''
<a id=a>Press any key to continue...


HTML + JavaScript (ES6), 33 + 28 = 61 bytes

Alternate solution suggested by @LarsW that redirects to about:blank.

onkeyup=_=>location='about:blank'
Press any key to continue...


HTML/JavaScript, 60 bytes

Another awesome solution by @Ismael Miguel that doesn't use standalone JS. 1 byte saved!

<html onkeyup=this.innerHTML=''>Press any key to continue...


HTML + JavaScript (ES6), 26 + 28 = 54 bytes

Yet another solution by @George Reith making use of document writes.

onkeyup=_=>document.open()
Press any key to continue...

HTML + JavaScript (ES7), 23 + 28 = 51 bytes

Same program using the proposed ES7 bind operator:

onkeyup=::document.open
Press any key to continue...


As most of these solutions are not my own, do a courtesy and vote them up in the comments!

\$\endgroup\$
11
  • 10
    \$\begingroup\$ 36 + 25 is 61 or am I missing something? \$\endgroup\$
    – user64039
    Jan 23, 2017 at 10:12
  • 2
    \$\begingroup\$ @LarsW, ckjbgames Well, what are the chances? You never know until you write it! ;) \$\endgroup\$
    – darrylyeo
    Jan 23, 2017 at 19:22
  • 2
    \$\begingroup\$ <html onkeyup=this.innerHTML=''>Press any key to continue... <-- 60 bytes. Try it on jsfiddle.net/xhb69401 (it works with both <html> and <body>) \$\endgroup\$ Jan 24, 2017 at 10:45
  • 2
    \$\begingroup\$ Shorter way to clear the document and doesn't need anchor. onkeyup=_=>document.open() with Press any key to continue... for a grand total of 54 bytes. \$\endgroup\$ Jan 25, 2017 at 1:33
  • 2
    \$\begingroup\$ Also with the es-next proposed bind operator it could be reduced to onkeyup=::document.open. \$\endgroup\$ Jan 25, 2017 at 1:45
14
\$\begingroup\$

MATL, 35 bytes

'Press any key to continue...'D0$Y.

Explanation

'Press any key to continue...'      % String literal
D                                   % Display the string (has trailing newline)
0$Y.                                % Pause until the user presses any key
\$\endgroup\$
8
  • \$\begingroup\$ @LuisMendo Ah good catch \$\endgroup\$
    – Suever
    Jan 22, 2017 at 19:06
  • 2
    \$\begingroup\$ On the other hand, Y. doesn't either. Maybe the OP should clarify. Keys like Control and Caps-lock will fail in most of the solutions \$\endgroup\$
    – Luis Mendo
    Jan 22, 2017 at 19:08
  • 1
    \$\begingroup\$ @LuisMendo It is fine to miss control keys, caps lock, etc. \$\endgroup\$
    – ckjbgames
    Jan 22, 2017 at 19:46
  • \$\begingroup\$ @LuisMendo Wouldn't your modification require an enter key? Or maybe I'm mis-remembering it \$\endgroup\$
    – Suever
    Jan 22, 2017 at 20:04
  • \$\begingroup\$ @Suever Oh, of course! Enter is required \$\endgroup\$
    – Luis Mendo
    Jan 22, 2017 at 20:05
11
\$\begingroup\$

TI-Basic, 55 bytes

Basically, it loops until a key is pressed. Too bad lowercase letters are two bytes each in TI-Basic...

Disp "Press any","key to continuesin(
Repeat getKey
End

P.S. See the comment by @GoldenRatio for the explanation as to how this works. It's genius!

\$\endgroup\$
5
  • \$\begingroup\$ First of all, your byte count is currently off, that is actually 55 bytes (just made a program on TI-84+ and tested). Second, this technically isn't valid, because it doesn't display Press any key to continue... and instead it cuts to 15 chars plus ... Instead, preserving byte count, make it two disp commands. :Disp "Press any (don't forget : in your code, it counts) and Disp "key to continuesin( (The sin forces a ... in 1 byte to make up for the new line and disp, and this saves a space between any and key) \$\endgroup\$ Jan 24, 2017 at 9:18
  • \$\begingroup\$ Beat me to this, first language I thought of. \$\endgroup\$
    – C Anderson
    Jan 24, 2017 at 15:25
  • \$\begingroup\$ Thanks for the tip @GoldenRatio, I guess I was thinking getKey was 2 bytes when it's actually 1. I suppose with Text( or Output( you could fit it all on one line. But, your suggestion is better. I didn't think about using the implicit ellipsis at the end of a line. Thanks again, I'll edit that now. \$\endgroup\$
    – Timtech
    Jan 24, 2017 at 22:12
  • \$\begingroup\$ Thx for updating it, and I guess using disp with a comma has a good reason finally, since it never saved bytes. I usually put them on seperate lines because of my tendency to hit the clear button :D. Also, is there any official method of how to score the size? By this I mean, should the 9 bytes I did not count for a program name count? And should the 1 byte for the start colon that cannot be deleted count? I did mark that as 0 bytes, because for finding space I'm used to subtracting 10 for program size, but maybe the colon should count. \$\endgroup\$ Jan 25, 2017 at 9:37
  • \$\begingroup\$ No, basically if you make a completely empty program it counts for 0 bytes, then you start from there. That would be like counting the filename of a source file. Also, the beginning colon is just for show and is not actually a part of the program. In fact, every newline automatically shows a colon in the editor on the calculator, but again, these are just for show (try cemetech sourcecoder to see what I mean). Last, the bytecount is the same whether we split the Disp over two lines or not (but, it is not always this way). \$\endgroup\$
    – Timtech
    Jan 25, 2017 at 11:47
8
\$\begingroup\$

Bash, 46 43 42 bytes

Saved 1 byte thanks to @DigitalTrauma

read -rn1 -p"Press any key to continue..."

Uses the read built-in. -r makes sure it doesn't allow the user to input escapes. -n 1 allows just one character. -p is the prompt

\$\endgroup\$
6
  • \$\begingroup\$ Doesn't continue until I hit a <cr> \$\endgroup\$ Jan 22, 2017 at 20:02
  • \$\begingroup\$ @GlennRanders-Pehrson I believe that can happen if your bash does not support -n 1 . It works without hitting enter for me however (v3.2.57) and on my debain machine (v4.3.30) \$\endgroup\$
    – Downgoat
    Jan 22, 2017 at 20:04
  • \$\begingroup\$ Also doesn't work if I type a quote (") \$\endgroup\$ Jan 22, 2017 at 20:05
  • 1
    \$\begingroup\$ Does work OK if I put the line in a file (anykey.sh) and run that. Works fine too even if I remove the space between "-rn" and "1". \$\endgroup\$ Jan 22, 2017 at 20:10
  • 1
    \$\begingroup\$ -rn1 saves 1 byte \$\endgroup\$ Jan 22, 2017 at 21:39
8
\$\begingroup\$

Haskell, 51 50 bytes (Thanks @villou24)

main=putStr"Press any key to continue...">>getChar

Try it online!

\$\endgroup\$
3
  • 6
    \$\begingroup\$ That waits for enter. You can use getChar instead. \$\endgroup\$ Jan 23, 2017 at 3:40
  • 1
    \$\begingroup\$ You can save a byte by removing the space between putStr and the string. \$\endgroup\$ Jan 23, 2017 at 10:56
  • \$\begingroup\$ Let the answerer edit his own post, @ckjbgames , don't do it for him \$\endgroup\$ Jan 23, 2017 at 18:09
5
\$\begingroup\$

SmallBasic, 18 17 bytes

  • Version 2.0

17 bytes

TextWindow.Show()
  • Version 1.0

18 bytes

TextWindow.Pause()
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Couldn't you also just do TextWindow.Show()? Based on this site that should have the same effect. \$\endgroup\$
    – Suever
    Jan 23, 2017 at 14:35
5
\$\begingroup\$

Octave / MATLAB, 42 bytes

disp('Press any key to continue...');pause
\$\endgroup\$
1
  • \$\begingroup\$ I upvoted this answer a month ago, assuming it was optimal... But you've been outgolfed :) \$\endgroup\$ Mar 3, 2017 at 13:55
4
\$\begingroup\$

QBasic (QB64), 37 (42?) bytes

Unfortunately, QBasic's built-in end-of-program prompt doesn't have the ellipsis, so we'll have to print it ourselves:

?"Press any key to continue..."
SLEEP

(SLEEP without an argument waits until a keypress.)

This code does what the question literally asks for, but it doesn't seem like it fits the spirit of the question because, of course, QBasic then displays "Press any key to continue" and waits for a keypress before returning to the IDE. Here's one that goes straight to the IDE, for +5 bytes:

?"Press any key to continue..."
SLEEP
STOP

STOP is a debugging statement. In regular QBasic, it sets a breakpoint: execution halts and we return to the IDE, but execution can be resumed again with F5. It's unclear whether that would count as the program "exiting." However, I'm using the QB64 emulator, which can't do breakpoints. Upon encountering STOP, it simply halts--returning straight to the IDE without the redundant "Press any key" message.

\$\endgroup\$
1
  • \$\begingroup\$ Oddly enough, the 'Press any key to continue' also shows up when executing a standalone QB64 exe. I tried to use LOCATE to just print the dots after it, but QBasic clears the entire bottom row when it prints this message... \$\endgroup\$
    – steenbergh
    Jan 23, 2017 at 9:04
4
\$\begingroup\$

Processing, 89 81 bytes

void setup(){print("Press any key to continue...");}void draw(){if(key>0)exit();}

Explanation

void setup(){print("Press any key to continue...");}

This is required since I am using more than one function in my program. Anything inside setup() gets called, in this case the string "Press any key to continue...".

if(key>0)exit();

Checks if key (key will always contain the int value of the last key pressed) is more than 0 (ie not a null byte). If the condition is satisfied, then the program exits.

void draw(){}

draw() ensures that that the program will always keep looking for a key instead of stopping once the program starts.

(That feel when a builtin in a Java-like language is still verbose...)

\$\endgroup\$
2
  • \$\begingroup\$ I don't really know Processing, but I think you should be able to replace void steup(){...} with static{...} \$\endgroup\$
    – Pavel
    Jan 22, 2017 at 19:36
  • 2
    \$\begingroup\$ @Pavel Nope, doesn't work \$\endgroup\$
    – user41805
    Jan 22, 2017 at 19:37
4
\$\begingroup\$

Pascal, 75 65 bytes

This was tested with the Free Pascal Compiler, version 3.0.0.
It may work with TurboPascal 7 or newer.

uses Crt;begin write('Press any key to continue...');readkey;end.

Sadly, I can't replace readkey with readln since the challenge requires that any key be accepted.


I've tested this code on http://rextester.com/l/pascal_online_compiler, with and without supplying an input.
As expected, the program is terminated after 10s, since it sits waiting for a keypress that never happens.


Thanks to @manatwork for saving me 10 bytes by proving me wrong, and showing that I don't need the program _;.

\$\endgroup\$
4
  • \$\begingroup\$ Neither FreePascal nor TurboPascal requires the use of program keyword. (Actually I never met such ancient implementation that did.) \$\endgroup\$
    – manatwork
    Jan 23, 2017 at 14:03
  • \$\begingroup\$ @manatwork I tried it without program and it complained. Also, I sometimes use TurboPascal. That one requires (notice: requires) the program there. If you know any place I can test where it runs without the program, I will remove it. I hate having it there. And that uses Crt;. \$\endgroup\$ Jan 23, 2017 at 14:45
  • 1
    \$\begingroup\$ I don't get you. It works fine including the rextester service you linked. In my command line: pastebin.com/pVr9dqxd \$\endgroup\$
    – manatwork
    Jan 23, 2017 at 15:08
  • \$\begingroup\$ @manatwork You're right. I know why it complained: I wrote program; instead of actually removing that bit. Thanks for alerting me to that. \$\endgroup\$ Jan 23, 2017 at 16:31
4
\$\begingroup\$

Scratch, 81 bytes

image

when gf clicked
say [Press any key to continue...
wait until <key [any v] pressed

(Scratchblocks link)

If you wanted it to stop the entire program when you pressed a key (including other threads) you'd have to add a stop all. If you want to get rid of the say dialog you need an empty say block (stop all works as well).

Convenient that Scratch has a builtin for this!

\$\endgroup\$
4
\$\begingroup\$

Bash 48 44 42 bytes

read -rn1 -p"Press any key to continue..."

@mame98 Thanks for saving 4 bytes.

@RaisingAgent Thanks for saving 2 bytes.

\$\endgroup\$
3
  • \$\begingroup\$ You can improve this by adding up the flags, like read -srn1 -p".." and remove the last space in the promt quote. Also, I'm not sure if you need the 's' flag \$\endgroup\$
    – mame98
    Jan 23, 2017 at 13:58
  • 1
    \$\begingroup\$ remove the last space in the quotes _ & -s \$\endgroup\$
    – Penguin9
    Jan 24, 2017 at 13:15
  • \$\begingroup\$ @RaisingAgent Thanks, updated :) \$\endgroup\$
    – Abel Tom
    Jan 27, 2017 at 7:07
4
\$\begingroup\$

Python 2, 110 bytes

import curses as c
s=c.initscr()
c.cbreak()
s.addstr(0,0,'Press any key to continue...')
while not s.getch():1
\$\endgroup\$
1
  • \$\begingroup\$ Cutting it down to 98 bytes (current Python 3): from curses import* s=initscr() raw() s.addstr('Press any key to continue...') while 0>s.getch():1 \$\endgroup\$
    – movatica
    Feb 28, 2021 at 22:16
3
\$\begingroup\$

R, 56 bytes

cat('Press any key to continue...');keypress::keypress()

This works in Linux and OSX terminals.

\$\endgroup\$
3
\$\begingroup\$

Ruby (2.3) (+ Batch), 52 55 54 53 46 bytes

Now 46 bytes thanks to Alexis Andersen.

Note: Tested on Windows, might not work if there is no pause command.

puts"Press any key to continue...";`pause>nul`

Explanation

Puts the required text:

puts"Press any key to continue..."

End the line:

;

Run the Batch pause command and pipe output to nul:

`pause>nul`
\$\endgroup\$
7
  • \$\begingroup\$ Hello, welcome to PPCG. I see that you have a list of edits in the body of your submission. While this may be customary on other sites, since anyone can see the edit history of a post, we generally leave the history of the post out of the body (aside from the strike throughs for byte counts). This seems like a good first golf and I hope you have fun here. \$\endgroup\$
    – Wheat Wizard
    Jan 23, 2017 at 0:22
  • \$\begingroup\$ @WheatWizard Thanks, should I leave the edits there or remove them? \$\endgroup\$
    – notme1560
    Jan 23, 2017 at 1:23
  • 1
    \$\begingroup\$ I think that it makes for a much more readable answer if you remove them and I would personally recommend doing so. \$\endgroup\$
    – Wheat Wizard
    Jan 23, 2017 at 1:24
  • \$\begingroup\$ you can shorten that by replacing system with backticks: ` pause>nul ` or even shorter, just use gets (also, there's no good way to include a backtick in a code block in a comment) \$\endgroup\$ Jan 23, 2017 at 15:11
  • \$\begingroup\$ @AlexisAndersen doesn't gets wait for enter, and not just any key? \$\endgroup\$
    – notme1560
    Jan 23, 2017 at 16:19
3
\$\begingroup\$

Java, 127 bytes

class P{public static void main(String[]a)throws Exception{System.out.print("Press any key to continue...");System.in.read();}}

Note: the console must be set to raw mode in order for this to work.

\$\endgroup\$
2
  • \$\begingroup\$ Then I'm afraid your code should set the console so, like original.legin's Perl solution does. \$\endgroup\$
    – manatwork
    Jan 23, 2017 at 17:43
  • \$\begingroup\$ @manatwork Also, the program must be fairly portable (i.e no OS-specific headers or modules, runs outside of an IDE, etc.) if you follow that SO link there is no OS-independent way to do this. Meaning, the answer you linked is equally as valid as this one. \$\endgroup\$
    – user18932
    Jan 23, 2017 at 17:47
3
\$\begingroup\$

SmileBASIC, 52 bytes

?"Press any key to continue...
WHILE""==INKEY$()WEND
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 62 bytes

EventHandler["Press any key to continue...","KeyDown":>Exit[]]

Explanation

EventHandler["Press any key to continue...","KeyDown":>Exit[]]
EventHandler[                                                ]  (* Create a new EventHandler *)
             "Press any key to continue..."                     (* Print the dialog text *)
                                            "KeyDown":>Exit[]   (* When a key is pressed down, exit *)
\$\endgroup\$
2
\$\begingroup\$

SmileBASIC, 55 bytes

?"Press any key to continue..."@L IF INKEY$()!=""GOTO@L

Explained:

?"Press any key to continue..."  '? means PRINT
@L                               'start of loop
IF INKEY$()!="" GOTO @L          'if no key pressed goto @L
\$\endgroup\$
1
  • \$\begingroup\$ WHILE""==INKEY$()WEND is smaller \$\endgroup\$
    – 12Me21
    Jan 24, 2017 at 6:21
2
\$\begingroup\$

Python 2/3 POSIX, 85 bytes

import sys,tty
print('Press any key to continue...')
tty.setraw(1)
sys.stdin.read(1)
\$\endgroup\$
2
\$\begingroup\$

Python 3, 65 bytes

Requires the Windows version of Python.

from msvcrt import*
print("Press any key to continue...")
getch()

msvcrt.getch() doesn't wait for the enter key to be pressed like input(), it returns the first key pressed.

Python Docs for msvcrt.getch(): https://docs.python.org/3/library/msvcrt.html#msvcrt.getch

Thanks to @Fliptack for saving some bytes

\$\endgroup\$
3
  • \$\begingroup\$ This answer is invalid without an import msvcrt somewhere \$\endgroup\$
    – FlipTack
    Jan 23, 2017 at 17:48
  • 2
    \$\begingroup\$ As this is code-golf you should remove the comments from your code. Also, using from msvcrt import* appears to be 1 byte shorter \$\endgroup\$
    – FlipTack
    Jan 23, 2017 at 18:00
  • \$\begingroup\$ cool, thx @FlitTack \$\endgroup\$
    – Cutwell
    Jan 24, 2017 at 18:48
2
\$\begingroup\$

Node.js, 102 101 99 bytes

with(process)stdout.write('Press any key to continue...'),s=stdin,s.setRawMode(1),s.on('data',exit)
\$\endgroup\$
9
  • \$\begingroup\$ Not much gain, just 1 character shorter with with: pastebin.com/BhrAyq2K \$\endgroup\$
    – manatwork
    Jan 24, 2017 at 8:56
  • \$\begingroup\$ @manatwork Nice one! First time with has saved me bytes (or byte) \$\endgroup\$ Jan 24, 2017 at 10:05
  • 1
    \$\begingroup\$ Now you can remove the curly brackets and replace line breaks with commas for 2 more bytes. \$\endgroup\$
    – darrylyeo
    Jan 25, 2017 at 15:56
  • \$\begingroup\$ Also if I'm not mistaken, stdout.write`Press any key to continue...` will save 2 bytes in ES6. \$\endgroup\$
    – darrylyeo
    Jan 25, 2017 at 18:43
  • \$\begingroup\$ @darrylyeo Commas work thats awesome thanks! Unfortunately a tagged template literal can't be used here as it actually passes in an array and process.stdout.write doesn't do implicit conversions to string (it just errors). \$\endgroup\$ Jan 25, 2017 at 23:17
2
\$\begingroup\$

Sinclair ZX81/Timex TS1000 BASIC: Method 1 approximately 41 bytes

1 PRINT "PRESS ANY KEY TO CONTINUE..."
2 GOTO (INKEY$<>"")+VAL "2"

Method 2 approximately 38 BYTES

1 PRINT "PRESS ANY KEY TO CONTINUE..."
2 PAUSE VAL "4E4"

I prefer method 1 as on the ZX81, there is a screen flicker when PAUSE is called, and if you want long enough (providing the ZX81 doesn't overheat or crash) the pause will eventually come to an end, whereas method 1 is stuck in an infinite loop until a key is pressed, and no screen flicker.

I'll work out the correct number of bytes used later when I have the right bit of BASIC that will tell me. By the way, using VAL "x" instead of the number saves valuable RAM on a ZX81 (I think that this is the same for the ZX Spectrum as well).

\$\endgroup\$
4
  • \$\begingroup\$ Both methods will work on the Sinclair ZX Spectrum as well, the advantage being that the Spectrum has upper and lower case letters, whereas the ZX81 does not by default (and doesn't have a single quote either as a standard character). \$\endgroup\$ Jan 26, 2017 at 10:56
  • \$\begingroup\$ Method 2 on the Sinclair ZX Spectrum could be condensed to 1 PRINT "Press any key to continue...": PAUSE 4e4 \$\endgroup\$ Feb 10, 2017 at 17:00
  • 1
    \$\begingroup\$ PAUSE 0 pauses for ever. \$\endgroup\$
    – Neil
    Feb 19, 2017 at 22:34
  • \$\begingroup\$ Yes I've not programmed in Sinclair Spectrum BASIC for so long that I forgot about that. PAUSE 0 in ZX81 BASIC does not pause forever. \$\endgroup\$ Mar 6, 2017 at 17:45
1
\$\begingroup\$

Perl 5, 79 bytes

system "stty cbreak";$|=1;print "Press any key to continue...";read(STDIN,$a,1)

used as:

perl -e 'system "stty cbreak";$|=1;print "Press any key to continue...";read(STDIN,$a,1)'

No prizes of course. I'm sure some perl person will have a better way.

(89 bytes if the interpreter invocation as well needs to be included in the count)

\$\endgroup\$
6
  • \$\begingroup\$ @Pavel, he counted system in his bytecount, so I don't know what you meant with that comment. \$\endgroup\$
    – Dada
    Jan 23, 2017 at 16:27
  • 1
    \$\begingroup\$ @original.legin I think Perl won't do much better than this. A few bytes you can golf still: $|=1 isn't necessary, you have a space after print, you can drop the parenthesis around the arguments of read, you can use backticks instead of system, and finally, print returns 1, so you can use it instead of the litteral 1 in the call to read. So in the end, we get: perl -e '`stty cbreak`;read STDIN,$a,print"Press any key to continue..."' \$\endgroup\$
    – Dada
    Jan 23, 2017 at 16:30
  • \$\begingroup\$ @Dada You can also shave off 2 bytes with say, although I think this approach fails the "portable" requirement. The best portable solution I can find is perl -MTerm::ReadKey -E'say"Press any key to continue...";ReadMode 3;ReadKey' (52 bytes + 16 bytes for -MTerm::ReadKey<space>). Be careful, this will screw up your terminal unless you restore the read mode at the end with ReadMode 1. \$\endgroup\$ Jan 23, 2017 at 19:07
  • 1
    \$\begingroup\$ @ThisSuitIsBlackNot Indeed, I didn't see that requirement about portability. About say, I tend not to use it anymore (I agree mostly with this meta post). Now back to a short code, IO::Prompt will be shorter than Term::ReadKey (40 + 13 = 53 bytes) : perl -MIO::Prompt -e 'prompt"Press any key to continue... ",-1'. And as it happens, it's even shorter than what I suggested in my previous comment. \$\endgroup\$
    – Dada
    Jan 23, 2017 at 19:54
  • 1
    \$\begingroup\$ @Dada I can see both sides of the -E argument. The last time I checked, though, J B's answer had been uncontested for four years, so that's what I've followed. Maybe I need to revisit it again. As for IO::Prompt, good find! Although it doesn't work on my Strawberry 5.20.2 on Windows, while Term::ReadKey does. \$\endgroup\$ Jan 23, 2017 at 20:11
1
\$\begingroup\$

PHP, 73 bytes

echo"Press any key to continue...";$h=fopen("php://stdin","r");fgets($h);

Run it in the PHP interactive shell (php -a)

\$\endgroup\$
1
  • \$\begingroup\$ “The program must exit immediately after a key is pressed.” – This waits until Enter or blackout… \$\endgroup\$
    – manatwork
    Jan 23, 2017 at 13:55
1
\$\begingroup\$

C#, 101 bytes

using C=System.Console;class P{static void Main(){C.Write("Press any key to continue...");C.Read();}}

Tested on Linux, should run on any system having the .NET libraries and the Common Language Runtime.

Ungolfed program:

using C = System.Console; // creating a shorter alias for the System.Console class

class P
{
    static void Main()
    {
        C.Write("Press any key to continue...");    // prints the message
        C.Read();   // waits for key press
    }
}

CTRL, ALT, SHIFT are ignored. The pressed key will be echoed on screen if printable.

Echo can be disabled by replacing C.Read() with C.ReadKey(0<1) at the cost of 6 more bytes.

\$\endgroup\$
1
\$\begingroup\$

8th, 47 bytes

"Press any key to continue..." . cr con:key bye

This program ignores keys like ctrl, alt, caps lock. Quits with keys like letters, numbers, symbols, and enter.

Explanation

"Press any key to continue..." . cr \ Print message 
con:key                             \ Wait for key input from console  
bye                                 \ Quit program 
\$\endgroup\$
1
  • \$\begingroup\$ On Windows only you can save 3 bytes omitting cr \$\endgroup\$ Jan 25, 2017 at 7:13
1
\$\begingroup\$

C#, 29 bytes

class P{static void Main(){}}

Not sure if this is considered valid because it prints:

Press any key to continue . . .

But there is a Batch answer that prints this as well.

\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 39 bytes

." Press any key to continue..."key bye

(Yes, there is already an 8th solution but this is shorter)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.