I am working a text editor in the C prigramming language and I am stuck on how to make the user be able to move around in the console using the arrow key here is my code if you can help that would be great thank you.
// EDIT LOOP
void insertMode(char *buffer, const char *filename)
{
// Init cursor
int cursor = strlen(buffer);
COORD cursorPos = {0, 0};
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
BOOL isEditing = TRUE;
while (isEditing)
{
// clear screen, display file contents and show cursor
system("cls");
displayBuffer(buffer, filename);
// Show all instruction to the user
printf("\nesc = SAVE&QUIT\n");
// Move the cursor to the current cursorPos
SetConsoleCursorPosition(console, cursorPos);
// Capture user input
char ch = _getch();
// If 'ESC' key to return to normal mode
if (ch == 27)
{
// Save the file contents before exiting insert mode
if (saveFile(filename, buffer) == 0)
{
system("cls");
printf("\nFile saved successfully.\n");
}
else
{
printf("\nError saving file.\n");
}
break;
}
// 'Backspace' key
else if (ch == '\b')
{
if (cursor > 0)
{
buffer[cursor - 1] = '\0'; // Remove last character
cursor--;
}
}
// 'Enter' key
else if (ch == '\r')
{
if (cursor < MAX_BUFFER - 1)
{
// Adds newline
buffer[cursor] = '\n';
cursor++;
}
}
else
{
// Add the input character to the buffer
if (cursor < MAX_BUFFER - 1)
{
buffer[cursor] = ch;
cursor++;
buffer[cursor] = '\0';
}
}
}
}
I tried using gpt to try help me move the cursor using the arrow key but unfortunalaty it did not work.
You need to sign in to view this answers
Leave feedback about this