Is there any equivalent to C++’s getch()
in Java? That is a function that moves the control forward as soon as a keyboard key is pressed, and also stores the character pressed.
I would like to use that function in a console app.
We use a getch() function in a C/ C++ program to hold the output screen for some time until the user passes a key from the keyboard to exit the console screen. Using getch() function, we can hide the input character provided by the users in the ATM PIN, password, etc.
getch() is a nonstandard function and is present in conio. h header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX. Like these functions, getch() also reads a single character from the keyboard.
This function does not take any parameters. Here, getch() returns the ASCII value of the character read from stdin . For example, if we give the character '0' as input, it will return the ASCII value of '0', which is 49. Now, in C / C++, we can directly convert a character to an integer.
Function getch in C program prompts a user to press a character. It doesn't show up on the screen. Its declaration is in "conio. h" header file.
There's no getch()
function equivalent in java. But since you are not the first one who is asking about it, there are some solutions available:
I think nothing has changed since then - I mean, no new getch() alike functions were added to Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With