📄 keyboard.cpp
字号:
enum { BSCode = 0x80, TabCode, CapsCode, RetCode, ShiftCode, CtrlCode, AltCode, SpaceCode, BackSlash, UpCode, LeftCode, DownCode, RightCode, Blank, Expand, Opti, ResetDict, Divide, Multiply, Add, Subtract, Decimal, Equal, Percent, Sqrt, Inverse, Escape };typedef struct SpecialMap { int qcode; ushort unicode; const char * label; const char * const *xpm;};static const SpecialMap specialM[] = { { Qt::Key_Backspace, 8, "<", backspace_xpm }, { Qt::Key_Tab, 9, "Tab", NULL }, // No tr { Qt::Key_CapsLock, 0xffff, "Caps", NULL }, // No tr { Qt::Key_Return, 13, "Ret", NULL }, // No tr { Qt::Key_Shift, 0xffff, "Shift", NULL }, // No tr { Qt::Key_Control, 0xffff, "Ctrl", NULL }, // No tr { Qt::Key_Alt, 0xffff, "Alt", NULL }, // No tr { Qt::Key_Space, ' ', "", NULL }, { BackSlash, 43, "\\", NULL }, // Need images? { Qt::Key_Up, 0xffff, "^", uparrow_xpm }, { Qt::Key_Left, 0xffff, "<", leftarrow_xpm }, { Qt::Key_Down, 0xffff, "v", downarrow_xpm }, { Qt::Key_Right, 0xffff, ">", rightarrow_xpm }, { Qt::Key_Insert, 0xffff, "I", insert_xpm }, { Qt::Key_Home, 0xffff, "H", home_xpm }, { Qt::Key_PageUp, 0xffff, "U", pageup_xpm }, { Qt::Key_End, 0xffff, "E", end_xpm }, { Qt::Key_Delete, 0xffff, "X", delete_xpm }, { Qt::Key_PageDown, 0xffff, "D", pagedown_xpm }, { Blank, 0, " ", NULL }, { Expand, 0xffff, "->", expand_xpm }, { Opti, 0xffff, "#", NULL }, { ResetDict, 0xffff, "R", NULL }, // number pad stuff { Divide, 0, "/", NULL }, { Multiply, 0, "*", NULL }, { Add, 0, "+", NULL }, { Subtract, 0, "-", NULL }, { Decimal, 0, ".", NULL }, { Equal, 0, "=", NULL }, { Percent, 0, "%", NULL }, { Sqrt, 0, "^1/2", NULL }, { Inverse, 0, "1/x", NULL }, { Escape, 27, "ESC", escape_xpm }};static int keycode( int i2, int j, const uchar **keyboard ){ if ( j <0 || j >= 5 ) return 0; const uchar *row = keyboard[j]; while ( *row && *row <= i2 ) { i2 -= *row; row += 2; } if ( !*row ) return 0; int k; if ( row[1] >= 0x80 ) { k = row[1]; } else { k = row[1]+i2/2; } return k;}/* return scancode and width of first key in row \a j if \a j >= 0, or next key on current row if \a j < 0. */int Keyboard::getKey( int &w, int j ) { static const uchar *row = 0; static int key_i = 0; static int scancode = 0; static int half = 0; if ( j >= 0 && j < 5 ) { if (useOptiKeys) row = keyboard_opti[j]; else row = keyboard_standard[j]; half=0; } if ( !row || !*row ) { return 0; } else if ( row[1] >= 0x80 ) { scancode = row[1]; w = (row[0] * w + (half++&1)) / 2; row += 2; return scancode; } else if ( key_i <= 0 ) { key_i = row[0]/2; scancode = row[1]; } key_i--; if ( key_i <= 0 ) row += 2; return scancode++;} void Keyboard::paintEvent(QPaintEvent* e){ QPainter painter(this); painter.setClipRect(e->rect()); drawKeyboard( painter ); picks->dc->draw( &painter );}/* Draw the keyboard. If key >= 0, only the specified key is drawn.*/void Keyboard::drawKeyboard( QPainter &p, int key ){ const bool threeD = FALSE; const QColorGroup& cg = colorGroup(); QColor keycolor = // cg.background(); QColor(240,240,230); // Beige! QColor keycolor_pressed = cg.mid(); QColor keycolor_lo = cg.dark(); QColor keycolor_hi = cg.light(); QColor textcolor = QColor(0,0,0); // cg.text(); int margin = threeD ? 1 : 0; // p.fillRect( 0, , kw-1, keyHeight-2, keycolor_pressed ); for ( int j = 0; j < 5; j++ ) { int y = j * keyHeight + picks->height() + 1; int x = xoffs; int kw = defaultKeyWidth; int k= getKey( kw, j ); while ( k ) { if ( key < 0 || k == key ) { QString s; bool pressed = (k == pressedKey); bool blank = (k == 0223); const char * const * xpm = NULL; if ( k >= 0x80 ) { s = specialM[k - 0x80].label; xpm = specialM[k - 0x80].xpm; if ( k == ShiftCode ) { pressed = shift; } else if ( k == CapsCode ) { pressed = lock; } else if ( k == CtrlCode ) { pressed = ctrl; } else if ( k == AltCode ) { pressed = alt; } } else {#if defined(Q_WS_QWS) || defined(_WS_QWS_)/* s = QChar( shift^lock ? QWSServer::keyMap()[k].shift_unicode : QWSServer::keyMap()[k].unicode);*/ // ### Fixme, bad code, needs improving, whole thing needs to // be re-coded to get rid of the way it did things with scancodes etc char shifted = k; if ( !isalpha( k ) ) { for ( unsigned i = 0; i < sizeof(shiftMap)/sizeof(ShiftMap); i++ ) if ( shiftMap[i].normal == k ) shifted = shiftMap[i].shifted; } else { shifted = toupper( k ); } s = QChar( shift^lock ? shifted : k );#endif } if (!blank) { if ( pressed ) p.fillRect( x+margin, y+margin, kw-margin, keyHeight-margin-1, keycolor_pressed ); else p.fillRect( x+margin, y+margin, kw-margin, keyHeight-margin-1, keycolor ); if ( threeD ) { p.setPen(pressed ? keycolor_lo : keycolor_hi); p.drawLine( x, y+1, x, y+keyHeight-2 ); p.drawLine( x+1, y+1, x+1, y+keyHeight-3 ); p.drawLine( x+1, y+1, x+1+kw-2, y+1 ); } else if ( j == 0 ) { p.setPen(pressed ? keycolor_hi : keycolor_lo); p.drawLine( x, y, x+kw, y ); } // right p.setPen(pressed ? keycolor_hi : keycolor_lo); p.drawLine( x+kw-1, y, x+kw-1, y+keyHeight-2 ); if ( threeD ) { p.setPen(keycolor_lo.light()); p.drawLine( x+kw-2, y+keyHeight-2, x+kw-2, y+1 ); p.drawLine( x+kw-2, y+keyHeight-2, x+1, y+keyHeight-2 ); } if (xpm) { p.drawPixmap( x + 1, y + 2, QPixmap((const char**)xpm) ); } else { p.setPen(textcolor); p.drawText( x - 1, y, kw, keyHeight-2, AlignCenter, s ); } if ( threeD ) { p.setPen(keycolor_hi); p.drawLine( x, y, x+kw-1, y ); } // bottom p.setPen(keycolor_lo); p.drawLine( x, y+keyHeight-1, x+kw-1, y+keyHeight-1 ); } else { p.fillRect( x, y, kw, keyHeight, cg.background() ); } } x += kw; kw = defaultKeyWidth; k = getKey( kw ); } }}void Keyboard::mousePressEvent(QMouseEvent *e){ clearHighlight(); // typing fast? int i2 = ((e->x() - xoffs) * 2) / defaultKeyWidth; int j = (e->y() - picks->height()) / keyHeight; int k = keycode( i2, j, (const uchar **)((useOptiKeys) ? keyboard_opti : keyboard_standard) ); bool need_repaint = FALSE; unicode = -1; qkeycode = 0; if ( k >= 0x80 ) { if ( k == ShiftCode ) { shift = !shift; need_repaint = TRUE; } else if ( k == AltCode ){ alt = !alt; need_repaint = TRUE; } else if ( k == CapsCode ) { lock = !lock; need_repaint = TRUE; } else if ( k == CtrlCode ) { ctrl = !ctrl; need_repaint = TRUE; } else if ( k == 0224 /* Expand */ ) { useLargeKeys = !useLargeKeys; resizeEvent(0); repaint( TRUE ); // need it to clear first } else if ( k == 0225 /* Opti/Toggle */ ) { useOptiKeys = !useOptiKeys; resizeEvent(0); repaint( TRUE ); // need it to clear first } else { qkeycode = specialM[ k - 0x80 ].qcode; unicode = specialM[ k - 0x80 ].unicode; } } else { //due to the way the keyboard is defined, we know that //k is within the ASCII range, and can be directly mapped to //a qkeycode; except letters, which are all uppercase qkeycode = toupper(k); if ( shift^lock ) { if ( !isalpha( k ) ) { for ( unsigned i = 0; i < sizeof(shiftMap)/sizeof(ShiftMap); i++ ) if ( shiftMap[i].normal == k ) { unicode = shiftMap[i].shifted; qkeycode = unicode; break; } } else { unicode = toupper( k ); } } else { unicode = k; } } if ( unicode != -1 ) { if ( ctrl && unicode >= 'a' && unicode <= 'z' ) unicode = unicode - 'a'+1; modifiers = (shift ? Qt::ShiftButton : 0) | (ctrl ? Qt::ControlButton : 0) | (alt ? Qt::AltButton : 0);#if defined(Q_WS_QWS) || defined(_WS_QWS_) emit key( unicode, qkeycode, modifiers, true, false ); repeatTimer->start( 500 );#endif need_repaint = shift || alt || ctrl; shift = alt = ctrl = FALSE; KeyboardConfig *dc = picks->dc; if (dc) { if (qkeycode == Qt::Key_Backspace) { dc->input.remove(dc->input.last()); // remove last input dc->decBackspaces(); } else if ( k == 0226 || qkeycode == Qt::Key_Return || qkeycode == Qt::Key_Space || QChar(unicode).isPunct() ) { dc->input.clear(); dc->resetBackspaces(); } else { dc->add(QString(QChar(unicode))); dc->incBackspaces(); } } picks->repaint(); } pressedKey = k; if ( need_repaint ) { repaint( FALSE ); } else { QPainter p(this); drawKeyboard( p, pressedKey ); } if ( pressTid ) killTimer(pressTid); pressTid = startTimer(80); pressed = TRUE;}void Keyboard::mouseReleaseEvent(QMouseEvent*){ if ( pressTid == 0 ) clearHighlight();#if defined(Q_WS_QWS) || defined(_WS_QWS_) if ( unicode != -1 ) { emit key( unicode, qkeycode, modifiers, false, false ); repeatTimer->stop(); }#endif pressed = FALSE;}void Keyboard::timerEvent(QTimerEvent* e){ if ( e->timerId() == pressTid ) { killTimer(pressTid); pressTid = 0; if ( !pressed ) clearHighlight(); }}void Keyboard::repeat(){ repeatTimer->start( 150 ); emit key( unicode, qkeycode, modifiers, true, true );}void Keyboard::clearHighlight(){ if ( pressedKey >= 0 ) { int tmp = pressedKey; pressedKey = -1; QPainter p(this); drawKeyboard( p, tmp ); }}QSize Keyboard::sizeHint() const{ QFontMetrics fm=fontMetrics(); int keyHeight = fm.lineSpacing()+2; if (useOptiKeys) keyHeight += 1; return QSize( 320, keyHeight * 5 + picks->sizeHint().height() + 1 );}void Keyboard::resetState(){ picks->resetState();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -