This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

_getkey() + putchar() vs. getchar()

Hello,

I've written some code for an educational project which tries to capture input from the user, I've noticed that when using the library function _getkey() alone it turns each typed Enter or Intro pressed key into CR ('\r', ASCII encoding = 0x0D), but when using the function getchar(), which is supposed to use _getkey() internally, it turns each typed Enter or Intro pressed key into LF ('\n', ASCII encoding = 0x0A), following I show two small snippets I've used in order to verify this behavior:

1.- Using _getkey() and putchar() to get the job done:

#include <reg515c.h>
#include <stdio.h>

void test_getkey (void) {
  char c;

  while ((c = _getkey ()) != 0x1B) {
    putchar(c);
    printf ("\ncharacter = %bu %bx\n", c, c, c);
  }
}

void main(void)
{
        SCON = 0x52;
        BD = 1;
        /* 9600 Bd @ 10 MHz => SMOD = 1, SREL = 0x3BF */
        PCON |= 0x80;
        SRELL = 0xBF;

        while(1)
                test_getkey();
}

Output when pressing the Enter or Intro key:

character = 13 d

1.- Using getchar() to get the job done:

#include <reg515c.h>
#include <stdio.h>

void test_getchar(void) {
  char c;

  while ((c = getchar ()) != 0x1B) {
        putchar(c);
    printf ("\ncharacter = %bu %bx\n", c, c, c);
  }
}

void main(void)
{
        SCON = 0x52;
        BD = 1;
        /* 9600 Bd @ 10 MHz => SMOD = 1, SREL = 0x3BF */
        PCON |= 0x80;
        SRELL = 0xBF;

        while(1)
                test_getchar();
}

Output when pressing the Enter or Intro key:

character = 10 a

In the following Support Knowledge Base article:

http://www.keil.com/support/docs/1791.htm

it is stated that by defaul getchar() is implemented as follows:

char getchar (void)
{
char c;

c = _getkey ();
putchar (c);
return (c);
}

So I don't understand why the characters read vary when pressing the Enter/Intro key, could somebody please shed some light on this issue?.

Thanks in advance,
Juan