1

I have the following piece of code which reads in a character using the read system write call:

char character;
read(STDIN_FILENO, &character, 1);

How do I detect whether character is a backspace or not? Based on that, I need to delete the last character from the console output.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Adam Lee
  • 436
  • 1
  • 14
  • 49
  • Backspace is ASCII character 8. – Ken White Sep 27 '21 at 01:06
  • @KenWhite So it's as simple as if (character == 8)? – Adam Lee Sep 27 '21 at 01:08
  • 1
    I'd guess you could try it yourself and find out, couldn't you? :-) – Ken White Sep 27 '21 at 01:10
  • @KenWhite It doesn't seem that if (character == 8) detects whether the backspace is pressed or not unfortunately. – Adam Lee Sep 27 '21 at 01:12
  • I suppose that you need a keyboard hook here, not a function that reads a character; If you're running windows, then something like: https://stackoverflow.com/questions/22975916/global-keyboard-hook-with-wh-keyboard-ll-and-keybd-event-windows – Ivan Silkin Sep 27 '21 at 01:14
  • Did you try `char(8)`? ASCII 8 is the backspace character - you can read it yourself in any ASCII chart. It will be labeled as `BS` for `BackSpace`. Google it. – Ken White Sep 27 '21 at 01:15
  • @IvanSilkin I am on linux. – Adam Lee Sep 27 '21 at 01:22
  • @KenWhite I have tried if (character == 8), if (character == '\b'), and even if (character == 0x08), all to no avail. Is the problem related to the fact that I am using the read system call? – Adam Lee Sep 27 '21 at 01:23
  • You could print _any character_ that you read and check the different codes for the keys you pressed. If BS is not read, the mode of the terminal is not correct. – the busybee Sep 27 '21 at 07:48

2 Answers2

3

You can check if it's a backspace by looking for character number 8 (ASCII). It's written in C as '\b'.

However did you forget to put your terminal in RAW mode?

To change terminal to raw mode, see this answer to a different question: https://stackoverflow.com/a/13129698/14768 ; the code you want is in function changemode.

Joshua
  • 40,822
  • 8
  • 72
  • 132
0
#include<iostream>
#include<string>
#include<cstdlib>
#include <windows.h> 
#include <winuser.h> 
using namespace std;

int main(){
    
    string str;
    getline(cin, str);
    
    if(GetAsyncKeyState(8)) //checks to see if the input contained any backspaces
    {
        cout<<"Backspace was detected"; 
    }
    else{
        cout<<"Backspace Not detected";
    }
    
    return 0;
}

or similarly using a character:

#include<iostream>
#include<string>
#include<cstdlib>
#include <windows.h> 
#include <winuser.h> 
using namespace std;

int main(){
    
    char c;
    cin.get(c);
    
    if(GetAsyncKeyState(8)) //checks to see if the input contained any backspaces
    {
        cout<<"Backspace was detected"; 
    }
    else{
        cout<<"Backspace Not detected";
    }
    
    return 0;
}

I now realised you are using C instead of C++ so all you do is change the libraries you are using.

  • GetAsyncKeyState doesn't exist. See OP's comments. " I am on linux. " – Joshua Sep 27 '21 at 16:25
  • @Joshua that comment was not there prior to writing my proposed solution. – Keston Smith Sep 28 '21 at 03:54
  • I beg to differ. It was there before I wrote my answer and my answer sorts above yours when I sort by oldest. – Joshua Sep 28 '21 at 14:13
  • @Joshua, I opened answer tab before any replies were opened on your parent post because I was testing out many solutions out before I posted. During that time, it could be that you replied within that timeframe. I assumed it was Windows because the majority uses windows. But this solution is not incorrect , it's just not compatible with your OS. – Keston Smith Sep 30 '21 at 02:39
  • Ah I get it. Comment was posted while you were typing answer. – Joshua Sep 30 '21 at 02:41