how to getche() in c++?

how can i simulate the behaviour of the nonstandard getche() from conio.h in c++?

i tried converting
ch=getche();
into
cin.get(ch);
or
cin.read(&ch,1);

the problem: cin only seems to react if i press enter.
how can i have it react on every input character?

i read about
cin.sync(),
cin.sync_with_stdio(),
cin.rdbuf()->sgetc(),
cin.rdbuf()->sbumpc()
because they sounded promising. no success.


Comments

  • cmon, help me.
  • You can't.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • : how can i simulate the behaviour of the nonstandard getche() from conio.h in c++?
    :
    : i tried converting
    : ch=getche();
    : into
    : cin.get(ch);
    : or
    : cin.read(&ch,1);
    :
    : the problem: cin only seems to react if i press enter.
    : how can i have it react on every input character?
    :
    : i read about
    : cin.sync(),
    : cin.sync_with_stdio(),
    : cin.rdbuf()->sgetc(),
    : cin.rdbuf()->sbumpc()
    : because they sounded promising. no success.
    :
    :
    :

    I have solution but it uses dos interapts and it can't work in BC++ ver 5.5 but it works in TC ver 3.0 or you can code it in assembler and link it with c++ program if you want to use in 32 bit version.

    Here is code:
    #include
    #include
    #include

    int scan_code(){
    union REGS r;
    if (!kbhit()) return 0;
    r.h.ah=0x10;
    int86(0x16,&r,&r);
    if ((r.x.ax & 0xff) == 0xe0 || (r.x.ax & 0xff) == 0xf0) r.x.ax &= 0xff00;
    return (r.x.ax & 0x00ff) ? r.x.ax & 0xff : (0x0100 | (r.x.ax >> 8));
    }

    void main(){
    while (!kbhit());
    cout<<"Code: "<<scan_code()<<"
    ";
    }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories