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.

share|improve this question
3  
Is the output text case sensitive? Would WinBatch PAUSE be a valid answer (ellipsis is spread from ... to . . .)? – FlipTack 2 days ago
12  
Also I'd suggest waiting for a week or 2 before accepting any answer – Kritixi Lithos 2 days ago
3  
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 – Luis Mendo 2 days ago
13  
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) – FlipTack 2 days ago
12  
@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 – Suever 2 days ago

30 Answers 30

Batch, 46 bytes

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

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

share|improve this answer
4  
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. – steenbergh 2 days ago
    
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. – manatwork yesterday
    
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. – geisterfurz007 yesterday
    
@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). – Ismael Miguel yesterday
    
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. – manatwork yesterday

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!

share|improve this answer
1  
Perhaps a redirect to about:blank? – LarsW yesterday
10  
36 + 25 is 61 or am I missing something? – Masterzagh yesterday
2  
@LarsW, ckjbgames Well, what are the chances? You never know until you write it! ;) – darrylyeo yesterday
1  
@darrylyeo It turn out that the two snippets are the same length. :) – ckjbgames yesterday
1  
<html onkeyup=this.innerHTML=''>Press any key to continue... <-- 60 bytes. Try it on jsfiddle.net/xhb69401 (it works with both <html> and <body>) – Ismael Miguel 20 hours ago

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
share|improve this answer
    
@LuisMendo Ah good catch – Suever 2 days ago
2  
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 – Luis Mendo 2 days ago
1  
@LuisMendo It is fine to miss control keys, caps lock, etc. – ckjbgames 2 days ago
    
@LuisMendo Wouldn't your modification require an enter key? Or maybe I'm mis-remembering it – Suever 2 days ago
    
@Suever Oh, of course! Enter is required – Luis Mendo 2 days ago

Batch, 5 bytes

pause

This really is IT.


Batch, 40 bytes
--------------------

echo Press any key to continue...
pause

--------------------
If '...' instead of '. . .' - It's @Neil's then, but better.

share|improve this answer
5  
is this paritcular OS dependant? on my system it prints Press any key to continue . . . instead of Press any key to continue... – Alexis Andersen yesterday
1  
Lel, that's a real game changer now. :'( - though on mine it does exactly that: Press any key to continue... – RaisingAgent yesterday
    
well it's worth noting, I'm just emulating batch in windows. And either way, this might still be a valid solution. I'm not familiar with the rules on platform dependency. – Alexis Andersen yesterday
4  
just so you know, @orlp already posted this answer, but it was deleted for being invalid... (Edit: rahnema1 also posted and deleted this) – FlipTack yesterday
1  
hmm, so the OP really wants it to consistently say '...' instead of '. . .' – RaisingAgent 21 hours ago

Haskell, 51 50 bytes (Thanks @villou24)

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

Try it online!

share|improve this answer
6  
That waits for enter. You can use getChar instead. – Christian Sievers 2 days ago
1  
You can save a byte by removing the space between putStr and the string. – villou24 yesterday
    
Let the answerer edit his own post, @ckjbgames , don't do it for him – Mistah Figgins yesterday

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!

share|improve this answer
    
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) – Golden Ratio 21 hours ago
    
Beat me to this, first language I thought of. – C Anderson 15 hours ago
    
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. – Timtech 8 hours ago

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

share|improve this answer
    
Doesn't continue until I hit a <cr> – Glenn Randers-Pehrson 2 days ago
    
@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) – Downgoat 2 days ago
    
Also doesn't work if I type a quote (") – Glenn Randers-Pehrson 2 days ago
1  
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". – Glenn Randers-Pehrson 2 days ago
1  
-rn1 saves 1 byte – Digital Trauma 2 days ago

Octave / MATLAB, 42 bytes

disp('Press any key to continue...');pause
share|improve this answer

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.

share|improve this answer
    
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... – steenbergh yesterday

SmallBasic, 18 bytes

TextWindow.Pause()
share|improve this answer
2  
Couldn't you also just do TextWindow.Show()? Based on this site that should have the same effect. – Suever yesterday

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 _;.

share|improve this answer
    
Neither FreePascal nor TurboPascal requires the use of program keyword. (Actually I never met such ancient implementation that did.) – manatwork yesterday
    
@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;. – Ismael Miguel yesterday
1  
I don't get you. It works fine including the rextester service you linked. In my command line: pastebin.com/pVr9dqxd – manatwork yesterday
    
@manatwork You're right. I know why it complained: I wrote program; instead of actually removing that bit. Thanks for alerting me to that. – Ismael Miguel yesterday

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...)

