Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 code golf, 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 PCG.

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

14 Answers 14

up vote 4 down vote accepted

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

Batch, 46 bytes

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

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

share|improve this answer
3  
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 12 hours ago

HTML + JavaScript (ES6), 36 + 25 = 41 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...

share|improve this answer

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 11 hours 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 11 hours ago
    
Also doesn't work if I type a quote (") – Glenn Randers-Pehrson 11 hours ago
    
I have #bash --version GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu) on Ubuntu 16:04 – Glenn Randers-Pehrson 11 hours ago
    
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 11 hours ago

TI-Basic, 56 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 continue...
Repeat getKey
End
share|improve this answer

Ruby (2.3), 52 55 54 53 bytes

Now 53 bytes by removing parentheses.

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

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

Explanation

Puts the required text:

puts"Press any key to continue..."

Run the Batch pause command and pipe output to nul:

system "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 7 hours ago
    
@WheatWizard Thanks, should I leave the edits there or remove them? – Punknoodles 6 hours 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 6 hours ago

Octave / MATLAB, 42 bytes

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

Bash 48 bytes

read -rn 1 -s -p "Press any key to continue... "
share|improve this answer

Pascal, 75 bytes

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

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

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

Haskell, 51 bytes

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

Try it online!

share|improve this answer
1  
That waits for enter. You can use getChar instead. – Christian Sievers 4 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
    
The standard invocation for perl is perl -e '[code]', right? That means that system has to be part of your byte count, since it's not part of that standard invocation, but not anything else. – Pavel 5 hours ago

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

Processing, 89 81 bytes

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

Explanation (outdated)

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 12 hours ago
    
@Pavel Nope, doesn't work – Kritixi Lithos 12 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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