From 18657676cc04c0d2d50edf5bf23d690b0915f27c Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Sat, 13 Mar 2021 21:39:00 +0900 Subject: [PATCH] Fixed key text display when shift is used (caps is still broken) as well as several improvements to the code. Signed-off-by: Michele Calgaro (cherry picked from commit 00e207e95d5aab6bbc37c8178bd189b0c0619be6) --- src/MainWidget.cpp | 203 ++++++++++++++++++--------------------------- src/MainWidget.h | 11 +-- src/VButton.cpp | 75 +++++++++-------- src/VButton.h | 4 +- 4 files changed, 132 insertions(+), 161 deletions(-) diff --git a/src/MainWidget.cpp b/src/MainWidget.cpp index 26c0f8f..8797085 100644 --- a/src/MainWidget.cpp +++ b/src/MainWidget.cpp @@ -132,7 +132,6 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const connect ( v,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( keyPress ( unsigned int ) ) ); btns.append ( v ); v->res(); - //caps_btns.append ( v ); } VButton *bksp = new VButton ( this,"" ); @@ -201,10 +200,9 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const caps->resize ( 63,30 ); caps->setText ( "Caps" ); caps->setToggleButton ( true ); - caps->res(); + connect ( caps,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( toggleShift ( unsigned int ) ) ); other_keys.append(caps); - connect ( caps,TQT_SIGNAL ( clicked() ),this,TQT_SLOT ( toggleCaps() ) ); - connect ( caps,TQT_SIGNAL ( keyClick ( unsigned int ) ),this,TQT_SLOT ( keyPress ( unsigned int ) ) ); + caps->res(); for ( int a=0;ares(); //ROW 4 - lshft = new VButton ( this,"" ); - lshft->setKeyCode ( 50 ); - lshft->move ( stx,sty+ ( 4*35 ) ); - lshft->resize ( 45,30 ); - lshft->setText ( "Shift" ); - lshft->setToggleButton ( true ); - connect ( lshft,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( toggleShift() ) ); - mod_keys.append ( lshft ); - lshft->res(); + lshift = new VButton ( this,"" ); + lshift->setKeyCode ( 50 ); + lshift->move ( stx,sty+ ( 4*35 ) ); + lshift->resize ( 45,30 ); + lshift->setText ( "Shift" ); + lshift->setToggleButton ( true ); + connect ( lshift,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( toggleShift( unsigned int ) ) ); + mod_keys.append ( lshift ); + lshift->res(); for ( int a=0;asetKeyCode ( kc4[a] ); //v->setText ( k4.mid ( a,1 ) ); //v->setShiftText ( k4s.mid ( a,1 ) ); - v->move ( stx+lshft->width() +5+ ( 35*a ),sty+ ( 4*35 ) ); + v->move ( stx+lshift->width() +5+ ( 35*a ),sty+ ( 4*35 ) ); btns.append ( v ); v->res(); connect ( v,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( keyPress ( unsigned int ) ) ); @@ -269,7 +267,7 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const VButton *sm = new VButton ( this,"" ); sm->setKeyCode ( 59 ); - sm->move ( stx+ ( R4LEN *35 ) +lshft->width() +5,sty+ ( 4*35 ) ); + sm->move ( stx+ ( R4LEN *35 ) +lshift->width() +5,sty+ ( 4*35 ) ); //sm->setText ( "," ); //sm->setShiftText ( "<" ); connect ( sm,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( keyPress ( unsigned int ) ) ); @@ -278,7 +276,7 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const VButton *gr = new VButton ( this,"" ); gr->setKeyCode ( 60 ); - gr->move ( stx+ ( ( R4LEN +1 ) *35 ) +lshft->width() +5,sty+ ( 4*35 ) ); + gr->move ( stx+ ( ( R4LEN +1 ) *35 ) +lshift->width() +5,sty+ ( 4*35 ) ); //gr->setText ( "." ); //gr->setShiftText ( ">" ); connect ( gr,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( keyPress ( unsigned int ) ) ); @@ -287,22 +285,22 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const VButton *sl = new VButton ( this,"" ); sl->setKeyCode ( 61 ); - sl->move ( stx+ ( ( R4LEN +2 ) *35 ) +lshft->width() +5,sty+ ( 4*35 ) ); + sl->move ( stx+ ( ( R4LEN +2 ) *35 ) +lshift->width() +5,sty+ ( 4*35 ) ); //sl->setText ( "/" ); //sl->setShiftText ( "?" ); connect ( sl,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( keyPress ( unsigned int ) ) ); btns.append ( sl ); sl->res(); - rshft = new VButton ( this,"" ); - rshft->setKeyCode ( 62 ); - rshft->move ( stx+ ( ( R4LEN +3 ) *35 ) +lshft->width() +5,sty+ ( 4*35 ) ); - rshft->resize ( 68,30 ); - rshft->setText ( "Shift" ); - rshft->setToggleButton ( true ); - connect ( rshft,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( toggleShift() ) ); - mod_keys.append ( rshft ); - rshft->res(); + rshift = new VButton ( this,"" ); + rshift->setKeyCode ( 62 ); + rshift->move ( stx+ ( ( R4LEN +3 ) *35 ) +lshift->width() +5,sty+ ( 4*35 ) ); + rshift->resize ( 68,30 ); + rshift->setText ( "Shift" ); + rshift->setToggleButton ( true ); + connect ( rshift,TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( toggleShift( unsigned int ) ) ); + mod_keys.append ( rshift ); + rshift->res(); //ROW 5 lctrl = new VButton ( this,"" ); @@ -378,8 +376,6 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const mod_keys.append ( rctrl ); rctrl->res(); - mappingNotify(NULL); - quit = new VButton ( this,"quit" ); quit->resize ( 20,30 ); quit->move ( 524,15 ); @@ -461,8 +457,7 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const numl->setText("Num\nLock"); numl->setToggleButton ( true ); other_keys.append(numl); - connect ( numl, TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( keyPress ( unsigned int ) ) ); - connect ( numl, TQT_SIGNAL ( clicked() ), this, TQT_SLOT ( toggleNumlock() ) ); + connect ( numl, TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( toggleNumlock() ) ); div = new VButton(this,"div"); @@ -508,6 +503,8 @@ MainWidget::MainWidget ( TDEAboutData *about, bool tren, TQWidget *parent, const min->res(); connect ( min, TQT_SIGNAL ( keyClick ( unsigned int ) ), this, TQT_SLOT ( keyPress ( unsigned int ) ) ); + mappingNotify(NULL); + if (!stand_alone) { tray = new KbdTray ( this ); @@ -656,6 +653,7 @@ void MainWidget::restorePosition() } setGeometry(dflt_geom); } + void MainWidget::saveState() { TDEConfig *cfg = 0; @@ -831,6 +829,7 @@ void MainWidget::showDock() cfg->sync(); } + void MainWidget::toggleFontAutoRes() { bool c = tray->contextMenu()->isItemChecked( mnu_autores); @@ -839,64 +838,52 @@ void MainWidget::toggleFontAutoRes() cfg->writeEntry ("autoresfont", !c); cfg->sync(); } + void MainWidget::toggleNumlock() { - bool p=numl->isOn(); - for ( unsigned a=0;anumlockPressed(p); - } + send_key(numl->getKeyCode()); + updateNumlock(); } -void MainWidget::toggleCaps() + +void MainWidget::updateNumlock() { - bool p=caps->isOn(); - for ( unsigned a=0;acapsPressed(p); + bool state = numl->isOn(); + for (unsigned a = 0; a < numl_keys.size(); a++) + { + NumpadVButton *v = numl_keys[a]; + v->numlockPressed(state); } } -void MainWidget::toggleShift() +void MainWidget::toggleShift(unsigned int keycode) { - bool p=false; - if ( lshft->isOn() || rshft->isOn() ) p=true; + send_key(keycode); + updateShift(); +} - for ( unsigned a=0;aisOn() ^ (lshift->isOn() || rshift->isOn()); + for (unsigned a = 0; a < btns.size(); a++) + { VButton *v = btns[a]; - v->shiftPressed ( p ); + v->shiftPressed(state); } - } void MainWidget::keyPress ( unsigned int a ) { - send_key ( a,true,true ); - bool reverse = false; - if (lshft->isOn() || rshft->isOn()) - reverse=true; - - for ( unsigned a=0;asetOn ( false ); - } - - if (caps->isOn()) { - if (reverse) { - for (unsigned a=0;acapsPressed(true); - } - } - }else { - for (unsigned a=0;acapsPressed(false); - } + send_key(a); + for (unsigned a = 0; a < mod_keys.size(); a++) + { + VButton *mod = mod_keys[a]; + mod->setOn(false); } - + // Make sure the key labels are correctly updated + updateShift(); } -void MainWidget::send_key ( unsigned int keycode, bool press, bool release ) + +void MainWidget::send_key(unsigned int keycode) { Window curr_focus; int revert_to; @@ -942,68 +929,52 @@ bool MainWidget::keyState ( int iKey ) void MainWidget::queryModState() { - //printf("Scroll: %d\n",keyState(XK_Scroll_Lock,pDisplay)); //printf("Caps : %d\n",keyState(XK_Caps_Lock,pDisplay)); //printf("Num : %d\n",keyState(XK_Num_Lock,pDisplay)); - bool caps_state = keyState ( XK_Caps_Lock); - bool numl_state = keyState ( XK_Num_Lock); - - if ( caps_state!=caps->isOn() ) { - caps->setOn ( caps_state ); - toggleCaps(); + bool caps_state = keyState(XK_Caps_Lock); + if (caps_state != caps->isOn()) + { + caps->setOn(caps_state); + updateShift(); } - if ( numl_state!= numl->isOn() ) { + + bool numl_state = keyState(XK_Num_Lock); + if (numl_state != numl->isOn()) + { numl->setOn(numl_state); - toggleNumlock(); + updateNumlock(); } } void MainWidget::setupText(VButton& v) { - KeyCode keycode=v.getKeyCode(); + // lowercase text KeySym keysym_l = XkbKeycodeToKeysym(display, keycode, 0, 0); - - KeySym keysym_u = XkbKeycodeToKeysym(display, keycode, 0, 1); - - long ret = keysym2ucs(keysym_l); - - TQString btn_text(TQChar((uint)ret)); - + TQString btn_text(TQChar((uint)keysym2ucs(keysym_l))); v.setText(btn_text); - TQString btn_upper(btn_text.upper()); - - if (btn_upper==btn_text) { - ret = keysym2ucs(keysym_u); - TQChar c((uint)ret); - if (c=='&') - v.setShiftText("&&"); - else - v.setShiftText(c); + // uppercase text + KeySym keysym_u = XkbKeycodeToKeysym(display, keycode, 0, 1); + TQChar c((uint)keysym2ucs(keysym_u)); + if (c == '&') + { + v.setShiftText("&&"); } - else { - v.setShiftText(btn_upper); + else + { + v.setShiftText(c); } } void MainWidget::mappingNotify(XMappingEvent *) { - //TQTimer::singleShot( 1000, this, TQT_SLOT(test()) ); - //if (e)XRefreshKeyboardMapping(e); - - //int index=0; - - for (unsigned a=0;a other_keys; TQValueList numl_keys; - void send_key(unsigned int keycode, bool press, bool release); + void send_key(unsigned int keycode); Display *display; KbdTray *tray; diff --git a/src/VButton.cpp b/src/VButton.cpp index b4870f1..cabedd1 100644 --- a/src/VButton.cpp +++ b/src/VButton.cpp @@ -23,33 +23,12 @@ VButton::~VButton() void VButton::shiftPressed(bool press) { - if (press==true){ - TQString tu=l.upper(); - if (tu.compare(l)==0){ - //TQPushButton::setText(tu); - TQPushButton::setText(u); - } - else{ - TQPushButton::setText(tu); - } + if (press) + { + TQPushButton::setText(u); } - else{ - TQPushButton::setText(l); - } -} - -void VButton::capsPressed(bool press) -{ - if (press==true){ - TQString tu=l.upper(); - if (tu.compare(l)==0){ - TQPushButton::setText(tu); - } - else{ - TQPushButton::setText(u); - } - } - else{ + else + { TQPushButton::setText(l); } } @@ -107,34 +86,62 @@ void VButton::res() orig_size=geometry(); } -void VButton::mousePressEvent(TQMouseEvent * e) +void VButton::enterEvent(TQEvent *e) +{ + TQPushButton::enterEvent(e); + inside = true; +} + +void VButton::leaveEvent(TQEvent *e) +{ + TQPushButton::leaveEvent(e); + inside = false; +} + +void VButton::mousePressEvent(TQMouseEvent *e) { press=true; TQPushButton::mousePressEvent(e); - sendKey(); - startTimer(500); + if (!isToggleButton()) + { + // non toggle buttons need to send the key here + sendKey(); + startTimer(500); + } } -void VButton::mouseReleaseEvent(TQMouseEvent * e) +void VButton::mouseReleaseEvent(TQMouseEvent *e) { press=false; TQPushButton::mouseReleaseEvent(e); - TQT_TQOBJECT(this)->killTimers(); + if (hitButton(e->pos()) && isToggleButton()) + { + // toggle buttons need to send the key here because the underlying pushbutton state + // is only updated on mouse release + sendKey(); + } + killTimers(); inrpt=false; } void VButton::timerEvent(TQTimerEvent *) { - if (!press){ + if (!press) + { inrpt=false; return; } - if (press && !inrpt) { + if (press && !inrpt) + { inrpt=true; startTimer(120); return; } - sendKey(); + if (inside) + { + // Send key only if the mouse is still inside the button rectangle + sendKey(); + } } #include "VButton.moc" diff --git a/src/VButton.h b/src/VButton.h index 2f916ff..cab13cd 100644 --- a/src/VButton.h +++ b/src/VButton.h @@ -17,7 +17,6 @@ public: void setText(const TQString &text); void setShiftText(const TQString &text); void setColor(const TQColor &color); - void capsPressed(bool press); void reposition(int width, int height); void res(); @@ -28,6 +27,7 @@ public: private: bool press; bool inrpt; + bool inside; protected: unsigned int keycode; @@ -42,6 +42,8 @@ public slots: void shiftPressed(bool press); protected slots: + void enterEvent(TQEvent *e); + void leaveEvent(TQEvent *e); void mousePressEvent(TQMouseEvent *e); void mouseReleaseEvent(TQMouseEvent *e);