From b0538aaa6839187ea0821dc77a4d017408f2c2b1 Mon Sep 17 00:00:00 2001 From: Federico Villa Date: Thu, 2 May 2024 16:20:51 +0200 Subject: [PATCH] Fix issue #304 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. --- src/console.cpp | 55 +++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index e5ac5df2f..80adab8f3 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -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()); } } }