2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-06-10 16:50:54 +00:00
|
|
|
* Copyright (C) 2008 Volker Lanz (vl@fidra.de)
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "KeySequenceWidget.h"
|
|
|
|
|
|
2014-02-14 16:21:02 +00:00
|
|
|
#include <QMouseEvent>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <iostream>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
KeySequenceWidget::KeySequenceWidget(QWidget *parent, const KeySequence &seq)
|
2024-07-16 13:36:44 +00:00
|
|
|
: QPushButton(parent),
|
|
|
|
|
m_KeySequence(seq),
|
|
|
|
|
m_BackupSequence(seq),
|
|
|
|
|
m_Status(Stopped),
|
|
|
|
|
m_MousePrefix("mousebutton("),
|
|
|
|
|
m_MousePostfix(")"),
|
|
|
|
|
m_KeyPrefix("keystroke("),
|
|
|
|
|
m_KeyPostfix(")") {
|
2024-07-02 19:07:06 +00:00
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
updateOutput();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void KeySequenceWidget::setKeySequence(const KeySequence &seq) {
|
|
|
|
|
keySequence() = seq;
|
|
|
|
|
backupSequence() = seq;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
setStatus(Stopped);
|
|
|
|
|
updateOutput();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void KeySequenceWidget::mousePressEvent(QMouseEvent *event) {
|
|
|
|
|
event->accept();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (status() == Stopped) {
|
|
|
|
|
startRecording();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (m_KeySequence.appendMouseButton(event->button()))
|
|
|
|
|
stopRecording();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
updateOutput();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void KeySequenceWidget::startRecording() {
|
|
|
|
|
keySequence() = KeySequence();
|
|
|
|
|
setDown(true);
|
|
|
|
|
setFocus();
|
|
|
|
|
grabKeyboard();
|
|
|
|
|
setStatus(Recording);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void KeySequenceWidget::stopRecording() {
|
|
|
|
|
if (!keySequence().valid()) {
|
|
|
|
|
keySequence() = backupSequence();
|
|
|
|
|
updateOutput();
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
setDown(false);
|
|
|
|
|
focusNextChild();
|
|
|
|
|
releaseKeyboard();
|
|
|
|
|
setStatus(Stopped);
|
|
|
|
|
emit keySequenceChanged();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool KeySequenceWidget::event(QEvent *event) {
|
|
|
|
|
if (status() == Recording) {
|
|
|
|
|
switch (event->type()) {
|
|
|
|
|
case QEvent::KeyPress:
|
|
|
|
|
keyPressEvent(static_cast<QKeyEvent *>(event));
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case QEvent::MouseButtonRelease:
|
|
|
|
|
event->accept();
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case QEvent::ShortcutOverride:
|
|
|
|
|
event->accept();
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case QEvent::FocusOut:
|
|
|
|
|
stopRecording();
|
|
|
|
|
if (!valid()) {
|
|
|
|
|
keySequence() = backupSequence();
|
|
|
|
|
updateOutput();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return QPushButton::event(event);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void KeySequenceWidget::keyPressEvent(QKeyEvent *event) {
|
|
|
|
|
event->accept();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (status() == Stopped)
|
|
|
|
|
return;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (m_KeySequence.appendKey(event->key(), event->modifiers()))
|
|
|
|
|
stopRecording();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
updateOutput();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void KeySequenceWidget::updateOutput() {
|
|
|
|
|
QString s;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (m_KeySequence.isMouseButton())
|
|
|
|
|
s = mousePrefix() + m_KeySequence.toString() + mousePostfix();
|
|
|
|
|
else
|
|
|
|
|
s = keyPrefix() + m_KeySequence.toString() + keyPostfix();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
setText(s);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|