📄 keyboardwidget_internal.cpp
字号:
wchar key = getKey( m_selection, m_modifier ); if( !isValidKey( key ) ) key = Key_Invalid; int old_modifier = m_modifier; switch( key ) { case Key_Invalid : break; case Key_Shift : m_modifier = ( m_modifier != Shift && m_modifier != Caps ) ? Shift : None; key = Key_Invalid; break; case Key_CapsLock : m_modifier = ( m_modifier != Caps ) ? Caps : None; key = Key_Invalid; break; case Key_Alt : m_modifier = ( m_modifier != Alt ) ? Alt : None; key = Key_Invalid; break; case Key_AltGr : m_modifier = ( m_modifier != AltGr ) ? AltGr : None; key = Key_Invalid; break; default : { if( m_modifier == Shift ) m_modifier = None; sendKey( key, autorepeat ); break; } } if( m_modifier != old_modifier ) { update(); }}void archos::KeyboardWidgetInternal::sendKey( wchar key, bool autorepeat ){ int ascii = key & 0x7f; int uni = QChar( key ).upper().unicode(); char buf[] = { key, '\0' }; m_ignore_event = true; QKeyEvent kpe( QEvent::KeyPress, uni, ascii, Qt::NoButton, QString( buf ), autorepeat ); QApplication::sendEvent( m_tgt, &kpe ); QKeyEvent kre( QEvent::KeyRelease, uni, ascii, Qt::NoButton, QString( buf ), autorepeat ); QApplication::sendEvent( m_tgt, &kre ); m_ignore_event = false; if( key == Key_Enter ) emit enterPressed();}void archos::KeyboardWidgetInternal::timerEvent( QTimerEvent * ){ wchar key = getKey( m_selection, m_modifier ); sendKey( key, true ); killTimers(); startTimer( 128 );}void archos::KeyboardWidgetInternal::keyPressEvent( QKeyEvent *evt ){ switch( evt->key() ) { case Qt::Key_Return : handleOkPressed( evt->isAutoRepeat() ); break; case Qt::Key_F4 : toggleModifier(); break; case Qt::Key_F6 : reset(); evt->ignore(); break; case Key_Left : case Key_Right : case Key_Up : case Key_Down : moveSelection( evt->key() ); break; case Qt::Key_PageUp : if( m_layout->type == TV ) { toggleArea(); } else { sendKey( Key_Right, evt->isAutoRepeat() ); } break; case Qt::Key_PageDown : if( m_layout->type == TV ) { toggleArea(); } else { sendKey( Key_Left, evt->isAutoRepeat() ); } break; default : evt->ignore(); break; }}void archos::KeyboardWidgetInternal::keyReleaseEvent( QKeyEvent *evt ){ switch( evt->key() ) { case Key_Left : case Key_Right : case Key_Up : case Key_Down : case Qt::Key_F4 : case Qt::Key_Return : case Qt::Key_PageUp : case Qt::Key_PageDown : break; default : evt->ignore(); break; }}bool archos::KeyboardWidgetInternal::eventFilter( QObject *, QEvent *evt ){ if( evt->type() != QEvent::KeyPress && evt->type() != QEvent::KeyRelease ) return false; if( !isVisible() ) return false; if( m_ignore_event ) return false; QKeyEvent *ke = (QKeyEvent*) evt; if( evt->type() == QEvent::KeyPress ) { keyPressEvent( ke ); } else { keyReleaseEvent( ke ); } return ke->isAccepted();}void archos::KeyboardWidgetInternal::moveSelection( int dir ){ const int cols = m_layout->cols; const int rows = m_layout->rows; int col = m_selection % cols; int row = m_selection / cols; int old_index = m_selection; int old_col = col; int i, max = ( dir == Key_Left || dir == Key_Right ) ? cols : rows; for( i = 0; i < max; i++ ) { switch( dir ) { case Key_Left : col--; if( col < 0 ) col += cols; break; case Key_Right : col++; if( col >= cols ) col -= cols; break; case Key_Up : row--; if( row < 0 ) row += rows; break; case Key_Down : row++; if( row >= rows ) row -= rows; break; } int index = row * cols + col; if( getKey( index, m_modifier ) == Key_Fused ) { if( dir == Key_Right ) { index++; } else { index--; } } if( isValidKey( getKey( index, m_modifier ) ) ) { // stop on the first valid key update( areaFor( m_selection ) ); m_selection = index; update( areaFor( m_selection ) ); break; } } // remember position where we moved from the area with special keys to standard keys or vice versa if( ( old_col >= cols - 1 && col < cols - 1 ) || ( old_col < cols - 1 && col >= cols - 1 ) ) { m_selection_bak = old_index; }}void archos::KeyboardWidgetInternal::mousePressEvent( QMouseEvent *evt ){ mouseMoveEvent( evt ); startTimer( 750 );}void archos::KeyboardWidgetInternal::mouseMoveEvent( QMouseEvent *evt ){ int index = indexAt( evt->pos().x(), evt->pos().y() ); if( index != m_selection ) { int old_selection = m_selection; m_selection = index; update( areaFor( old_selection ) ); update( areaFor( m_selection ) ); }}void archos::KeyboardWidgetInternal::mouseReleaseEvent( QMouseEvent *evt ){ int index = indexAt( evt->pos().x(), evt->pos().y() ); if( index < 0 ) return; if( isValidKey( getKey( index, m_modifier ) ) ) { handleOkPressed( false ); } killTimers();}QRect archos::KeyboardWidgetInternal::areaFor( int index ) const{ const int row = index / m_layout->cols; const int col = index % m_layout->cols; int cw = width() * 4 / ( 4 * m_layout->cols + 1 ); int ch = height() / m_layout->rows; cw &= ~1; int xofs; int extraw = 0; if( m_layout->type == Touchscreen ) { xofs = 2; int tofs = width() - m_layout->cols * cw; if( row == m_layout->rows - 1 ) { if( col == 4 ) { extraw = tofs - 2; } else if( col > 4 ) { xofs = tofs - 2; } } else if( row & 1 ) { xofs = tofs; } } else { xofs = ( width() - m_layout->cols * cw ) / 2; } QRect area( xofs + col * cw, row * ch + 1, cw + extraw, ch ); const int rowr = ( index + 1 ) / m_layout->cols; if( row == rowr && getKey( index + 1, m_modifier ) == Key_Fused ) area |= areaFor( index + 1 ); return area;}int archos::KeyboardWidgetInternal::indexAt( int xpos, int ypos ) const{ int cw = width() * 4 / ( 4 * m_layout->cols + 1 ); int ch = height() / m_layout->rows; cw &= ~1; int row = ( ypos - 1 ) / ch; row = QMIN( row, m_layout->rows - 1 ); int xofs; if( m_layout->type == Touchscreen ) { xofs = 2; int tofs = width() - m_layout->cols * cw; if( row == m_layout->rows - 1 ) { if( xpos > rect().width() / 2 ) { xofs = tofs - 2; } } else if( row & 1 ) { xofs = tofs; } } else { xofs = ( width() - m_layout->cols * cw ) / 2; } int col = ( xpos - xofs ) / cw; col = QMIN( col, m_layout->cols - 1 ); int index = row * m_layout->cols + col; if( getKey( index, m_modifier ) == Key_Fused ) index--; if( index >= 0 && index < m_layout->cols * m_layout->rows ) { return index; } else { return -1; }}void archos::KeyboardWidgetInternal::toggleModifier(){ switch( m_modifier ) { case None : if( m_layout->supported_modifiers & Shift ) { m_modifier = Shift; } else if( m_layout->supported_modifiers & Alt ) { m_modifier = Alt; } else { m_modifier = None; } break; case Shift : if( m_layout->supported_modifiers & Alt ) { m_modifier = Alt; } else { m_modifier = None; } break; case Alt : if( m_layout->supported_modifiers & AltGr ) { m_modifier = AltGr; } else { m_modifier = None; } break; case AltGr : m_modifier = None; break; } update();}void archos::KeyboardWidgetInternal::toggleArea(){ update( areaFor( m_selection ) ); SWAP( m_selection, m_selection_bak ); update( areaFor( m_selection ) );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -