Hi again,

since my last thread I proceeded a bit and in the meanwhile took a closer look on Mode13h. I started programming some simple games like Snake etc, but I'm stuck on a specific function I couldn't discover yet:

I need a getch(); which aint actually waiting for the input itself or in other words, a function to simply return the value of the last pressed key.

Thanks in advance, ~

Unfortunately I aint able to edit this post since I posted when I was on another comp, so I'll ask again a bit more precisely:

I need a function that returns the int value of the last pressed key, no matter when it was pressed.

And to add another question: How can I make use of keys like the arrows keys or the f-keys under Dos, since when I try to print them I either get

0 xy

using cout or

(null)
xy

using printf, where xy is a decimal 9<xy<100, which -leaving out the 0/(null)- already is assigned to yet another key.

>I need a getch(); which aint actually waiting for the input itself or in other
>words, a function to simply return the value of the last pressed key.
Try using a combination of kbhit and getch. I'll assume that you have those on your implementation because you mentioned them.

>How can I make use of keys like the arrows keys or the f-keys under Dos
Searching is easy and lucrative, you should try it sometime.

Arite, should bring me on...
I didn't use kbhit yet though, since as far as I had been proceeding getch worked quite well.

Thank you.

And concerning searching... So far I've been searching -and finding- for like everything I needed to know here, brought me ahead amazingly. Just added that question there 'cause I had to do a new post in here anyway.


//Actually wanted to add another question here, but I think I just figured out a solution myself... O.o

/edit:
Hmm...Been reading the syntax and definition things 'bout kbhit in bc++5.0 help and trying some different combination things, but didn't get it running since it either cannot be compiled (tried stuff like... kbhit(getch()) and other way round Oo) or simply wont work...

I'm still very inexperienced in C++ (Started like 4 weeks ago and taught myself most of the stuff on my own, since in school they're currently starting with simple if/then/else stuff), so -since I got no clue how to combine those two functions- would greatly appreciate if you could post a sample of those in combination.

Yet again - Thanks in advance... ^^

Arg.. hit the wrong button -.-
No way to delete my own posts in here?

kbhit typically takes no arguments and returns either 0 if there are no keyboard events in the queue and non-zero if there are. You might use it like this:

#include <stdio.h>
#include <conio.h>

int main ( void )
{
  int last_key;

  while ( !kbhit() )
    ; /* Do nothing */

  last_key = getch();
  cprintf ( "Last key pressed: %c\n", last_key );

  return 0;
}

Ah..I see, thought kinda wrong then, hehe.
I think I know how to do it now, thanks.

This might help you with the arrow keys:

// trap the arrow keys
// note: Pelles C uses _getch() rather than getch()

#include <stdio.h> 
#include <conio.h> 

static int get_code ( void );

// System dependent key codes
enum
{
  KEY_ESC     = 27,
  ARROW_UP    = 256 + 72,
  ARROW_DOWN  = 256 + 80,
  ARROW_LEFT  = 256 + 75,
  ARROW_RIGHT = 256 + 77
};

int main ( void )
{
  int ch;

  // escape key gets you out of the loop
  while ( ( ch = get_code() ) != KEY_ESC ) {
    switch ( ch ) {
    case ARROW_UP:
      printf ( "UP\n" );  // or do something else ...
      break;
    case ARROW_DOWN:
      printf ( "DOWN\n" );
      break;
    case ARROW_LEFT:
      printf ( "LEFT\n" );
      break;
    case ARROW_RIGHT:
      printf ( "RIGHT\n" );
      break;
    }
  }
  getchar();   // wait
  return 0;
}

static int get_code ( void )
{
  int ch = _getch();

  if ( ch == 0 || ch == 224 )
    ch = 256 + _getch();

  return ch;
}

You can add the function keys to this:

enum { F1 = 315, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11 = 389, F12 };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.