13
\$\begingroup\$

Thanks to MD XF's recent challenge, we can now read passwords in many different languages! The problem is, now we need to port our applications to mobile devices, and they do password entry a little differently.

Challenge

  • Read a string from the keyboard.
  • Each time a characterk is entered, display it for a short time interval.
  • After the time interval has passed OR the user has entered another character, replace k with some character c.

Rules

  • c must be constant; it must be the same character.
  • c can be any visible character (i.e. it cannot be a newline, space, tab, or unprintable).
  • c can't be based on any inputted k; c must be defined/constant before the first k is read.
  • c must be the same every time the program is run.
  • c can be one of the values given as k if by accident, as long as all other rules are followed.

  • You must print k in realtime. As soon as the user enters a new k, you must display it immediately.
  • k should be visible to the end user before being changed to c; the time interval shall not be less than 0.5 seconds.
  • k should be replaced by c within a reasonable time; the time interval shall not exceed 5 seconds.
  • As soon as a new character is entered, whether or not the time interval has expired, you should replace k with c and use the entered key as the new k immediately.

  • It is acceptable to clear and redraw the screen each time you need to change a character.
  • You may use any reasonable methods of input and output as long as all other rules are followed.
  • You may assume that the number of characters inputted is never longer than the terminal/graphical window width.
  • If using a terminal, your program should terminate after a newline is entered or EOF is reached.
  • Your program should function as outlined here on both mobile and other environments.

  • Your program may assume that the input will only contain printable characters (letters, numbers, symbols) and possibly a terminating newline (no backspace, arrow keys, delete, etc).
  • Your program may terminate when Ctrl+C is pressed.
  • You may terminate your program by closing a window, if your program launches one.

Scoring

This is , so the shortest answer in each language wins!

Example

Here is an example of what it should look like. Note this is recorded on a mobile platform, but should also work on a desktop platform.

password

\$\endgroup\$
7
  • \$\begingroup\$ For those who can see deleted posts, here is the Sandbox post \$\endgroup\$ Commented Jun 11, 2017 at 19:33
  • \$\begingroup\$ Which keyboard are you using in the video? \$\endgroup\$
    – Beta Decay
    Commented Jun 11, 2017 at 22:02
  • 1
    \$\begingroup\$ @BetaDecay the notification at the end states zenui keyboard \$\endgroup\$
    – ovs
    Commented Jun 11, 2017 at 22:12
  • \$\begingroup\$ @BetaDecay It is in fact the ZenUI keyboard. Just curious, or does it matter? \$\endgroup\$ Commented Jun 11, 2017 at 22:41
  • \$\begingroup\$ @musicman523 Just curious :) \$\endgroup\$
    – Beta Decay
    Commented Jun 12, 2017 at 6:04

4 Answers 4

4
\$\begingroup\$

HTML + JavaScript, 20+105 = 125 bytes

<input id=I oninput=v=I.value;s='*'.repeat(l=v.length-1);I.value=s+v[l];clearTimeout(I.t);I.t=setTimeout(`I.value=s+'*'`,1e3)

The delay between the entry of k and the letter becoming * is one second.

JavaScript oninput, Formatted

v=I.value;
s='*'.repeat(l=v.length-1);
I.value=s+v[l];
clearTimeout(I.t);
I.t=setTimeout(`I.value=s+'*'`,1e3)

Test Snippet

Added the ending bracket (>) for better compatibility.

<input id=I oninput=v=I.value;s='*'.repeat(l=v.length-1);I.value=s+v[l];clearTimeout(I.t);I.t=setTimeout(`I.value=s+'*'`,1e3)>

