![]() |
|
char input - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: char input (/Thread-char-input) |
char input - blackeagle - 07-17-2014 is there a way to take a char input from the keyboard ? i know i can use a string input and get the first char !! but should be a way so the user can only input a char ? RE: char input - dropzon3 - 07-17-2014 Kinda, if you're operating in the terminal using STDIN then it is only actually considered input after the newline has been entered. You can read it char by char but you won't get the char until after the enter key has been hit. This is because the tty driver buffers the input until a newline. There are two ways around this, accessing the event loop and hooking the keypress(or down/up) event for the window, or in C I'd use the curses library cbreak() which disables the tty buffering. A quick google shows that Java Curses implements cbreak() so that might be of use to you. RE: char input - Ex094 - 07-17-2014 Found this http://www.inf.unibz.it/~calvanese/teaching/ip/lecture-notes/uni02/node33.html and this http://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner see if it's useful RE: char input - dropzon3 - 07-17-2014 (07-17-2014, 04:29 PM)Ex094 Wrote: Found this http://www.inf.unibz.it/~calvanese/teaching/ip/lecture-notes/uni02/node33.html and this http://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner see if it's useful That needs to wait until a newline is entered. So they need to enter multiple characters before you can read the input. RE: char input - blackeagle - 07-17-2014 (07-17-2014, 04:29 PM)Ex094 Wrote: Found this http://www.inf.unibz.it/~calvanese/teaching/ip/lecture-notes/uni02/node33.html and this http://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner see if it's useful i already saw these b4 asking my question here !! in those examples user an enter more then 1 character but the program only takes the first character and thats not what i'm asking for what i'm asking for is : is there a way to force a user using only 1 character ? example: a b c acceptable abc not acceptable hack not accetable !! @dropzon3 could u tell me more about cbreak(); and how it could do what i want !! RE: char input - dropzon3 - 07-17-2014 well cbreak would let you read the buffer as its typed so you could move on with the program after they've typed the one character without waiting on a new line. If you want to limit what they can possible type and not just make them reenter it after they've entered too many characters then you'll need to get access to the windows event loop and capture key strokes yourself and prevent the keystroke from propagating through the handlers after they've entered one character. Is there a reason you can't just let them enter multiple characters and only accept the first one? Or just loop until they enter valid information. That is a far more common technique than actually trying to block input. RE: char input - blackeagle - 07-17-2014 well then i guess the best to do is using charat(0) to take the first char !! RE: char input - Solixious - 07-19-2014 Unless you are looking for command line specific solution, you could try using a KeyListener interface and adding a KeyListener using addKeyListener(Component) method. |