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 character
kis entered, display it for a short time interval. - After the time interval has passed OR the user has entered another character, replace
kwith some characterc.
Rules
cmust be constant; it must be the same character.ccan be any visible character (i.e. it cannot be a newline, space, tab, or unprintable).ccan't be based on any inputtedk;cmust be defined/constant before the firstkis read.cmust be the same every time the program is run.ccan be one of the values given askif by accident, as long as all other rules are followed.- You must print
kin realtime. As soon as the user enters a newk, you must display it immediately. kshould be visible to the end user before being changed toc; the time interval shall not be less than 0.5 seconds.kshould be replaced bycwithin 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
kwithcand use the entered key as the newkimmediately. - 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 code-golf, 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.
-
\$\begingroup\$ For those who can see deleted posts, here is the Sandbox post \$\endgroup\$musicman523– musicman5232017年06月11日 19:33:07 +00:00Commented Jun 11, 2017 at 19:33
-
\$\begingroup\$ Which keyboard are you using in the video? \$\endgroup\$Beta Decay– Beta Decay2017年06月11日 22:02:35 +00:00Commented Jun 11, 2017 at 22:02
-
1\$\begingroup\$ @BetaDecay the notification at the end states zenui keyboard \$\endgroup\$ovs– ovs2017年06月11日 22:12:45 +00:00Commented Jun 11, 2017 at 22:12
-
\$\begingroup\$ @BetaDecay It is in fact the ZenUI keyboard. Just curious, or does it matter? \$\endgroup\$musicman523– musicman5232017年06月11日 22:41:57 +00:00Commented Jun 11, 2017 at 22:41
-
\$\begingroup\$ @musicman523 Just curious :) \$\endgroup\$Beta Decay– Beta Decay2017年06月12日 06:04:11 +00:00Commented Jun 12, 2017 at 6:04
4 Answers 4
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)>
-
\$\begingroup\$ Doesn't seem to terminate though! \$\endgroup\$0xffcourse– 0xffcourse2017年06月12日 07:07:33 +00:00Commented 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\$Justin Mariner– Justin Mariner2017年06月12日 09:54:03 +00:00Commented Jun 12, 2017 at 9:54
-
\$\begingroup\$ Ah, silly me. Sorry. \$\endgroup\$0xffcourse– 0xffcourse2017年06月12日 09:55:15 +00:00Commented Jun 12, 2017 at 9:55
-
\$\begingroup\$ On my browser
I.value=>valueseems to work fine \$\endgroup\$l4m2– l4m22023年07月17日 01:41:32 +00:00Commented Jul 17, 2023 at 1:41
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
-
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\$musicman523– musicman5232017年06月13日 00:20:58 +00:00Commented 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– 0xffcourse2017年06月13日 03:50:11 +00:00Commented 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\$musicman523– musicman5232017年06月13日 04:02:12 +00:00Commented 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– 0xffcourse2017年06月13日 07:00:47 +00:00Commented Jun 13, 2017 at 7:00
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
-
\$\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– 0xffcourse2017年06月13日 15:26:34 +00:00Commented Jun 13, 2017 at 15:26
-
\$\begingroup\$ @officialaimm by the
\rin the print \$\endgroup\$Felipe Nardi Batista– Felipe Nardi Batista2017年06月13日 15:27:34 +00:00Commented Jun 13, 2017 at 15:27 -
1\$\begingroup\$ I'll post a py3 variant when i get to my classroom \$\endgroup\$Felipe Nardi Batista– Felipe Nardi Batista2017年06月13日 15:28:22 +00:00Commented Jun 13, 2017 at 15:28
-
1\$\begingroup\$ @officialaimm this should work, i'm on linux atm, so i can't test it \$\endgroup\$Felipe Nardi Batista– Felipe Nardi Batista2017年06月13日 15:37:47 +00:00Commented Jun 13, 2017 at 15:37
-
\$\begingroup\$ I checked it, It's working. (y) \$\endgroup\$0xffcourse– 0xffcourse2017年06月13日 15:44:36 +00:00Commented Jun 13, 2017 at 15:44
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))">