I am looking for a replacement for _getch();
Yes I have seen other threads like this, but they do not really satisfy my need(And I do not want to waste my time trying to write one in Assembler, as it isn't my strongpoint). I need something that is exactly like _getch() in that you do not have to press enter. I would probably use it for passwords, and menu's, so it would need to be able to work like _getch(mycharhere); works.

I have tried WM_KEYDOWN but I do not really know how to use it properly.

Yes I have seen other threads like this, but they do not really satisfy my need

Then please list your requirements. The threads you speak of usually result in something like this (using Windows since you mentioned WM_KEYDOWN):

char& raw_input(char& ch)
{
    HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
    DWORD old_mode, n;
    
    // Switch to raw input mode
    GetConsoleMode(h, &old_mode);
    SetConsoleMode(h, ENABLE_PROCESSED_INPUT);
    
    if (WaitForSingleObject(h, INFINITE) == WAIT_OBJECT_0)
        ReadConsole(h, &ch, 1, &n, 0);
    
    // Switch back to the original input mode
    SetConsoleMode(h, old_mode);
    
    return ch;
}

This is a facsimile of getch(). If that doesn't satisfy your needs then neither does getch(), in which case you need to specify what you needs are.

I would use that as that is pretty much what I need, but windows.h is not cross platform. I need something that is the EXACT same as _getch(); but doable on all Operating Systems and all Compilers.

int main(){
     char menuItemChoice;
     MiracleFunction(&menuItemChoice);
     switch (menuItemChoice){
           case 'x':
                cout << "X\n";
                break;
           default:
                cout << "default\n";
                break;
     }
     cout << "That is what I need";
}

Your function would be fine, but not exactly cross platform.

I need something that is the EXACT same as _getch(); but doable on all Operating Systems and all Compilers.

What part of non-portable is difficult to understand?

What part of non-portable is difficult to understand?

I don't understand what your talking about.
I need something like _getch(); using just the standard libraries(or less). And no Operating System specific libraries/headers.

What part of non-portable is difficult to understand?

I don't understand what your talking about.
I need something like _getch(); using just the standard libraries(or less). And no Operating System specific libraries/headers.

Clear and succinctly:

There is nothing, repeat nothing like getch() that is
1) portable
2) not O/S specific
3) not compiler specific
4) standard
in C++ NOTHING. Period.

Comments
.

I need something like _getch(); using just the standard libraries(or less). And no Operating System specific libraries/headers.

Well, that simplifies things greatly. What you want is impossible. Have a nice day.

How is it impossible if the compiler has it. It is obvious that it can be written in Assembly. Well in that case I am in the wrong forum. Thanks!

Edited 5 Years Ago by BCBTP: n/a

If the compiler has it, then why are you asking it on this forum? getch() is not a standard function, therefore compiler does not have to implement it. In your case all the forums are going to be the "wrong forums". If you don't like C++ ANSI/ISO standards, create your own standardization committee, and make compilers listen to you, but there is no cause to express your anger on us.

The only way you can make it cross platform is by putting system specific code into #defines.

How is it impossible if the compiler has it. It is obvious that it can be written in Assembly. Well in that case I am in the wrong forum. Thanks!

If other forums explain how this can be done, please come back and explain it to us and we will apologize profusely. We will also learn something even we don't know.

Comments
I guesss he does'nt get it.

How is it impossible if the compiler has it.

The compiler is closely tied to its target system, it doesn't have to care about the implementation of getch() being portable. For example, you'll find that Visual Studio's implementation of getch() is very similar to the one I posted above because it's a Windows-targeted compiler.

Compilers that target multiple systems will have multiple builds where the non-portable parts are rewritten for each target system.

What you're asking for is impossible because when it comes to operating system dependencies there's no single implementation that will work everywhere, and getch() has an operating system dependency of direct access to console input rather than the cooked input coming in from the standard input stream.

... I was not trying to be rude... and I was not angry...

This article has been dead for over six months. Start a new discussion instead.