LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2020, 10:56 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
how to get backspace input from keyboard


hi to all, I've mysql server in centos6 server and codeblocks in centos 7client. i am trying to do autofill of

product name when we input some initial letters. this happned but it does not getting back when giving input of

backspace. here is mine code :-
Code:
void product::productSearch()
{
    Get *get = new Get;
    clrscr();
    string prdin = "";
    string prd1 = "";
    int l = get->printMenu(productRecords, "Product Search");
    gotoxy(30, 5);
    //cin >> prdin;
    Getchar *getc;
    char ch;

    MYSQL_ROW row;
    MYSQL_RES *res;
    Conn::conn = Conn::connection();

    do
    {
        ch = getc->getch();
        if(ch == '\b')
        {
            if(prdin.length() > 0)
            {
                prdin = prdin.substr(prdin.length() -1);
            }
        }
        else
        {
            prdin += ch;
        }

        sql = "select productname,stock, rate, productId  from tableProductRecords where productname like '%"+prdin

+"%';";
        int qstate = mysql_query(Conn::conn, sql.c_str());

        if(!qstate)
        {

            res = mysql_store_result(Conn::conn);

            if((row = mysql_fetch_row(res)) != nullptr)
            {
                gotoxy(30, 5);
                cout << row[0];

                gotoxy(30,6);
                cout << row[1];

                gotoxy(30, 7);
                cout << row[2];

                gotoxy(30, 8);
                cout << row[3];
                gotoxy(30 + prdin.length() , 5);
                //ch = getc->getch();
            }
        }
        else
        {
            gotoxy(10, 20);
            cout << "error in product Enter : " << mysql_error(Conn::conn);
        }

    }while(ch != '\n');

    clrscr();
    drawrect();
}
mine getch code is :-
Code:
Read 1 character without echo
getch() function definition.

char Getchar::getch(void)
{
  return getch_(0);
}

void Getchar::initTermios(int echo)
{
  tcgetattr(0, &oldterm); //grab old terminal i/o settings
  newterm = oldterm; //make new settings same as old settings
  newterm.c_lflag &= ~ICANON; //disable buffered i/o
  newterm.c_lflag &= echo ? ECHO : ~ECHO; //set echo mode
  tcsetattr(0, TCSANOW, &newterm); //apply terminal io settings
}

/* Restore old terminal i/o settings */
void Getchar::resetTermios(void)
{
  tcsetattr(0, TCSANOW, &oldterm);
}
 
Old 09-25-2020, 11:32 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
If by '\b' you are hoping to catch a backspace you are wrong. '\b' means ring the bell.

You might try
Code:
if (ch == KEY_BACKSPACE)
and have a look at the ncurses manpage for getch.


https://linux.die.net/man/3/getch
 
2 members found this post helpful.
Old 09-25-2020, 12:34 PM   #3
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by SoftSprocket View Post
If by '\b' you are hoping to catch a backspace you are wrong. '\b' means ring the bell.

What was I thinking - \a is the bell (well alert) and \b is backspace but it won't work as expected just the same. The rest of my post does apply.
 
1 members found this post helpful.
Old 09-25-2020, 01:14 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rahulvishwakarma View Post
hi to all, I've mysql server in centos6 server and codeblocks in centos 7client. i am trying to do autofill of

product name when we input some initial letters. this happned but it does not getting back when giving input of

backspace. here is mine code :-
Code:
void product::productSearch()
{
    Get *get = new Get;
    clrscr();
    string prdin = "";
    string prd1 = "";
    int l = get->printMenu(productRecords, "Product Search");
    gotoxy(30, 5);
    //cin >> prdin;
    Getchar *getc;
    char ch;

    MYSQL_ROW row;
    MYSQL_RES *res;
    Conn::conn = Conn::connection();

    do
    {
        ch = getc->getch();
        if(ch == '\b')
        {
            if(prdin.length() > 0)
            {
                prdin = prdin.substr(prdin.length() -1);
            }
        }
        else
        {
            prdin += ch;
        }

        sql = "select productname,stock, rate, productId  from tableProductRecords where productname like '%"+prdin

+"%';";
        int qstate = mysql_query(Conn::conn, sql.c_str());

        if(!qstate)
        {

            res = mysql_store_result(Conn::conn);

            if((row = mysql_fetch_row(res)) != nullptr)
            {
                gotoxy(30, 5);
                cout << row[0];

                gotoxy(30,6);
                cout << row[1];

                gotoxy(30, 7);
                cout << row[2];

                gotoxy(30, 8);
                cout << row[3];
                gotoxy(30 + prdin.length() , 5);
                //ch = getc->getch();
            }
        }
        else
        {
            gotoxy(10, 20);
            cout << "error in product Enter : " << mysql_error(Conn::conn);
        }

    }while(ch != '\n');

    clrscr();
    drawrect();
}
mine getch code is :-
Code:
Read 1 character without echo
getch() function definition.

char Getchar::getch(void)
{
  return getch_(0);
}

void Getchar::initTermios(int echo)
{
  tcgetattr(0, &oldterm); //grab old terminal i/o settings
  newterm = oldterm; //make new settings same as old settings
  newterm.c_lflag &= ~ICANON; //disable buffered i/o
  newterm.c_lflag &= echo ? ECHO : ~ECHO; //set echo mode
  tcsetattr(0, TCSANOW, &newterm); //apply terminal io settings
}

/* Restore old terminal i/o settings */
void Getchar::resetTermios(void)
{
  tcsetattr(0, TCSANOW, &oldterm);
}
Really? YOUR getch code??
https://stackoverflow.com/questions/...-linux/7469410

And didn't you ask about something very, VERY similar a few months ago?
https://www.linuxquestions.org/quest...27-4175673488/

Since you've been a member here for ten years, and been coding for that long, you should know to do basic research, and know what '\b' is and does...a non-destructive backspace. Using
Code:
string = sometring.substr(0, somestring.size()-1);
would work. What have you done/tried to get 'your' code working?
 
Old 09-26-2020, 06:37 AM   #5
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Original Poster
Rep: Reputation: 2
please tell me how to take input of non-printable ascii chars. such as backspace or enter. or some special keys such as function keys and numpad keys.
 
Old 09-26-2020, 08:57 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rahulvishwakarma View Post
please tell me how to take input of non-printable ascii chars. such as backspace or enter. or some special keys such as function keys and numpad keys.
Please show effort of your own.

You can read any of the MANY programming guides that tell you how to do this and do basic research on your own. You can find this with a brief Google search, and again...you have been programming for **TEN YEARS** now. Have you not progressed any in an entire decade?? Also curious that you don't comment on 'your' getch code that you posted.

Try this: https://bfy.tw/PBp5

Last edited by TB0ne; 09-26-2020 at 09:02 AM.
 
Old 09-26-2020, 11:41 AM   #7
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Original Poster
Rep: Reputation: 2
i solved my problem via another website. there link as follows :-
https://www.cplusplus.com/forum/beginner/273034/
 
Old 09-27-2020, 09:14 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rahulvishwakarma View Post
i solved my problem via another website. there link as follows :-
https://www.cplusplus.com/forum/beginner/273034/
Excellent; so you finally were able to look something up for yourself? Great news!
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fgets is not taking input from keyboard after one input in centos 7.5 rahulvishwakarma Programming 9 10-12-2018 12:29 PM
Python: Quirky behavior on backspace of Chinese input General Programming 0 03-15-2010 06:44 AM
xf86-input-keyboard and xf86-input-mouse masked CollieJim Gentoo 4 11-09-2009 09:57 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration