提问人:lkilgoretrout 提问时间:11/11/2023 最后编辑:Abderrahmene Rayene Mihoublkilgoretrout 更新时间:11/11/2023 访问量:20
使用 bash 脚本在 RHEL systemd 服务中禁用 gsettings 键盘快捷键
Disabling gsettings keyboard shortcuts in a RHEL systemd service using a bash script
问:
我正在使用以下方法编写屏幕锁定实用程序:
- 红帽企业 Linux 7.6
- Qt Creator的
- C++
用户不应能够在锁屏界面上使用任何键盘快捷键。
到目前为止,我已经能够在Qt Creator中调试时停止所有键盘快捷键,方法是使用以下脚本(在Qt中使用)在显示锁定的登录页面之前禁用然后重新启用快捷键:execute()
#! /usr/bin/bash
shortcuts=$(gsettings list-keys org.gnome.desktop.wm.keybindings);
for x in $shortcuts; do
echo "Disabling $x";
gsettings set org.gnome.desktop.wm.keybindings $x [];
done
当我尝试在我编译的Qt程序中执行此脚本时,问题就出现了,该脚本使用其来自服务的别名调用。该服务正确启动并运行程序,查看日志我可以看到该命令正在执行,但我仍然能够使用键盘快捷键(通过按 + 我可以进入)并看到没有禁用任何内容。PATH
systemd
journalctl
echo "Disabling...
ALTF1applications->system tools->settings->devices
我写的 testService.service 看起来像这样(不要介意所有的评论,因为我正在做一些 run 实验):types
[Unit]
Description=testing screenlocker service
After=graphical.target
[Service]
#Type=simple
Type=exec
Environment="DISPLAY=:0"
#ExecStartPre=/home/user/Desktop/ScreenLocker/Source/disableShortcuts.sh
ExecStart=/home/user/Desktop/ScreenLocker/Source/build-ScreenLocker-Desktop_Qt_5_3_0_GCC_64bit-Debug/Screenlocker
TimeoutStartSec=0
[Install]
WantedBy=default.target
我不确定是什么权限或设置阻止了此系统服务修改用户的键盘映射。
Qt代码:
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// set current directory to build directory so QT looks for resources in the right spot
// (before it would look in the directory above where PWD was and would have issues
QDir::setCurrent(QCoreApplication::applicationDirPath());
// Disabling keyboard shortcuts
QString qstr = QApplication::applicationDirPath();
QDir qdir = QDir(qstr);
qdir.cdUp();
QString qstrpath = qdir.absoluteFilePath("disableShortcuts.sh");
qstrpath = QDir::toNativeSeparators(qstrpath);
system(qPrintable(qstrpath));
...
}
MainWindow.cpp,其中存储按钮上的逻辑以重置快捷方式:
void MainWindow::handleButton(){
QString usr = getUsername();
QString pswd = getPassword();
QCloseEvent *event;
if(usr == "Ray" && pswd == "Theon"){
// Reenabling keyboard shortcuts
QString qstr = QApplication::applicationDirPath();
QDir qdir = QDir(qstr);
qdir.cdUp();
QString qstrpath = qdir.absoluteFilePath("resetShortcuts.sh");
qstrpath = QDir::toNativeSeparators(qstrpath);
system(qPrintable(qstrpath));
setStyleSheet("background-color: blue");
event->setAccepted(true);
this->close();
}
}
...
我尝试过的事情:
- 使用 / 分别在屏幕锁之前和之后运行 disableShortcuts.sh 和 resetShortcuts.sh。我试过这个老大学,但想不通;如果事实证明,纯粹在 Qt/bash 中不可能做到这一点,那么我将重新审视这一点。
ExecStartPre
ExecStopPost
- 在 .service 文件的设置中使用 和 试图防止其他服务不抢占执行(您可以在日志中看到它被调度程序抢占了相当多,但它确实运行了)。
simple
oneshot
type
testService.service
- 将 bash 脚本的权限更改为 777,这样它们就不会遇到任何尝试执行它们的人/任何人的执行权限问题。
答: 暂无答案
评论