Revision b3f2c3ea-fd0b-4102-8c37-0bc19aec2db6 - Code Golf Stack Exchange
#Java, <s>404</s> 388 bytes
<!-- language-all: lang-java -->
import java.awt.*;import java.awt.event.*;class M extends Frame{{TextField f=new TextField();add(f);f.addKeyListener(new KeyAdapter(){long t,i=96;public void keyPressed(KeyEvent e){t=t>0?t:System.nanoTime();char c=e.getKeyChar();System.out.print(c+(c==++i?i>121?"\n"+(System.nanoTime()-t)/1e6:"":"\nFail"));if(c!=i|i>122)dispose();}});show();}public static void main(String[]a){new M();}}
And here I thought Java Console was already verbose..
Since Java has no way to raw listen for key-presses in the Console a.f.a.i.k., I use a GUI with `java.awt`.
-16 bytes thanks to *@OlivierGrégoire*.
**Explanation:**
import java.awt.*; // Required import for Frame and TextField
import java.awt.event.*; // Required import for KeyAdapter and KeyEvent
class M extends Frame{ // Class (extending Frame)
{ // Class-initialization
TextField f=new TextField() // Instantiate an input-field
add(f); // Add the input-field to the Frame
f.addKeyListener(new KeyAdapter(){
// Add a KeyAdapter to the input-field
long t, // Long to save the time
i=96; // Previous character, starting at 96 ('a'-1)
public void keyPressed(KeyEvent e){
// Override the keyPressed-method:
t=t>0? // If `t` is already set:
t // Leave it the same
: // Else:
System.nanoTime(); // Save the current time in ns (== start the timer)
char c=e.getKeyChar(); // Get the character pressed by the user
System.out.print(c // Print this character
+(c==++i? // +If it's the correct next character in the alphabet:
i>121? // and we're now at 'z'
"\n" // Print a new-line
+(System.nanoTime()-t)/1e6
// + the end-time in ms
:"": // Else (an incorrect character was pressed)
"\nFail")); // Print a new-line + literal "Fail"
if(c!=i // As soon as an incorrect character is pressed,
|i>122) // or we've reached 'z':
dispose(); // Exit the application
} // End of keyPressed-method
}); // End of KeyAdapter
show(); // Initially show the Frame
} // End of class-initialization
public static void main(String[]a){
// Mandatory main-method
M m=new M(); // Create an instance of itself (of the Frame)
} // End of main-method
} // End of class
**Example gif of success:**
[![enter image description here][1]][1]
**Example gif of fail:**
[![enter image description here][2]][2]
[1]: https://i.sstatic.net/qYPs4.gif
[2]: https://i.sstatic.net/pgIAE.gif