Skip to content

Commit

Permalink
Fix issue #304 (#360)
Browse files Browse the repository at this point in the history
The issue #304 is related to WASM console input not working, as by pressing Enter, Return or Backspace key nothing happens. This was due to how those commands were handled: first it was checked that their text was non empty, then their keys were checked with the elements of the `Qt::Key` enumeration. The problem is that in WebAssembly the events related to those keys have an empty text, therefore the reported problem. I modified the logic behind the evaluation of those commands: in the `Console::keyPressEvent(QKeyEvent *e)` method there is an enumeration that used to handle just the arrow keys and the default case; I propose to handle the Return, Enter and Backspace keys differently from the rest of the commands, without checking if their related text is empty.
  • Loading branch information
federicovilla55 committed Aug 12, 2024
1 parent 0af6a3f commit f6627af
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,34 @@ void Console::keyPressEvent(QKeyEvent *e) {
case Qt::Key_Down:
QPlainTextEdit::keyPressEvent(e);
break;

case Qt::Key_Return:
case Qt::Key_Enter:
// Return is interpreted as \n instead of the default \r (\n)
m_buffer += "\n";

// Flush buffer to output
emit sendData(m_buffer.toLocal8Bit());
m_buffer.clear();

if (m_localEchoEnabled)
putData("\r");
break;

case Qt::Key_Backspace:
if (!m_buffer.isEmpty()) {
// Remove the last character from the buffer
m_buffer.chop(1);
if (m_localEchoEnabled)
backspace();
}
break;

default:
if (!e->text().isEmpty()) {
bool backspacedBuffer = false;
const QString text = e->text();
// Buffer managing
if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
// Return is interpreted as \n instead of the default \r
m_buffer += "\n";

// Flush buffer to output
emit sendData(m_buffer.toLocal8Bit());
m_buffer.clear();
} else if (e->key() == Qt::Key_Backspace) {
if (!m_buffer.isEmpty()) {
m_buffer.chop(1);
backspacedBuffer = true;
}
} else {
m_buffer += text;
}

// Console echoing
if (m_localEchoEnabled) {
if (e->key() == Qt::Key_Backspace) {
if (backspacedBuffer) {
backspace();
}
} else {
putData(text.toUtf8());
}
}
m_buffer += e->text();
if (m_localEchoEnabled)
putData(e->text().toUtf8());
}
}
}
Expand Down

0 comments on commit f6627af

Please sign in to comment.