#include "frmKeyBoard.h"
frmKeyBoard::frmKeyBoard(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
this->setWindowTitle("屏幕键盘");
this->setWindowModality(Qt::WindowModal);
this->setAttribute(Qt::WA_DeleteOnClose);
MoveWidget* moveWidget = new MoveWidget();
moveWidget->setWidget(this);
this->initFrm();
this->initStyleSheet();
}
frmKeyBoard::~frmKeyBoard()
{
}
void frmKeyBoard::initFocusWidget(QWidget* widget)
{
m_focusWidget = widget;
}
void frmKeyBoard::initFrm()
{
ui.pushButton_closeKeyboard->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
m_letterKeys.clear();
m_NumberKeys.clear();
QList<QPushButton*> pbtns = this->findChildren<QPushButton*>();
foreach(QPushButton * pbtn, pbtns) {
pbtn->setAutoRepeat(true); //允许自动重复
pbtn->setAutoRepeatDelay(500);//设置重复操作的时延
if (pbtn->text() >= 'a' && pbtn->text() <= 'z') {
connect(pbtn, &QPushButton::clicked, this, &frmKeyBoard::slotKeyLetterButtonClicked);
m_letterKeys.push_back(pbtn);
}
else if (pbtn->text().toInt() > 0 && pbtn->text().toInt() <= 9 || pbtn->text() == "0") {
connect(pbtn, &QPushButton::clicked, this, &frmKeyBoard::slotKeyNumberButtonClicked);
m_NumberKeys.push_back(pbtn);
}
else{
connect(pbtn, &QPushButton::clicked, this, &frmKeyBoard::slotKeyButtonClicked);
}
}
m_mapSymbolKeys.insert("~", Qt::Key_AsciiTilde);
m_mapSymbolKeys.insert("`", Qt::Key_nobreakspace);
m_mapSymbolKeys.insert("-", Qt::Key_Minus);
m_mapSymbolKeys.insert("_", Qt::Key_Underscore);
m_mapSymbolKeys.insert("+", Qt::Key_Plus);
m_mapSymbolKeys.insert("=", Qt::Key_Equal);
m_mapSymbolKeys.insert(",", Qt::Key_Comma);
m_mapSymbolKeys.insert(".", Qt::Key_Period);
m_mapSymbolKeys.insert("/", Qt::Key_Slash);
m_mapSymbolKeys.insert("<", Qt::Key_Less);
m_mapSymbolKeys.insert(">", Qt::Key_Greater);
m_mapSymbolKeys.insert("?", Qt::Key_Question);
m_mapSymbolKeys.insert("[", Qt::Key_BracketLeft);
m_mapSymbolKeys.insert("]", Qt::Key_BracketRight);
m_mapSymbolKeys.insert("{", Qt::Key_BraceLeft);
m_mapSymbolKeys.insert("}", Qt::Key_BraceRight);
m_mapSymbolKeys.insert("|", Qt::Key_Bar);
m_mapSymbolKeys.insert("\\", Qt::Key_Backslash);
m_mapSymbolKeys.insert(":", Qt::Key_Colon);
m_mapSymbolKeys.insert(";", Qt::Key_Semicolon);
m_mapSymbolKeys.insert("\"", Qt::Key_QuoteLeft);
m_mapSymbolKeys.insert("'", Qt::Key_Apostrophe);
}
void frmKeyBoard::initStyleSheet()
{
QString qss;
qss += "QWidget{ background-color:rgb(26,26,26)}";
qss += "QPushButton{ color:white; background-color:rgb(51,51,51); height:60px; font-size:bold 15pt; border:1px solid rgb(26,26,26); border-radius: 0px; min-width:50px;}";
qss += "QPushButton:hover{background-color:rgb(229,229,229); color:black;}";
qss += "QPushButton:pressed,QPushButton:checked{background-color:rgb(0,118,215);}";
qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
qss += "#pushButton_space{min-width:500px;}";
qss += "#pushButton_backspace,#pushButton_shift{min-width:100px;}";
qss += "#pushButton_enter{min-width:120px;}";
qss += "#pushButton_tab,#pushButton_ctrl{min-width:70px;}";
qss += "#pushButton_capsLock{min-width:80px;}";
qss += "#pushButton_up{min-width:150px;}";
this->setStyleSheet(qss);
}
void frmKeyBoard::slotKeyButtonClicked()
{
QPushButton* pbtn = (QPushButton*)sender();
QString objectName = pbtn->objectName();
if (objectName == "pushButton_closeKeyboard") {
this->close();
return;
}
if (pbtn->text().contains("Backspace")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Backspace, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("Caps")) {
if (pbtn->isChecked()) {
for (auto pbtnKey : m_letterKeys) {
pbtnKey->setText(pbtnKey->text().toUpper());
}
}
else {
for (auto pbtnKey : m_letterKeys) {
pbtnKey->setText(pbtnKey->text().toLower());
}
}
}
else if(pbtn->text() == "Space") {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier, " ");
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Space, Qt::NoModifier, " ");
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("Tab")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Tab, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("Enter")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("Shift")) {
if (pbtn->isChecked()) {
for (auto pbtnKey : m_letterKeys) {
pbtnKey->setText(pbtnKey->text().toUpper());
}
}
else {
for (auto pbtnKey : m_letterKeys) {
pbtnKey->setText(pbtnKey->text().toLower());
}
}
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Shift, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Shift, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("Ctrl")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Control, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Control, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("Win")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Menu, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Menu, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("Alt")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Alt, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Alt, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("↑")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Up, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("↓")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Down, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("←")) {
QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier);
QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Left, Qt::NoModifier);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyPress);
QApplication::sendEvent(m_focusWidget->focusWidget(), &keyRelease);
}
else if (pbtn->text().contains("→")) {
评论2