Type "Hello, World!"

11

2

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.

cardboard_box

Posted 2013-02-23T01:45:35.720

Reputation: 5 150

Answers

9

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++);}

saeedn

Posted 2013-02-23T01:45:35.720

Reputation: 1 241

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 – 2013-02-25T14:44:32.150

@ugoren Thanks, I'll try them :) – saeedn – 2013-02-26T00:44:57.600

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 – 2013-03-11T12:56:33.633

@11684 The author of the shortest answer asked me not to count it, so I accepted the shortest non-trivial answer instead. – cardboard_box – 2013-03-11T23:44:23.620

No need to call tty it only works if stdin is a tty, in which case, 0 is already an open terminal fd. – Dave – 2013-12-09T08:38:20.713

18

AppleScript, 50 bytes

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

Thanks to marinus for the suggestion.

grc

Posted 2013-02-23T01:45:35.720

Reputation: 18 565

3Huh. Looks like you chose the right language for the right job. – cardboard_box – 2013-02-23T02:18:53.583

You can shorten it to tell app"System Events"to keystroke"Hello, World!". – marinus – 2013-02-25T05:01:02.413

Damnit, I thought of this independently... +1! – 11684 – 2013-03-11T12:54:37.613

13

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

beary605

Posted 2013-02-23T01:45:35.720

Reputation: 3 904

3

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!

cardboard_box

Posted 2013-02-23T01:45:35.720

Reputation: 5 150

3

Emacs Lisp, 34 bytes

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

Jordon Biondo

Posted 2013-02-23T01:45:35.720

Reputation: 1 030

1

Ducky Script for USB Rubber Ducky, 22 bytes

String "Hello, World!"

Slayter

Posted 2013-02-23T01:45:35.720

Reputation: 111

1

AutoIt3, 21 bytes

Send("Hello, World!")

jdstankosky

Posted 2013-02-23T01:45:35.720

Reputation: 1 474

missing comma! It's actually 21 chars – Doorknob – 2013-03-08T21:51:10.543

Where is a comma supposed to be? Edit, never mind, lol. – jdstankosky – 2013-03-11T20:18:44.607

1

Tcl, 46 bytes

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

Expect, 29 bytes

spawn bash;send Hello,\ World

Johannes Kuhn

Posted 2013-02-23T01:45:35.720

Reputation: 7 122

Tried to do the Tk version, but failed: file with some stuff https://pastebin.ca/3897417

– sergiol – 2017-10-28T23:08:59.760

1

VBScript, 54 bytes

createobject("wscript.shell").sendkeys "Hello, World!"

afuous

Posted 2013-02-23T01:45:35.720

Reputation: 429

0

InstantEXE 3.0, 20 bytes

Keys "Hello, World!"

Timtech

Posted 2013-02-23T01:45:35.720

Reputation: 12 038

0

Python 3,67 bytes

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

No module named pynput on tio

Vedant Kandoi

Posted 2013-02-23T01:45:35.720

Reputation: 1 955

0

PowerShell, 55 bytes

(New-Object -c wscript.shell).sendkeys('Hello, World!')

Rynant

Posted 2013-02-23T01:45:35.720

Reputation: 2 353