numlockmanager/wl_numlock.h

75 lines
1.5 KiB
C
Raw Normal View History

2023-09-01 14:13:57 +00:00
#ifndef WL_NUMLOCK_H
#define WL_NUMLOCK_H
#include <qglobal.h>
#ifdef Q_OS_LINUX
#include <QProcess>
#include <QDebug>
#include <fstream>
#include <iostream>
#include <QDir>
// wl - WindwsLinux
class WLKeyManager{
public:
WLKeyManager(){}
bool getNumLockState(){
if (path.length() == 0)
path = getNumLockFilePath();
std::ifstream file(path);
char str;
file >> str;
file.close();
return str == '1';
}
void pressNumLock(){
QProcess::execute("/bin/xdotool", {"key", "Num_Lock"});
}
private:
std::string path;
std::string getNumLockFilePath(){
QDir inputs("/sys/class/input/");
QString numlock;
foreach (QString dir, inputs.entryList({"event*"})){
QDir temp(inputs.absoluteFilePath(dir) + "/device/");
QStringList list = temp.entryList({"*::numlock"});
if (!list.empty()){
numlock = list[0];
break;
}
}
return QString("/sys/class/input/event3/device/" + numlock + "/brightness").toStdString();
}
};
#endif
#ifdef Q_OS_WIN
#include <windows.h>
class WLKeyManager{
public:
WLKeyManager(){}
bool getNumLockState(){
return (bool)GetKeyState(VK_NUMLOCK);
}
void pressNumLock(){
keybd_event(VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0);
}
};
#endif
#endif // WL_NUMLOCK_H