Convert text to key presses

10

1

I am a robot. I bought this keyboard because of its easy rectangular layout:

~`   !1   @2   #3   $4   %5   ^6   &7   *8   (9   )0   _-   +=
tab  Qq   Ww   Ee   Rr   Tt   Yy   Uu   Ii   Oo   Pp   {[   }]    \|
     Aa   Ss   Dd   Ff   Gg   Hh   Jj   Kk   Ll   :;   "'   [-enter-]
          Zz   Xx   Cc   Vv   Bb   Nn   Mm   <,   >.   ?/
                         [========= space =========]

To print human text, I need to convert it to commands that my manipulators can interpret. My left manipulator hovers over the Shift key. My right manipulator, at the beginning, hovers over the ~ key. The commands that my manipulators understand are:

S      : press the shift key
s      : release the shift key
L      : move the right manipulator left by 1
R      : move the right manipulator right by 1
U      : move the right manipulator up by 1
D      : move the right manipulator down by 1
P      : press the key under the right manipulator
p      : release the key by the right manipulator

Write code to convert any ASCII message to a list of commands. The input can contain any number of the 95 printable ASCII characters; possibly also TAB and newline characters. The output should be the list of the commands to the manipulators.

So, for example, to type Hello World!, the commands are

SRRRRRRDDPp
sLLLUPp
RRRRRRDPp
Pp
UPp
LLLLDDDPp
SLLLUUUPp
sRRRRRRRPp
LLLLLPp
RRRRRDPp
LLLLLLPp
SLLUUPp

I reset the manipulators to initial state before printing each message.

There are some mechanical hazards that should be avoided by proper programming:

  1. No moving (LRUD) allowed when printing (P) is engaged
  2. No jamming of manipulators: when a manipulator is engaged (S or P), the next command for this manipulator should be disengaging (s or p), and vice-versa
  3. No unnecessary shifting: between each two shift (s, S) commands, there should be a P command

    So, to print ~~, commands SPpPp are valid, while SPpsSPp are not

  4. No moving out of bounds: no movement command should try to move the right manipulator more than 13 spaces to the right or 4 to the bottom of the initial position (or any spot to the top or left)

Additional notes:

  • Pressing a disabled key (command sequence like DDPp) results in no keys pressed and is allowed.
  • Pressing Shift+Tab has no effect, but Shift+Space and Shift+Enter have the same effect as without Shift.
  • Pressing on any spot on the space bar and the Enter key has the same effect.
  • Whitespace keys in output have no meaning, but can be used to format it in a beautiful way.

anatolyg

Posted 2018-05-21T17:51:47.767

Reputation: 10 719

Is speed a problem? Could we return the manipulators to their home position between each character (so long as it doesn't include unnecessary shifting, of course)? – Engineer Toast – 2018-05-21T18:05:26.837

No problem. Maybe it could be more interesting without extra movement, but I don't like requiring the best possible output. – anatolyg – 2018-05-21T18:10:27.583

1Related one, related two. – AdmBorkBork – 2018-05-21T18:11:27.010

2You haven't actually defined the task... What character can input contain? What is the actual task (I'm going to guess it's the obvious based on the title, but you should specify nonetheless) – HyperNeutrino – 2018-05-21T18:19:47.160

It seems a person typing on a On Screen Keyboard of a Smart TV using its remote control! I feel I do the same when it is me! Was it your inspiration? – sergiol – 2018-05-21T23:11:49.823

Actually, my inspiration was trying to type while all my fingers except two are smeared with mud. But a Smart TV seems more mainstream! – anatolyg – 2018-05-21T23:33:30.540

Related with a simpler output but more strict constraints. – Arnauld – 2018-05-22T00:18:08.590

3Why bother with Pp? As far as I can see those are always a single action and neither P or p can appear on its own. – orlp – 2018-05-22T05:31:24.960

Pp and Ss look more robotic. I imagine a simple controller inserting delays between commands and connecting commands to electrical motors after simple parsing. – anatolyg – 2018-05-22T07:40:27.757

Answers

5

Python 2, 338 337 335 331 325 bytes

x=y=s=0
for c in input():p='`1234567890-=`	qwertyuiop[]|`asdfghjkl;\'\n```zxcvbnm,./``````` ~!@#$%^&*()_+~~QWERTYUIOP{}\\~ASDFGHJKL:"\n~~~ZXCVBNM<>?~~~~~~~ '.find(c);S=[p>61,s][c in' \n'];p%=62;Y=p/14;X=[max(x,12),min(max(x,5),10),p%14]['\n '.find(c)];print'sS'[S]*(s^S)+'LR'[X>x]*abs(X-x)+'UD'[Y>y]*abs(Y-y)+'Pp';x,y,s=X,Y,S

Try it online!


Moves directly from each character to the next.

