Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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

Write a program which generates keyboard events which type Hello, World!.

It doesn't have to be platform-independent.

It can't leave any keys pressed. For example, in Java, Robot.KeyPress('H'); will leave H pressed, so you must release it with Robot.KeyRelease('H');

You may assume that the caps lock is off when your program is run. Capital letters can be typed either by holding shift and pressing the letter, or by toggling caps lock. For the !, you'll probably have to use the shift method.

Shortest code wins.

share|improve this question

11 Answers 11

up vote 9 down vote accepted

C, Linux (186 chars)

main(){char*s="Hello, World!",d[99],*c;fscanf(popen("tty","r"),"%s",d);int D=open(d,O_RDWR);for(c=s;*c;c++)ioctl(D,TIOCSTI,c);}

After applying ugoren's suggestions:

(111 chars)

main(D){char*c,d[99];fscanf(popen("tty","r"),"%s",d);D=open(d,2);for(c="Hello, World!";*c;)ioctl(D,21522,c++);}
share|improve this answer
You can shorten some. Remove s (just initialize for(c="H..), increment c in the ioctl, main(D) to save int. Maybe also remove some includes (even hard-code O_RDWR and TIOCSTI). – ugoren Feb 25 '13 at 14:44
@ugoren Thanks, I'll try them :) – saeedn Feb 26 '13 at 0:44
This one is not the shortest. Why is this the accepted answer? (I think it is a nice answer, but I just want to learn how the site works.) – 11684 Mar 11 '13 at 12:56
@11684 The author of the shortest answer asked me not to count it, so I accepted the shortest non-trivial answer instead. – cardboard_box Mar 11 '13 at 23:44
No need to call tty it only works if stdin is a tty, in which case, 0 is already an open terminal fd. – Dave Dec 9 '13 at 8:38

AppleScript, 50

tell app"System Events"to keystroke"Hello, World!"

Thanks to marinus for the suggestion.

share|improve this answer
Huh. Looks like you chose the right language for the right job. – cardboard_box Feb 23 '13 at 2:18
You can shorten it to tell app"System Events"to keystroke"Hello, World!". – marinus Feb 25 '13 at 5:01
Damnit, I thought of this independently... +1! – 11684 Mar 11 '13 at 12:54

Autohotkey (20)

Please don't count this one. I don't want to cheat out people who have actually done this with a language not meant for sending keystrokes. This answer is just for completion :P

send Hello, World{!}

share|improve this answer

C in Windows 7, 201

#define S SendInput(1,&k,28);
int k[7]={1};main(i){char*s="^HELLO, ^WORLD^1";for(i=0;i<16;i++){k[2]=0;if(s[i]>90){k[1]=16;S i++;}k[1]=s[i];S k[2]=2;S k[1]=16;S}}

Program result:


C:\My\Directory>Hello, World!
share|improve this answer

Ducky Script for USB Rubber Ducky (22)

String "Hello, World!"
share|improve this answer

AutoIt3 - (21)

Send("Hello, World!")
share|improve this answer
missing comma! It's actually 21 chars – Doorknob Mar 8 '13 at 21:51
Where is a comma supposed to be? Edit, never mind, lol. – jdstankosky Mar 11 '13 at 20:18

Emacs Lisp - 34 chars

(execute-kbd-macro"Hello, World!")

share|improve this answer

Tcl, 46 chars

package r Expect;spawn bash;send Hello,\ World

Expect, 29 chars

spawn bash;send Hello,\ World
share|improve this answer

InstantEXE 3.0, 20 characters

Keys "Hello, World!"
share|improve this answer

PowerShell - 55

(New-Object -c'Hello, World!')
share|improve this answer

VBScript, 54

createobject("").sendkeys "Hello, World!"
share|improve this answer

Your Answer


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.