2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2008 Volker Lanz <vl@fidra.de>
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "KeySequenceWidget.h"
|
|
|
|
|
|
2014-02-14 16:21:02 +00:00
|
|
|
#include <QMouseEvent>
|
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),
|
2025-05-22 22:22:20 +00:00
|
|
|
m_BackupSequence(seq)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
updateOutput();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void KeySequenceWidget::setKeySequence(const KeySequence &seq)
|
|
|
|
|
{
|
2025-01-05 00:44:04 +00:00
|
|
|
m_KeySequence = seq;
|
|
|
|
|
m_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-10-17 18:04:35 +00:00
|
|
|
void KeySequenceWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
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-10-17 18:04:35 +00:00
|
|
|
void KeySequenceWidget::startRecording()
|
|
|
|
|
{
|
2025-01-05 00:44:04 +00:00
|
|
|
m_KeySequence = KeySequence();
|
2024-07-02 19:07:06 +00:00
|
|
|
setDown(true);
|
|
|
|
|
setFocus();
|
|
|
|
|
grabKeyboard();
|
|
|
|
|
setStatus(Recording);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void KeySequenceWidget::stopRecording()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!keySequence().valid()) {
|
2025-01-05 00:44:04 +00:00
|
|
|
m_KeySequence = backupSequence();
|
2024-07-02 19:07:06 +00:00
|
|
|
updateOutput();
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
setDown(false);
|
|
|
|
|
focusNextChild();
|
|
|
|
|
releaseKeyboard();
|
|
|
|
|
setStatus(Stopped);
|
2024-10-21 04:08:10 +00:00
|
|
|
Q_EMIT keySequenceChanged();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool KeySequenceWidget::event(QEvent *event)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
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()) {
|
2025-01-05 00:44:04 +00:00
|
|
|
m_KeySequence = backupSequence();
|
2024-07-02 19:07:06 +00:00
|
|
|
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-10-17 18:04:35 +00:00
|
|
|
void KeySequenceWidget::keyPressEvent(QKeyEvent *event)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
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-10-17 18:04:35 +00:00
|
|
|
void KeySequenceWidget::updateOutput()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
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
|
|
|
}
|