EASY Question, How to detect delete key and backspace input for scanf

Posted on 2004-04-28
Last Modified: 2007-12-19

Title is a little confusing, but my program asks for input
and if they mess up and press the backspace it inserts weird characters, or when they press the back arrow key or forward error key.

How to detect that these keys were pressed and the input is deleted one space or if arrow key is pressed the cursor goes back or forward one place?

I am doing this program in C and some C++, so either solution would be nice.
This is a command line program.

Question by:LinkLink
LVL 46

Accepted Solution

Sjef Bosman earned 45 total points
ID: 10941244
You can't on a Linux-system. Normally the system is in "cooked" mode, unless you set "raw" mode. In "cooked" mode, some or all special characters are dealt with by the O/S itself.

What system are you on, what O/S??

Author Comment

ID: 10941314
I am on a Linux machine, Red Hat

A Project for school we are writing our own bash shell type thing,  I have got the whole thing working just need to make it so when you input something you can delete, and use arrow keys like a normal shell would do.

Author Comment

ID: 10941365
It is not part of the assignment, but just for my own satisfaction of making it the best I can
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

LVL 10

Assisted Solution

Mercantilum earned 40 total points
ID: 10941544
I guess your problem is actually that the default behaviour of backspace is not well configured,


     "abcdx" you want to remove the "x" to put a "e" and you get
     "abcdx^H" for instance.
Have a look to

Assisted Solution

Avik77 earned 40 total points
ID: 10941615
  try to use the terminfo capabilities in term.h and temios.h
There u have to properly configure the terminal environment at the start of the program.
Moreover u can also search for the tgoto function manuals. I don't remember whether it is available in ur Red Hat L. There is also the curses or ncurses library. Go through the manuals of each of them . You may get a possible solution. Because these libraries lets u configure the terminal of the Linux system.


Author Comment

ID: 10941982

you all had right answers so I split the points amongst you,
The thing is I was ssh ing into  the linux machine from my Mac, so the backspace keys are different and linux wasn't recognizing that.


Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery/ajax to code behind 7 158
Inorder binary search tree 5 153
List out all word 7 224
Problem to save 10 121
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

758 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

21 Experts available now in Live!

Get 1:1 Help Now