11
\$\begingroup\$

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.

\$\endgroup\$

12 Answers 12

18
\$\begingroup\$

AppleScript, 50 bytes

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

Thanks to marinus for the suggestion.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Huh. Looks like you chose the right language for the right job. \$\endgroup\$ Feb 23, 2013 at 2:18
  • \$\begingroup\$ You can shorten it to tell app"System Events"to keystroke"Hello, World!". \$\endgroup\$
    – marinus
    Feb 25, 2013 at 5:01
  • \$\begingroup\$ Damnit, I thought of this independently... +1! \$\endgroup\$
    – 11684
    Mar 11, 2013 at 12:54
13
\$\begingroup\$

Autohotkey, 20 bytes

send Hello, World{!}

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

\$\endgroup\$
0
10
\$\begingroup\$

C (Linux), 186 bytes

#include<sys/ioctl.h>
#include<fcntl.h>
#include<stdio.h>
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 bytes

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++);}
\$\endgroup\$
5
  • \$\begingroup\$ 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). \$\endgroup\$
    – ugoren
    Feb 25, 2013 at 14:44
  • \$\begingroup\$ @ugoren Thanks, I'll try them :) \$\endgroup\$
    – saeedn
    Feb 26, 2013 at 0:44
  • \$\begingroup\$ 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.) \$\endgroup\$
    – 11684
    Mar 11, 2013 at 12:56
  • \$\begingroup\$ @11684 The author of the shortest answer asked me not to count it, so I accepted the shortest non-trivial answer instead. \$\endgroup\$ Mar 11, 2013 at 23:44
  • \$\begingroup\$ No need to call tty it only works if stdin is a tty, in which case, 0 is already an open terminal fd. \$\endgroup\$
    – Dave
    Dec 9, 2013 at 8:38
3
\$\begingroup\$

Emacs Lisp, 34 bytes

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

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

C in Windows 7, 201 bytes

#include<windows.h>
#include<winable.h>
#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>type.exe

C:\My\Directory>Hello, World!
\$\endgroup\$
1
\$\begingroup\$

AutoIt3, 21 bytes

Send("Hello, World!")
\$\endgroup\$
2
  • \$\begingroup\$ missing comma! It's actually 21 chars \$\endgroup\$
    – Doorknob
    Mar 8, 2013 at 21:51
  • \$\begingroup\$ Where is a comma supposed to be? Edit, never mind, lol. \$\endgroup\$ Mar 11, 2013 at 20:18
1
\$\begingroup\$

VBScript, 54 bytes

createobject("wscript.shell").sendkeys "Hello, World!"
\$\endgroup\$
1
\$\begingroup\$

Tcl, 46 bytes

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

Expect, 29 bytes

spawn bash;send Hello,\ World
\$\endgroup\$
1
  • \$\begingroup\$ Tried to do the Tk version, but failed: file with some stuff pastebin.ca/3897417 \$\endgroup\$
    – sergiol
    Oct 28, 2017 at 23:08
1
\$\begingroup\$

Ducky Script for USB Rubber Ducky, 22 bytes

String "Hello, World!"
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 55 bytes

(New-Object -c wscript.shell).sendkeys('Hello, World!')
\$\endgroup\$
0
\$\begingroup\$

InstantEXE 3.0, 20 bytes

Keys "Hello, World!"
\$\endgroup\$
0
\$\begingroup\$

Python 3,67 bytes

from pynput.keyboard import*
s=Controller()
s.type("Hello, World!")

No module named pynput on tio

\$\endgroup\$
1
  • \$\begingroup\$ Controller().type("Hello, World!") ? \$\endgroup\$
    – Max
    Mar 9, 2022 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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