Qt key handling
Fix the popup versions of qt_yn_function() to handle control characters by using the same key press event decoding routine and menus and extended commands. Moves 'keyValue()' to qt_key.cpp and its declaration to qt_key.h, requring several files to start using #include "qt_key.h". 'make depend' update to follow.
This commit is contained in:
+36
-3
@@ -15,18 +15,51 @@ extern "C" {
|
|||||||
|
|
||||||
namespace nethack_qt_ {
|
namespace nethack_qt_ {
|
||||||
|
|
||||||
|
// convert a Qt key event into a simple ASCII character
|
||||||
|
uchar keyValue(QKeyEvent *key_event)
|
||||||
|
{
|
||||||
|
// key_event manipulation derived from NetHackQtBind::notify();
|
||||||
|
// used for menus and text windows in qt_menu.cpp, also for
|
||||||
|
// extended commands in xcmd.cpp and popup yn_function in qt_yndlg.cpp
|
||||||
|
|
||||||
|
const int k = key_event->key();
|
||||||
|
Qt::KeyboardModifiers mod = key_event->modifiers();
|
||||||
|
const QString &txt = key_event->text();
|
||||||
|
QChar ch = !txt.isEmpty() ? txt.at(0) : 0;
|
||||||
|
|
||||||
|
if (ch >= 128)
|
||||||
|
ch = 0;
|
||||||
|
// on OSX, ascii control codes are not sent, force them
|
||||||
|
if (ch == 0 && (mod & Qt::ControlModifier) != 0) {
|
||||||
|
if (k >= Qt::Key_A && k <= Qt::Key_Underscore)
|
||||||
|
ch = QChar((k - (Qt::Key_A - 1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
uchar result = (uchar) ch.cell();
|
||||||
|
//raw_printf("kV: k=%d, ch=%u", k, (unsigned) result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
NetHackQtKeyBuffer::NetHackQtKeyBuffer() :
|
NetHackQtKeyBuffer::NetHackQtKeyBuffer() :
|
||||||
in(0), out(0)
|
in(0), out(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetHackQtKeyBuffer::Empty() const { return in==out; }
|
bool NetHackQtKeyBuffer::Empty() const
|
||||||
bool NetHackQtKeyBuffer::Full() const { return (in+1)%maxkey==out; }
|
{
|
||||||
|
return (in == out);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NetHackQtKeyBuffer::Full() const
|
||||||
|
{
|
||||||
|
return (((in + 1) % maxkey) == out);
|
||||||
|
}
|
||||||
|
|
||||||
void NetHackQtKeyBuffer::Put(int k, int a, uint kbstate)
|
void NetHackQtKeyBuffer::Put(int k, int a, uint kbstate)
|
||||||
{
|
{
|
||||||
//raw_printf("k:%3d a:'%s' s:0x%08x", k, visctrl((char) a), kbstate);
|
//raw_printf("k:%3d a:'%s' s:0x%08x", k, visctrl((char) a), kbstate);
|
||||||
if ( Full() ) return; // Safety
|
if (Full())
|
||||||
|
return; // Safety
|
||||||
key[in] = k;
|
key[in] = k;
|
||||||
ascii[in] = a;
|
ascii[in] = a;
|
||||||
state[in] = (Qt::KeyboardModifiers) kbstate;
|
state[in] = (Qt::KeyboardModifiers) kbstate;
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
|
|
||||||
namespace nethack_qt_ {
|
namespace nethack_qt_ {
|
||||||
|
|
||||||
|
// not part of any class; used in qt_menu.cpp, qt_xcmd.cpp, qt_yndlg.cpp
|
||||||
|
extern uchar keyValue(QKeyEvent *key_event);
|
||||||
|
|
||||||
class NetHackQtKeyBuffer {
|
class NetHackQtKeyBuffer {
|
||||||
public:
|
public:
|
||||||
NetHackQtKeyBuffer();
|
NetHackQtKeyBuffer();
|
||||||
|
|||||||
+1
-18
@@ -37,6 +37,7 @@ extern "C" {
|
|||||||
#include "qt_post.h"
|
#include "qt_post.h"
|
||||||
#include "qt_menu.h"
|
#include "qt_menu.h"
|
||||||
#include "qt_menu.moc"
|
#include "qt_menu.moc"
|
||||||
|
#include "qt_key.h" // for keyValue()
|
||||||
#include "qt_glyph.h"
|
#include "qt_glyph.h"
|
||||||
#include "qt_set.h"
|
#include "qt_set.h"
|
||||||
#include "qt_streq.h"
|
#include "qt_streq.h"
|
||||||
@@ -55,24 +56,6 @@ namespace nethack_qt_ {
|
|||||||
void centerOnMain( QWidget* w );
|
void centerOnMain( QWidget* w );
|
||||||
// end temporary
|
// end temporary
|
||||||
|
|
||||||
uchar keyValue(QKeyEvent *key_event)
|
|
||||||
{
|
|
||||||
// key_event manipulation derived from NetHackQtBind::notify()
|
|
||||||
const int k = key_event->key();
|
|
||||||
Qt::KeyboardModifiers mod = key_event->modifiers();
|
|
||||||
QChar ch = !key_event->text().isEmpty() ? key_event->text().at(0) : 0;
|
|
||||||
if (ch >= 128)
|
|
||||||
ch = 0;
|
|
||||||
// on OSX, ascii control codes are not sent, force them
|
|
||||||
if (ch == 0 && (mod & Qt::ControlModifier) != 0) {
|
|
||||||
if (k >= Qt::Key_A && k <= Qt::Key_Underscore)
|
|
||||||
ch = QChar((k - (Qt::Key_A - 1)));
|
|
||||||
}
|
|
||||||
uchar result = (uchar) ch.cell();
|
|
||||||
//raw_printf("kV: k=%d, ch=%d", k, result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
QSize NetHackQtTextListBox::sizeHint() const
|
QSize NetHackQtTextListBox::sizeHint() const
|
||||||
{
|
{
|
||||||
QScrollBar *hscroll = horizontalScrollBar();
|
QScrollBar *hscroll = horizontalScrollBar();
|
||||||
|
|||||||
@@ -15,8 +15,6 @@
|
|||||||
|
|
||||||
namespace nethack_qt_ {
|
namespace nethack_qt_ {
|
||||||
|
|
||||||
extern uchar keyValue(QKeyEvent *key_event); // also used in qt_xcmd.cpp
|
|
||||||
|
|
||||||
class NetHackQtTextListBox : public QListWidget {
|
class NetHackQtTextListBox : public QListWidget {
|
||||||
public:
|
public:
|
||||||
NetHackQtTextListBox(QWidget* parent = NULL) : QListWidget(parent) { }
|
NetHackQtTextListBox(QWidget* parent = NULL) : QListWidget(parent) { }
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ extern "C" {
|
|||||||
#include "qt_post.h"
|
#include "qt_post.h"
|
||||||
#include "qt_xcmd.h"
|
#include "qt_xcmd.h"
|
||||||
#include "qt_xcmd.moc"
|
#include "qt_xcmd.moc"
|
||||||
|
#include "qt_key.h" // for keyValue()
|
||||||
#include "qt_bind.h"
|
#include "qt_bind.h"
|
||||||
#include "qt_set.h"
|
#include "qt_set.h"
|
||||||
#include "qt_str.h"
|
#include "qt_str.h"
|
||||||
|
|||||||
+6
-7
@@ -16,6 +16,7 @@ extern "C" {
|
|||||||
#include "qt_post.h"
|
#include "qt_post.h"
|
||||||
#include "qt_yndlg.h"
|
#include "qt_yndlg.h"
|
||||||
#include "qt_yndlg.moc"
|
#include "qt_yndlg.moc"
|
||||||
|
#include "qt_key.h" // for keyValue()
|
||||||
#include "qt_str.h"
|
#include "qt_str.h"
|
||||||
|
|
||||||
// temporary
|
// temporary
|
||||||
@@ -369,14 +370,12 @@ void NetHackQtYnDialog::AltChoice(char ans, char res)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetHackQtYnDialog::keyPressEvent(QKeyEvent* event)
|
void NetHackQtYnDialog::keyPressEvent(QKeyEvent *event)
|
||||||
{
|
{
|
||||||
keypress = '\0';
|
keypress = keyValue(event);
|
||||||
QString text(event->text());
|
if (!keypress)
|
||||||
if (text.isEmpty()) /* && event->modifiers()) */
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
keypress = text.at(0).cell();
|
|
||||||
char *p = NULL;
|
char *p = NULL;
|
||||||
if (*alt_answer && (p = strchr(alt_answer, keypress)) != 0)
|
if (*alt_answer && (p = strchr(alt_answer, keypress)) != 0)
|
||||||
keypress = alt_result[p - alt_answer];
|
keypress = alt_result[p - alt_answer];
|
||||||
@@ -388,13 +387,13 @@ void NetHackQtYnDialog::keyPressEvent(QKeyEvent* event)
|
|||||||
int where = QString::fromLatin1(choices).indexOf(QChar(keypress));
|
int where = QString::fromLatin1(choices).indexOf(QChar(keypress));
|
||||||
|
|
||||||
if (allow_count && strchr("#0123456789", keypress)) {
|
if (allow_count && strchr("#0123456789", keypress)) {
|
||||||
if (text == "#") {
|
if (keypress == '#') {
|
||||||
// 0 will be preselected; typing anything replaces it
|
// 0 will be preselected; typing anything replaces it
|
||||||
le->setText(QString("0"));
|
le->setText(QString("0"));
|
||||||
le->home(true);
|
le->home(true);
|
||||||
} else {
|
} else {
|
||||||
// digit will not be preselected; typing another appends
|
// digit will not be preselected; typing another appends
|
||||||
le->setText(text);
|
le->setText(QChar(keypress));
|
||||||
le->end(false);
|
le->end(false);
|
||||||
}
|
}
|
||||||
// (don't know whether this actually does anything useful)
|
// (don't know whether this actually does anything useful)
|
||||||
|
|||||||
Reference in New Issue
Block a user