share|improve this answer
    
I don't really know Processing, but I think you should be able to replace void steup(){...} with static{...} – Pavel 2 days ago
2  
@Pavel Nope, doesn't work – Kritixi Lithos 2 days ago

Bash 48 44 bytes

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

@mame98 Thanks for saving 4 bytes.

share|improve this answer
    
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 – mame98 yesterday
    
remove the last space in the quotes _ & -s – RaisingAgent 17 hours ago

Python 2, 117 bytes

import curses as c
s=c.initscr()
c.cbreak()
s.addstr(0,0,'Press any key to continue...')
while not s.getch():1
exit()
share|improve this answer
    
you do not need to call exit() in the end – ovs 9 hours ago

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`
share|improve this answer
    
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. – Wheat Wizard 2 days ago
    
@WheatWizard Thanks, should I leave the edits there or remove them? – Punknoodles 2 days ago
1  
I think that it makes for a much more readable answer if you remove them and I would personally recommend doing so. – Wheat Wizard 2 days ago
    
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) – Alexis Andersen yesterday
    
@AlexisAndersen doesn't gets wait for enter, and not just any key? – Punknoodles yesterday

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.

share|improve this answer
    
Then I'm afraid your code should set the console so, like original.legin's Perl solution does. – manatwork yesterday
    
@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. – Snowman yesterday

SmileBASIC, 52 bytes

?"Press any key to continue...
WHILE""==INKEY$()WEND
share|improve this answer

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 *)
share|improve this answer

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
share|improve this answer
    
WHILE""==INKEY$()WEND is smaller – 12Me21 yesterday

R, 56 bytes

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

This works in Linux and OSX terminals.

share|improve this answer

Python 2/3 POSIX, 85 bytes

import sys,tty
print('Press any key to continue...')
tty.setraw(1)
sys.stdin.read(1)
share|improve this answer

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

share|improve this answer
    
This answer is invalid without an import msvcrt somewhere – FlipTack yesterday
    
thanks, added and fixed – Cutwell yesterday
2  
As this is code-golf you should remove the comments from your code. Also, using from msvcrt import* appears to be 1 byte shorter – FlipTack yesterday
    
cool, thx @FlitTack – Cutwell 12 hours ago

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)

share|improve this answer
    
@Pavel, he counted system in his bytecount, so I don't know what you meant with that comment. – Dada yesterday
1  
@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..."' – Dada yesterday
    
@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. – ThisSuitIsBlackNot yesterday
    
@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. – Dada yesterday
    
@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. – ThisSuitIsBlackNot yesterday

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)

share|improve this answer
    
“The program must exit immediately after a key is pressed.” – This waits until Enter or blackout… – manatwork yesterday

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.

share|improve this answer

Node.js, 102 101 bytes

with(process){stdout.write('Press any key to continue...')
s=stdin
s.setRawMode(1)
s.on('data',exit)}
share|improve this answer
    
Not much gain, just 1 character shorter with with: pastebin.com/BhrAyq2K – manatwork 22 hours ago
    
@manatwork Nice one! First time with has saved me bytes (or byte) – George Reith 20 hours ago

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 
share|improve this answer

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.

share|improve this answer

Batch 5 bytes

pause

Yup. Take that golf languages.

If that is not allowed:

SmallBasic 18 bytes

TextWindow.Show();
share|improve this answer
1  
I think this answer is invalid because it displays spaces between the periods, and the challenge says The prompt must be exactly as shown above – DJMcMayhem 8 hours ago
    
Hmm. that is annoying. I guess I will wait for the OP – Christopher Peart 7 hours ago
    
Also, just so you know, there are 3 other answers (2 now deleted) using the same solution. – DJMcMayhem 7 hours ago
    
Huh. Well it is a easy solution – Christopher Peart 7 hours ago
    
@DJMcMayhem added a smallbasic answer – Christopher Peart 7 hours ago

Javascript, 52 51 45 44 bytes

alert('Press any key to continue...');close()

close(alert('Press any key to continue...'))

Insert into the dev console of your browser, hit Enter :-).

@manatwork saved 7 all bytes

share|improve this answer
    
saved 1 byte, removing the last semicolon – RaisingAgent 17 hours ago
1  
If you not wrote it as window.alert(), then why use window. the 2nd time? – manatwork 17 hours ago
1  
BTW, close() expects no parameter, so if receives one just ignores it: close(alert('Press...')). – manatwork 17 hours ago
    
thx @manatwork – RaisingAgent 17 hours ago
    
This solution isn't valid. You should be able to press any key, not just Enter. – darrylyeo 6 hours ago

protected by Community yesterday

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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