Explanation:

  • S=[c in K,s][c in' \n'], checks if the next character should be Upper- or lowercase. If c is a space or newline, the case remains the same.

  • X=[max(x,12),min(max(x,5),10),p%15]['\n '.find(c)]. If c is a space or newline, the closest x-coordinate to the current is chosen(as the keys span multiple columns)

  • print'sS'[S]*(s!=S)+'LR'[X>x]*abs(X-x)+'UD'[Y>y]*abs(Y-y)+'Pp', prints the case switch, the number of x-coordinate moves, the number of y-coordinate moves, and finally Pp, for each character


Shorter version, if the shortest path is not required:

Python 2, 294 293 291 287 281 bytes

x=y=s=0
for c in input():p='`1234567890-=`	qwertyuiop[]|`asdfghjkl;\'\n```zxcvbnm,./``````` ~!@#$%^&*()_+~~QWERTYUIOP{}\\~ASDFGHJKL:"\n~~~ZXCVBNM<>?~~~~~~~ '.find(c);S=[p>61,s][c in' \n'];p%=62;X,Y=p%14,p/14;print'sS'[S]*(s^S)+'LR'[X>x]*abs(X-x)+'UD'[Y>y]*abs(Y-y)+'Pp';x,y,s=X,Y,S

Try it online!

TFeld

Posted 2018-05-21T17:51:47.767

Reputation: 19 246

Is it really required to use the shortest path to space / enter? – Arnauld – 2018-05-22T10:40:20.353

@Arnauld, I guest not, it wasn't specified but the example goes to the nearest space (after o) – TFeld – 2018-05-22T11:02:02.480

2

JavaScript (ES6), 263 bytes

Takes input as an array of characters.

s=>s.map(c=>(y=i-(i=(j=`\`~1!2@3#4$5%6^7&8*9(0)-_=+00\t0qQwWeErRtTyYuUiIoOpP[{]}|\\00aAsSdDfFgGhHjJkKlL;:'"
${1e6}zZxXcCvVbBnNmM,<.>/?${1e13} `.indexOf(c))>>1),g=k=>'LRUD'[n=k?y/14:y%14,k^=n<0].repeat(n<0?-n:n))()+g(2)+['sS'[j-s&c!=' '&c!=`
`?s^=1:2]]+'Pp',i=s=0)

Try it online!

Arnauld

Posted 2018-05-21T17:51:47.767

Reputation: 111 334

1

.COM opcode, 108 104 bytes

0000h: B4 00 CD 16 BE 50 01 83 C6 03 3A 24 77 F9 0F B6
0010h: DC 03 5C 01 B4 02 CD 16 B4 02 68 00 01 A8 03 B2
0020h: 53 74 08 81 36 20 01 20 01 CD 21 84 DB 74 0B 4B
0030h: B2 52 E8 F4 FF B2 4C CD 21 C3 84 FF 74 0C FE CF
0040h: B2 44 E8 E4 FF B2 55 CD 21 C3 B2 50 CD 21 B2 70
0050h: CD 21 C3 0D FE 00 1B F1 00 1C F0 01 28 E3 01 29
0060h: D7 FF 35 D6 02 39 CC 03                        

Take input from keyboard with CapsLock off

Badly golfed though

        org 100h
        mov ah, 0
        int 16H
        mov si, table-3
tabing: add si, 3
        cmp ah, [si]
        ja tabing
        movzx bx, ah
        add bx, [si+1]
        mov ah, 2
        int 16H
        mov ah, 2
        push 100H
        test al, 3
        mov dl, 'S'
cmd:    jz fun
        xor [cmd-1], word 0x120
        int 21H
fun:    test bl, bl
        jz bl0
        dec bx
        mov dl, 'R'
        int 21H
        call fun
        mov dl, 'L'
        int 21H
        ret
bl0:    test bh, bh
        jz bh0
        dec bh
        mov dl, 'D'
        int 21H
        call fun
        mov dl, 'U'
        int 21H
        ret
bh0:    mov dl, 'P'
        int 21H
        mov dl, 'p'
        int 21H
        ret
macro key begin, end, U, L {
        db end
        dw U*256+L-begin
}
table:
        key 0x02, 0x0D, 1, 0
        key 0x10, 0x1B, 1, 1
        key 0x1C, 0x1C, 2, 12
        key 0x1E, 0x28, 2, 1
        key 0x29, 0x29, 0, 0
        key 0x2C, 0x35, 3, 2
        key 0x39, 0x39, 4, 5

l4m2

Posted 2018-05-21T17:51:47.767

Reputation: 5 985

A great idea to do that without a LUT! – anatolyg – 2018-05-22T15:02:24.650

1Take input from keyboard

How is our robot friend, who is asking for help using his keyboard, to make use of this program? – Shaun H – 2018-05-22T19:04:38.953