Solved

CRichEditCtrl problem with backspace-char from network

Posted on 2003-10-31
8
664 Views
Last Modified: 2013-11-20
Hi,
I am writing a dialogbased MFC console/telnet application. My problem is that when a backspace character (ascii 8) is arriving from some external source (network, serial...not the keyboard) the CRichEditCtrl class won´t print it out correctly. Instead it prints out a garbage character. Is there an easy way to handle this? I don´t want to check every arriving character, that would slow the application down to much.


void CRichEditCtrlTerminal::OnDataArrival(char *buffer, int size)
{
      // Insert in rich edit
      // Without checking every incoming char
      .
      .
      .
      ReplaceSel(buffer)      // Generates garbage when backspace (ascii 8) arrives
      .
      .
      .
}

This problem is driving me nuts, coz I have a gut feeling that it´s not a difficult problem.

// Icco
0
Comment
Question by:Icco
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
'I don´t want to check every arriving character, that would slow the application down to much'

I would be surprised that would happen.

Get your buffer
use _tcschr to search for an instance of ascii 8, if there is one (or more) then replace with what you want to display.
Now pass the buffer onto the RichEdit
0
 
LVL 3

Expert Comment

by:freewell
Comment Utility
Just before you pass the result into edit control, do a little conversion. :)

CString strTemp(buffer);
while(strTemp.Replace(0x08,'*'));
ReplaceSel(strTemp);
0
 
LVL 8

Assisted Solution

by:martynjpearson
martynjpearson earned 250 total points
Comment Utility
There is no option to disregard incoming characters like that, so you will have to do a cleanup. Performance shouldn't be affected that much, but if you are concerned, working with a raw character buffer rather than a CString etc is probably quicker.

If you want to replace the character with another one, then you can just go through your buffer and do a substitution. However, if you wish to remove the ascii 8 characters, then you'll have to create a new buffer and copy all valid characters over, and then pass that to ReplaceSel() thus :

char szWorking = new char[size];
char * pszDest = szWorking;

for (int nCount = 0; nCount < size; nCount++)
{
   if (buffer[nCount] != 8)
     *pszDest++ = buffer[nCount];
}
*pszDest = 0;

ReplaceSel(szWorking);

delete [] szWorking;


Hope this helps
Martyn
0
 

Author Comment

by:Icco
Comment Utility
I'm sorry if i didn't give you enough information.
When receiving ascii character 0x08 from some external connection I would like the cursor to step one char back.

In this case, a backspace from the OS that I am trying to communicate with through a console port, is the sequence  0x08 0x32 0x08.
This means: go back one char (0x08), print space char(0x32), go back one char (0x08).  When trying to print the 0x08 char to the CRichEditCtrl, it prints out garbage where I want it to go back one char.

example:

When I send the backspacechar to the console, the shell echoes back the sequence 0x08, 0x32, 0x08.
This will be printed as: '',' ','' in the CRichEditCtrl instead of removing the last char.

The sequence 0x08 0x32 0x08 is nothing that I can change, that´s what is received from the console and I have to handle it correctly.

//Icco
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Do I understand correctly that you would want the 0x08 0x32 0x08 string to be changed to 0x32 ?
If so parse the buffer you have for incidences of this string. Copy the bit before to another buffer.  Move 3 chars forward then look for next instance.  None found, copy rest of string to second buffer, found an instance copy up to that point then forward 3 chars again, continue parsing.
0
 
LVL 8

Expert Comment

by:martynjpearson
Comment Utility
OK, in that case there are two approaches - either you can process the text to remove characters that fall before the 0x08 (to simulate backspace), or you can break up the data top get the bits of text and the 0x08's, sending the text, then getting the rich edit control to delete the last character, then sending the next.

For example, say you have the data

H e l l l BS o ! ! ! BS

You could process the string so that the third 'l' and the third '!' are removed (along with the backspaces of course), or you could break the text up like this :

Send "Helll" to control
Get control to backspace
Send "o!!!" to control
Get control to backspace

It is probably easier and faster to process the string to deal with backspaces before you get to the control, using code like this :

The one complication arises when the stream starts with one or more backspace characters, as here you will need to instruct the rich edit control to do the backspace for you. However, you could be a little cunning and set the selection so that your text overwrites part of the content simulating a backspace.

Hope this helps
Martyn
0
 

Author Comment

by:Icco
Comment Utility
So if I understand this right, there isn't a way to insert text (that contains BS-chars) arriving from a shell without processing it or simulating a BS.

martynjpearson writes: "...you will need to instruct the rich edit control to do the backspace for you."
I tried this to simulate a backspace:

      GetSel(start_pos, end_pos);
      start_pos--;
      SetSel(start_pos,end_pos);
      ReplaceSel(CString(""));

Is there an easier way to get the control to do a backspace? The way it works now is not really what I want.
I want it to go back without processing incoming data, but maybe it just isn´t possible...


Real-life example coming up =)
When pressing escape followed by 'k' the shell returns the last command followed by the equal amount of 0x08-chars. It does this so that the cursor will be set at the first position on that line (don´t ask me why, but it does it that way).

In my app the output will be:
-> version
the cursor is set at the last position.

But the output should be:
-> version
and the cursor set at the first position (before the 'v')


// Icco
0
 
LVL 8

Accepted Solution

by:
martynjpearson earned 250 total points
Comment Utility
You could fire a message to your rich edit control to simulate the keypress, i.e.

SendMessage(WM_CHAR, VK_BACK);

It is probably best to use SendMessage rather than PostMessage so that the processing is done synchronously!

Martyn
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ORA-01403: no data found 43 82
Excel Use VBA to get user's Mac Address for their computer 5 81
no14 challenge 14 56
fix34  challenge 9 95
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now