c# - Console.ReadKey Unexpected Behavior with Pasted Text - Multi-Keys Entered at Once -
call console.readkey()
, @ prompt paste (edit->paste) of clipboard text length > 1 (let's 'hello world!').
i discovered unexpectedly, on subsequent calls console.readkey(), instead presenting prompt user, no prompt given! instead, subsequent characters pasted text read ... no prompt!
bottom line: use readkey, need make sure (darn sure) when call readkey next time, gives user prompt. currently, makes readkey unusable critical scenarios this.
it not there way disable this, or clear inputted not yet read out values.
in case, presented critical errors simple console app, looping looking either words 'r' 'run', or 'e' 'end'. since near time prompt file path, easy @ point user has used paste file path @ point on accident. problem (!!!), if file path contained 'r', runs.
using system; namespace practise1 { class program { static void main() { rundialogue2(); } public static void rundialogue() { while (true) { console.writeline("type 'r' run, 'e' end"); consolekey key = console.readkey().key; console.writeline("\r\n"); switch (key) { case consolekey.r: console.writeline("run! engines blast off!"); blastoff(); break; case consolekey.e: console.writeline("finished"); return; default: console.writeline("invalid entry, try again"); // *thought* pick bad input, not pasted text! break; } } } public static void rundialogue2() { while (true) { console.writeline("type 'r' run, 'e' end"); consolekey key = console.readkey().key; console.writeline("\r\n"); switch (key) { case consolekey.r: console.writeline("run! engines blast off!"); console.writeline("we blasted off at: " + datetime.utcnow); //blastoff(); break; case consolekey.e: console.writeline("finished"); return; default: console.writeline("invalid entry, try again"); // *thought* pick bad input, not pasted text! break; } } } } }
if had 'charley's!' in clipboard , pasted it, here output. notice how can multiple runs! unintended. quite scary how bad is, mean me can never depend on readkey realiable input method, particularly when thought validating input:
type 'r' run, 'e' end c invalid entry, try again type 'r' run, 'e' end h invalid entry, try again type 'r' run, 'e' end invalid entry, try again type 'r' run, 'e' end r run! engines blast off! blasted off at: 7/21/2013 11:24:36 pm type 'r' run, 'e' end l invalid entry, try again type 'r' run, 'e' end e finished
i know solution use console.readline instead, ability interact single key press happier. there way around odd behavior?
something rid of constant reads readkey:
public static void rundialogue() { while (true) { console.writeline("type 'r' run, 'e' end"); consolekey key = console.readkey().key; console.writeline("\r\n"); switch (key) { case consolekey.r: console.writeline("run! engines blast off!"); blastoff(); break; case consolekey.e: console.writeline("finished"); return; default: console.writeline("invalid entry, try again"); // *thought* pick bad input, not pasted text! break; } console.in.readtoend(); } }
while rid of immediate problem in original post, more complete solution read , ignore rest of input stream without echoing screen, work:
public static void rundialogue() { while (true) { console.writeline("type 'r' run, 'e' end"); consolekey key = console.readkey().key; console.writeline("\r\n"); switch (key) { case consolekey.r: console.writeline("run! engines blast off!"); blastoff(); break; case consolekey.e: console.writeline("finished"); return; default: console.writeline("invalid entry, try again"); // *thought* pick bad input, not pasted text! break; } while (console.keyavailable) { console.readkey(true); } } }
Comments
Post a Comment