\$\endgroup\$
4
  • \$\begingroup\$ Doesn't seem to terminate though! \$\endgroup\$
    – 0xffcourse
    Commented Jun 12, 2017 at 7:07
  • 1
    \$\begingroup\$ @officialaimm The challenge states that only terminal applications need to have a way to terminate them. \$\endgroup\$ Commented Jun 12, 2017 at 9:54
  • \$\begingroup\$ Ah, silly me. Sorry. \$\endgroup\$
    – 0xffcourse
    Commented Jun 12, 2017 at 9:55
  • \$\begingroup\$ On my browser I.value => value seems to work fine \$\endgroup\$
    – l4m2
    Commented Jul 17, 2023 at 1:41
3
\$\begingroup\$

Python 3, 186 224 bytes

Works only in windows.

import os,time,msvcrt as m
i=s=x=0;t=time.clock
def v():os.system("cls")
v()
while 1:
 if m.kbhit():
  k=str(m.getch())
  if"\\r"in k:break 
  i+=1;x=1;v();print("*"*(i-1)+k[2]);s=t()
 if (t()-s>.6and x):x=0;v();print("*"*i)

Older version(186 bytes): The sleep was mandatory regardless of the speed at which key was pressed.

import os,time,msvcrt
a=k=[];i=0;o=os.system
while 1:
 o("cls")
 if i:print("*"*(i-1)+a[i-1]);time.sleep(.6);o("cls");print("*"*i)
 k=str(msvcrt.getch())
 if"\\r"in k:break
 a+=k[2];i+=1
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I don't think this quite meets all the criteria, specifically As soon as a new character is entered, whether or not the time interval has expired, you should replace k with c and use the entered key as the new k immediately. That being said, I can't test it since I don't run Windows, so let me know! You may find this related challenge helpful as a reference \$\endgroup\$ Commented Jun 13, 2017 at 0:20
  • \$\begingroup\$ I assumed the replacement from k to c was only graphical (which is fulfilled in my code). Did you also mean to change the stored value of k with c? \$\endgroup\$
    – 0xffcourse
    Commented Jun 13, 2017 at 3:50
  • 1
    \$\begingroup\$ Nope, that's all there should be. Your time delay is .6 seconds - what happens if I type in two letters within .1 seconds of each other? Will you still sleep for 1.2 seconds? Again, sorry I can't test this myself - it might be worth posting a video of you running your code! \$\endgroup\$ Commented Jun 13, 2017 at 4:02
  • \$\begingroup\$ Modified. The newer version ensures new character can be thrown even before the previous character's time has not been out. \$\endgroup\$
    – 0xffcourse
    Commented Jun 13, 2017 at 7:00
3
\$\begingroup\$

Python 2, 133 bytes

Based on @officialaimm answer.

import time,msvcrt as m
k=T=0
s=p='\r'
while'\r'!=k:
 t=time.time();print s,
 if m.kbhit():k=m.getch();s=p+k;T=t;p+='*'
 if t-T>1:s=p
\$\endgroup\$
5
  • \$\begingroup\$ Woah, 90 bytes less!!! Would vote but my daily limit has been reached. Is there a way to check it(I only have python 3 on my pc)? Btw I dont see no clear screen, how is it managed? \$\endgroup\$
    – 0xffcourse
    Commented Jun 13, 2017 at 15:26
  • \$\begingroup\$ @officialaimm by the \r in the print \$\endgroup\$ Commented Jun 13, 2017 at 15:27
  • 1
    \$\begingroup\$ I'll post a py3 variant when i get to my classroom \$\endgroup\$ Commented Jun 13, 2017 at 15:28
  • 1
    \$\begingroup\$ @officialaimm this should work, i'm on linux atm, so i can't test it \$\endgroup\$ Commented Jun 13, 2017 at 15:37
  • \$\begingroup\$ I checked it, It's working. (y) \$\endgroup\$
    – 0xffcourse
    Commented Jun 13, 2017 at 15:44
0
\$\begingroup\$

HTML + JavaScript, 115 bytes

<input id=I oninput="setTimeout(g=>g==I.value?I.value=0+g.slice(0,-1):0,1e3,I.value=I.value.replace(/.(?=.)/g,0))">

\$\endgroup\$

Your Answer

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

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