-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
80 lines (73 loc) · 2.19 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QKeyEvent>
#include <iostream>
#ifdef Q_OS_WIN32
# include <windows.h>
#else
# include <X11/XKBlib.h>
#endif
std::pair<bool,bool> checkLock()
{
// platform dependent method of determining if CAPS LOCK is on
#ifdef Q_OS_WIN32 // MS Windows version
return GetKeyState(VK_CAPITAL) == 1;
#else // X11 version (Linux/Unix/Mac OS X/etc...)
Display * d = XOpenDisplay(static_cast<char*>(nullptr));
bool caps_state = false, num_state = false;
if (d)
{
unsigned n;
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
caps_state = (n & 0x01) == 1;
num_state = (n & 0x02) == 2;
}
return {caps_state, num_state};
#endif
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog widget;
QVBoxLayout layout(&widget);
QHBoxLayout passField;
QLabel label(((argc<2)?"Called for more-or-less nothing":argv[1]));
QLineEdit input;
QPushButton button("Submit"), showPass;
auto [caps,nums] = checkLock();
if(nums)
label.setText(label.text() + "\nHint: Num lock is on");
if(caps)
label.setText(label.text() + "\nHint: Caps lock is on");
input.setEchoMode(QLineEdit::EchoMode::Password);
showPass.setIcon(QIcon(":/inv"));
QObject::connect(&button,&QPushButton::clicked,&input,[&input]()
{
std::cout << input.text().toStdString() << std::endl;
exit(0);
});
QObject::connect(&input,&QLineEdit::returnPressed,&button,&QPushButton::click);
QObject::connect(&showPass,&QPushButton::clicked,&input,[&input,&showPass](){
if(input.echoMode() == QLineEdit::EchoMode::Password)
{
input.setEchoMode(QLineEdit::EchoMode::Normal);
showPass.setIcon(QIcon(":/vis"));
}
else
{
input.setEchoMode(QLineEdit::EchoMode::Password);
showPass.setIcon(QIcon(":/inv"));
}
});
layout.addWidget(&label);
passField.addWidget(&input);
passField.addWidget(&showPass);
layout.addLayout(&passField);
layout.addWidget(&button);
widget.show();
return a.exec();
}