| | |
how do I read backspace (getchar)?
Thread Solved |
hey, been working on a program and I had a need for a password line (btw I am using linux w/ gcc compiler). while I was making it I checked the net and found there's no getch() for linux, so I frankensteined one from several sources and got it to work. Only problem is, it doesn't register backspace ('\b'), so people can't correct mistakes. I tried using a different key instead of backspace, like \t for example, it worked as it should, getchar() registered it, it just doesn't seem to like \b. here's the code:
I've heard rumors of getchar not being able to read backspaces, but I wasn't sure. Also, I have no doubt there are better ways to emulate getch(), if you know of one please point me in the right direction. Any help would be appreciated.
~J
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <unistd.h> using namespace std; #ifndef KBHITh #define KBHITh #include <termios.h> class keyboard { public: keyboard(); ~keyboard(); private: struct termios initial_settings, new_settings; }; #endif keyboard::keyboard() { tcgetattr(0,&initial_settings); new_settings = initial_settings; new_settings.c_lflag &= ~ICANON; new_settings.c_lflag &= ~ECHO; new_settings.c_lflag &= ~ISIG; new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VTIME] = 0; tcsetattr(0, TCSANOW, &new_settings); } keyboard::~keyboard() { tcsetattr(0, TCSANOW, &initial_settings); } int main() { keyboard my_board; //essentially removes line buffer string pass=""; char c=' '; cout << "Testing passwords, enter a string to passwordize\n"; while(c != '\n') { c = getchar(); if(c != '\n') { if(c == '\b') //doesn't like this line { pass=pass.substr(0,pass.size()-1); cout << "\010 \010" << flush; } else { pass.push_back(c); cout << "*" << flush; } } } my_board.~keyboard(); //reverts to normal line buffering cout << "\nYou entered: \"" << pass << "\"" << endl; return 0; }
I've heard rumors of getchar not being able to read backspaces, but I wasn't sure. Also, I have no doubt there are better ways to emulate getch(), if you know of one please point me in the right direction. Any help would be appreciated.
~J
Ps. 121
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
hmmm... yes that does the job (and may look a little neater), but it clears the screen and only outputs things from the function until the program ends, that is not something I want to happen when I actually implement the function in the real program, unless there is an option for it not to do that. It also seems to again not register '\b', so I still have my original problem. code follows.
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <ncurses.h> using namespace std; int main() { initscr(); noecho(); string pass=""; char c=' '; cout << "Testing password stuff, enter a string to passwordize1\n" << flush; while(c != '\n') { c = getch(); if(c != '\n') { if(c == '\b') { pass=pass.substr(0,pass.size()-1); cout << "\010 \010" << flush; } else { pass.push_back(c); cout << "*" << flush; } } } endwin(); cout << "\nYou entered: \"" << pass << "\"" << endl; return 0; }
Ps. 121
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
I got it to read backspaces, keypad() does that trick. I still wish there was another way to do it while still inside the terminal. That just confirms the things I heard that the terminal intercepts the backspace before it can get to the program (not sure why, but it seems that way). Anywho, thanks for the nudge Ancient Dragon, I think I can still make it look/do the things I would like. For sake of completeness on anyone following this thread, here's the finished code:
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> #include <ncurses.h> using namespace std; int main() { initscr(); noecho(); keypad(stdscr, TRUE); string pass=""; int c=0; cout << "Testing password stuff, enter a string to passwordize1\n" << flush; while(c != '\n') { c = getch(); if(c != '\n') { if(c == KEY_BACKSPACE) { pass=pass.substr(0,pass.size()-1); cout << "\010 \010" << flush; } else { pass.push_back(c); cout << "*" << flush; } } } endwin(); cout << "\nYou entered: \"" << pass << "\"" << endl; return 0; }
Ps. 121
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
![]() |
Other Threads in the C++ Forum
- Previous Thread: Write a C++ program, I need help
- Next Thread: I don't know
Views: 5517 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ vector video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






