text editor in c++.

Hi. I want to make a text editor in c++. I want to take input from the user char by char and then if I detect an input that is meant to alter, the input should be altered:

for the following input a, B, <Backspace>, b, c, d, <enter>, e,f,g,<left arrow>,<Backspace> the output should be:
abcd
ef

Problem is that when i press <left arrow> it delivers two bytes α(224) and K(75).
And I'm not able to differentiate this from the situation in which the user actually wants to print these two chars.

Any suggestions?
the first byte means it's a special non-showable character, and the other one shows which arrow.
maybe you can do a condition statement if you press left arrow or right arrow.. etc..
On WindowsTM keyboards (which practically all PCs have), the character 0xE0 (or 224) is a special prefix meaning that the next character is an "extended code", such as an arrow key. Older keyboards reported zero for this, so my code tends to test for both 0 and E0.

The typical control construct is something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  ...
  int c = whatever_you_use_to_get_a_single_char_code();
  switch (c)
    {
    case 0: case 0xE0:
      switch (c = whatever_you_use_to_get_a_single_char_code())
        {
        case 71: home_pressed(); break;
        case 72: up_arrow_pressed(); break;
        case 73: page_up_pressed(); break;
        case 75: left_arrow_pressed(); break;
        ...
        }
      break;

    case 13: enter_pressed(); break;
    case 27: esc_pressed(); break;
    ...
    }
  ...

Hope this helps.
Last edited on
Hi Duoas. The function i use to get a single char is:

1
2
3
4
5
6
7
8
9
int a=kbhit();
while (!a)
{
  a=kbhit();
}
cout<<a;
 cout<<"recvd"<<getch()+0;//first byte
 if (kbhit())// what if user presses a key here
cout<<getch();//second byte 


I worried about if user press char 224 and at the second 'if' presses char 75

my program wud interpret this as a left arrow.
Last edited on
Take a deep breath and pay attention.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  ...
  int c = getch();
  switch (c)
    {
    case 0: case 0xE0:
      switch (c = getch())
        {
        case 71: home_pressed(); break;
        case 72: up_arrow_pressed(); break;
        case 73: page_up_pressed(); break;
        case 75: left_arrow_pressed(); break;
        ...
        }
      break;

    case 13: enter_pressed(); break;
    case 27: esc_pressed(); break;
    ...
    }
  ...

Notice that the trick is to immediately get the second code in a two-code sequence?


Since you are making a text editor, it is OK to wait on getch() to return. Just put it in a big loop:

1
2
3
4
5
6
7
8
9
  bool done = false;
  while (!done)
    {
    int c =  getch();
    switch (c)
      {
      ...
      }
    }


Hope this helps.
Notice that the trick is to immediately get the second code in a two-code sequence?


Consider this. A user presses char 224 and expects to see α (alpha). But our program does not prints it right away. It makes a few comparisons to determine whether it should accept the second byte. Then after the comparisons the getch() is called to take the second byte. What if the user presses another char meanwhile? It wud be detected as something like a down , up , etc arrow.

Nice try- my function like yours also relies upon the immediateness of execution of code.

In some cases it might possible that user is able to enter the two characters fast enough ( especially when the user is a computer). Even your function wud detect this as a 2 byte single keypress.

What I want is a function that tells me the number of key presses or function which can accept two bytes from the input buffer unlike getch() which accepts only one byte.
Last edited on
I think you have a serious misunderstanding of how keyboard input is handled. You are also being quite rude, as I have given you exact, 20-year old, industry proven code. Bye.
What if the user presses another char meanwhile? It wud be detected as something like a down , up , etc arrow.


No, it won't.

I'd help you but...
I apologize If you felt I was rude, although I never intended to be.
If you think I have a serious misunderstanding of how keyboard input is handled-then please give me some pointers for that.
In some cases it might possible that user is able to enter the two characters fast enough ( especially when the user is a computer). Even your function wud detect this as a 2 byte single keypress.

This isn't correct. getch() doesn't read "keypresses" from the input buffer. It reads bytes. As was explained, "extended" key codes start with a special byte that tells you to go ahead and read the next byte, treating in a special way.

Your original issue hinges, I think, on your incorrect idea that someone would press a key that sends a single byte for α, and that that byte would be 224. This is not the case. Mainly, in fact, because the very problem you anticipate would occur; the program wouldn't be able to determine the difference between two individual bytes in the buffer and a pair. The value 224 is a reserved value for this purpose.
Thank you very much Zhuge.
Topic archived. No new replies allowed.