2

I was making a small text editor in c++ for fun and the code worked, though a bit slow, but when I tried to paste something into the editor, I ran into a problem. When I typed ctrl+v, instead of it pasting the copied text, which was a web link, it showed this: ▬. This happened when I actually pressed ctrl+c or ctrl+s or any letter with ctrl at the beginning to be fair. Why does this happen and is there a way to stop this?

int editTxtFile() {
  std::string txtstr;
  std::string filename;
  std::cin >> filename;
  std::ofstream file("TXTFILES\\"+filename+".txt", std::ios::out);
  std::cout << "This is an editor for txt files (sorry for the flashing!)\n";

  char keypressed;
  int asciiVal;
  if (file.is_open()) {
    while (true) {
      keypressed = getch();
      asciiVal = keypressed;
      switch (asciiVal) {
        case 13:
          txtstr += "\n";
          system("cls");
          break;
        case 8:
          txtstr = txtstr.substr(0, txtstr.size() - 1);
          system("cls");
          break;
        case 94:
          file << txtstr;
          system("cls");
          file.close();
          break;
        default:
          txtstr += keypressed;
          system("cls");
          break;
      }
      std::cout << txtstr;
    }
  }
  return 0;
}

I am using the header file conio.h for the getch() function. How can I fix this problem? Thank you in advance.

  • 1
    Depends on your console. Not a c++ issue. – super Nov 12 '20 at 19:50
  • I am on Windows 10 and it is the default conole. Is there a way to solve this problem? – TheKonamiKoder Nov 12 '20 at 19:51
  • 1
    Look for another console. Find a library to control it, like ncurses or something. – super Nov 12 '20 at 19:57
  • You're not doing anything special with a ctrl+v, so it is handled like a normal character. You need to add a handler for it like you do for 13 (enter) or 8 (backspace). What are you going to do if you want to enter a `'^'` into the file you're editing? – 1201ProgramAlarm Nov 12 '20 at 20:27
  • 1
    Standard C++ has no concept of the clipboard. Copy/Paste operations are the responsibility of the Console Terminal to handle. – Remy Lebeau Nov 12 '20 at 21:23
  • "*I am using the header file conio.h for the getch() function.*" - You really shouldn't be use `` in C++. You can replace `getch()` with `cin.get()` instead. – Remy Lebeau Nov 12 '20 at 21:23

0 Answers0