954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Replacement for _getch();

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.

BCBTP
Light Poster
34 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

BCBTP
Light Poster
34 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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.

BCBTP
Light Poster
34 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
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 isnothing, repeat nothing like getch() that is
1) portable
2) not O/S specific
3) not compiler specific
4) standard
in C++NOTHING. Period.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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!

BCBTP
Light Poster
34 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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.

sergent
Posting Pro
598 posts since Apr 2011
Reputation Points: 70
Solved Threads: 22
 
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 evenwe don't know.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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 havemultiple 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

BCBTP
Light Poster
34 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: