Simulate my keyboard

I have an issue with my keyboard (more likely an issue with my browser). Some times when I click on a new text field and begin typing all my text comes out backwards. After a very short amount of time (for the purpose of the challenge we will say it is 10 milliseconds) it resumes typing forwards again. It seems that at first my cursor does not move when I begin to type so all the letters get inserted at the beginning of the textfield.

For example if I were typing python and I typed pyt in the first 10 milliseconds the result would be

hontyp


Your job is to simulate this behavior in as few bytes as possible.

Input

You may take input in any reasonable method. You may not however take the input as two separate fields. The 10 millisecond delay should start when the program begins to allow input. That is you may have preprocessing to do before input is allowed, which should not be counted towards the 10 milliseconds.

Some leniency is permitted in the timing as long as it averages 10 milliseconds and is off by more than 1 milliseconds no more than 1% of the time on a reasonably speced computer.

Output

You may output the string in whatever method you see fit. You may either update live as the user is typing or output the result after you are finished taking input (either by a newline or end of file ^D).

Test cases

Some of these were generated by this CMC in The Nineteenth Byte

First 10  | after   | result
bra         in-flak   in-flakarb
ap          ple       plepa
code-       golf      golf-edoc
no          ne        neon
we          st        stew
ai          med       media
draw        back      backward

• @Luke 1) Yes. 2) If that is the only way they can take input then they cannot compete. – Wheat Wizard Apr 16 '17 at 16:03
• ^ That probably rules out a lot of languages – Luis Mendo Apr 16 '17 at 16:12
• It is unfortunate. I don't want this challenge to rule our languages, however I really don't want it to become a trivial string manipulation challenge. – Wheat Wizard Apr 16 '17 at 16:23
• 10 milliseconds is a very short time. Did you really mean that? – Neil Apr 16 '17 at 21:02
• @Neil You are probably right, but it is what it is, I'm not in a position to change it at this point. – Wheat Wizard Apr 16 '17 at 21:19

VimScript, 48 47 bytes

Vim has some nice builtins for this. Requires Vim 8.

se ri|cal timer_start(10,{->execute("se ri&")})


Expects text entered in insert mode, a very reasonable input for VimScript. Explanation:

se ri " short for 'set revins', or 'reverse insert'
cal timer_start(10, " Start a timer that fires after 10ms
{->execute("se ri&")}) " Give it a lambda that unsets ri


My initial approach, se ri|sl10m|se ri&, doesn't work because the text is buffered until after the sleep command, and I couldn't find a way around this.

HTML (JS), 96 bytes

<input id=i oninput=i.d=i.d||Date.now()+1e3;Date.now()<i.d&&(i.selectionEnd=i.selectionStart=0)>

Starts timing for 1s from the first keypress to give you a chance to see it in action.

JavaScript (ES6), 88 bytes

p=s='',t=+new Date,document.onkeyup=k=>console.log(+new Date-t>9?(s+=k.key)+p:p=k.key+p)


Demo

The way the snippet is working, you'll need to click inside its frame so that it gets focus after clicking on the Run button and before you can start typing.

There's no way to do that in less that 10ms. So, I've set the delay to 1 second in this demo -- and even so, you'd better hurry.

p=s='',t=+new Date,document.onkeyup=k=>console.log(+new Date-t>999?(s+=k.key)+p:p=k.key+p)

Processing, 167165 146 bytes

String a="",b="";long m=0;void setup(){size(9,9);}void draw(){m=m<1?millis():m;println(b+a);}void keyTyped(){if(millis()-m>9)b+=key;else a=key+a;}


Takes input as keystrokes and outputs to the console. It updates every frame.

Explanation

String a="",b="";                // a contains the reversed (in the 10 milliseconds) string, b contains the text that is typed after the 10 milliseconds.
long m=0;                        // Store the start milliseconds in variable m
void setup() {
size(9,9);                     // set the dimensions of the window
}

void draw() {                    // loop forever
m=m<1?millis():m;              // update m
println(b+a);                  // print the string b+a
}

void keyTyped() {                // this function gets called whenever a key is typed
if(millis() - m) >9)           // if over 9 milliseconds have elapsed then
b+=key;                      //   add the key to the end of b
else                           // else (if less than 10 milliseconds have elapsed)
a=key+a;                     //   prepend the key to a
}

• @WheatWizard Say the input was asdf + ghjk. If everything was in one variable, then after the 10 milliseconds, we would have fdsa in that one string. After that, when g is typed, the string will prepend the g and become gfdsa. Then when the h is typed, I cannot prepend h to the variable (because then the input is still reversed), I will need to use substrings and have to calculate the length of that String variable. This will cost a lot of bytes. – user41805 Apr 16 '17 at 17:11
• use ternary to reduce bytes – Mr. Alien Apr 16 '17 at 17:13
• @Mr.Alien In order to use a ternary, I would need to deal with the same variable, but here, I am use two different variables (namely a and b). Even if I assign the result to a temp variable, the resulting bytecount is still larger. – user41805 Apr 16 '17 at 17:18
• My bad, I thought it's a JavaScript answer x) .. realized when I saw type casting in your answer... – Mr. Alien Apr 16 '17 at 19:53

C - 169 bytes

Well C is not the best for this as it has no platform-independent way of doing this. Works under MSVC.

#include<time.h>
#include<conio.h>
l,k,b[9];main(a){for(a=clock();clock()-a<100;(k=_getch())?b[l++]=k:0);for(;(k=_getch())-13;k?putchar(k):0);for(;l;putchar(b[--l]));}


Bit of a hacky approach, if you can type more than 9 characters in 10 ms, this will crash. Updates after a newline character.

• I'm not sure if I can mash 9 random characters in 10ms... – bendl Jul 5 '17 at 14:47

Python 2, 112 106

import msvcrt as m,time
a=b=''
T=time.time
t=T()
while 1:c=m.getch();exec'ba=+c=+cb'[T()-t>1::2];print a+b


If you want the output to be flush, without flooding with lines, use print'\r'+a+b, instead

You'll need to kill the process to stop it