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 :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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();
}
what happens if you make a test program and print out the integer value of what you typed in fetched by getch() and test that by typing in backspace?

gotoxy and getch are windows tools, not c++ and not portable. If that is OK with you, there are additional windows tools like kbhit that I believe can tell you when special keys were hit, eg it can tell the diff between 9 and 9 from normal 9 vs numeric keypad 9, and it can tell you left shift vs right shift, and so on --- things that games use to map controls that do not matter when just typing. This may be what is required; I don't recall what getch does for some instances but I don't think it can trap shift/ctrl/backspace/escape/ and such (??).

you can also read
https://www.cplusplus.com/forum/beginner/273033/
... maybe backspace is a 2 char response.
Last edited on
What is type Getchar?

'normal' MS _getch() returns 8 for backspace.

For MS, _kbhit() simply indicates whether there are char(s) waiting in the input buffer for extraction. Often used before _getch() so that _getch() is non-blocking if no input available.
Thanks Seeplus, I am rusty with windows, what is the one that is similar to getch, keypress()? I have a dim memory of getting keys with something and comparing them to some lookup table windows provided, VK_ labeled values for like F1 / esc / stuff ?

does not matter, if getch() returns 8, that should == \b ... not sure what is broken here

by the way substring to knock a letter off a string is dreadful. use pop_back.
Last edited on
mine getch code is :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
}

char Getchar::getch_(int echo)
{
  char ch;
  initTermios(echo);
  ch = getchar();
  resetTermios();
  return ch;
}
Last edited on
Instead of:

 
ch = getchar();


try:

 
ch = _getch();


getchar() is 'cooked' input.

PS Are you using Windows or Linux?

Also note that _getch() is non-echoing. If you want to echo, use _getche()

i am using centos linux version 7.5. and also there is no _getch in linux
No, the _versions are Windows specific.

Sorry I can't help further as I only use Windows. Outside of standard C++ input, special keyboard input is non-standard and different for different OS/compilers.

so please tell me anyone, how to take input key stroke of backspace in c++ in centos 7.5 linux
Last edited on
As jonnin said, make a test program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <termios.h>
#include <unistd.h>
using namespace std;

class Getchar {
private:
    termios   oldterm;
    termios   newterm;
    char getch_(bool echo);
public:
    char getch(void);
    void initTermios(bool echo);
    void resetTermios(void);
};

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

void Getchar::initTermios(bool 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);
}

char Getchar::getch_(bool echo)
{
  char ch;
  initTermios(echo);
  ch = getchar();
  resetTermios();
  return ch;
}

int main()
{
  Getchar mygch;
  char ch;
  do {
    ch = mygch.getch();
    std::cout << (int)ch << std::endl;
  } while ( ch != 'q' );
  return 0;
}



$ ./a.out 
127   - aka the backspace key
27    - aka the delete key, yes, you really need to deal with multiple key codes for some things
91
51
126
113   - aka 'q'
thanks a lot you solved my problem. one last question please :-
how to use multiple returning values output as by delete.
Well first you study how all the multi-byte key presses are encoded as a series of bytes.

Then perhaps you can take that information and return an int instead.
Topic archived. No new replies allowed.