diff --git a/src/session-widgets/lockcontent.cpp b/src/session-widgets/lockcontent.cpp index 65adcd2a..59cbd311 100644 --- a/src/session-widgets/lockcontent.cpp +++ b/src/session-widgets/lockcontent.cpp @@ -112,6 +112,14 @@ void LockContent::init(SessionBaseModel *model) } DConfigHelper::instance()->bind(this, SHOW_MEDIA_WIDGET, &LockContent::OnDConfigPropertyChanged); + + QString kbLayout = getCurrentKBLayout(); + if (!kbLayout.isEmpty() && !kbLayout.toLower().startsWith("us")) { + m_originalKBLayout = kbLayout; + qCInfo(DDE_SHELL) << "Original keyboard layout:" << m_originalKBLayout; + // 如果键盘布局有特殊设置,则切换到英文键盘布局,认证成功后恢复 + setKBLayout("us"); + } } void LockContent::initUI() @@ -184,8 +192,13 @@ void LockContent::initConnections() connect(m_model, &SessionBaseModel::userListChanged, this, &LockContent::onUserListChanged); connect(m_model, &SessionBaseModel::userListLoginedChanged, this, &LockContent::onUserListChanged); connect(m_model, &SessionBaseModel::authFinished, this, [this](bool successful) { - if (successful) + if (successful) { setVisible(false); + if (!m_originalKBLayout.isEmpty()) { + // 切换回原来的键盘布局 + setKBLayout(m_originalKBLayout); + } + } restoreMode(); }); connect(m_model, &SessionBaseModel::MFAFlagChanged, this, [this](const bool isMFA) { @@ -985,3 +998,26 @@ void LockContent::showShutdown() m_model->setCurrentModeState(SessionBaseModel::ModeStatus::ShutDownMode); m_model->setVisible(true); } + +QString LockContent::getCurrentKBLayout() const +{ + QProcess p; + p.start("/usr/bin/setxkbmap", {"-query"}); + p.waitForFinished(); + + const QString output = QString::fromUtf8(p.readAllStandardOutput()); + for (const QString &line : output.split('\n')) { + if (line.startsWith("layout:")) { + QString layout = line.section(':', 1).trimmed(); + return layout; + } + } + + return {}; +} + +void LockContent::setKBLayout(const QString &layout) +{ + qCDebug(DDE_SHELL) << "Set keyboard layout: " << layout; + QProcess::execute("/usr/bin/setxkbmap", { layout}); +} diff --git a/src/session-widgets/lockcontent.h b/src/session-widgets/lockcontent.h index b25ec2b6..dc6c8c56 100644 --- a/src/session-widgets/lockcontent.h +++ b/src/session-widgets/lockcontent.h @@ -101,6 +101,8 @@ public slots: void initFMAWidget(); void initUserListWidget(); void enableSystemShortcut(const QStringList &shortcuts, bool enabled, bool isPersistent); + QString getCurrentKBLayout() const; + void setKBLayout(const QString &layout); protected: SessionBaseModel *m_model = nullptr; @@ -133,6 +135,8 @@ public slots: bool m_MPRISEnable = false; bool m_showMediaWidget = false; bool m_hasResetPasswordDialog = false; + + QString m_originalKBLayout; }; #endif // LOCKCONTENT_H