Skip to content

Commit

Permalink
Add basic global keyboard handling for windows with ALT+k #161
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirPorobic committed Apr 7, 2019
1 parent 705b373 commit 88bd51b
Show file tree
Hide file tree
Showing 17 changed files with 443 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/CMakeLists.txt
Expand Up @@ -34,15 +34,19 @@ set(KSNIP_SRCS
${CMAKE_SOURCE_DIR}/src/gui/SettingsDialog.cpp
${CMAKE_SOURCE_DIR}/src/gui/AboutDialog.cpp
${CMAKE_SOURCE_DIR}/src/gui/ImgurHistoryDialog.cpp
${CMAKE_SOURCE_DIR}/src/gui/keyboardShortcuts/GlobalKeyboardShortcut.cpp
${CMAKE_SOURCE_DIR}/src/gui/keyboardShortcuts/NativeKeyEventFilter.cpp
${CMAKE_SOURCE_DIR}/src/gui/keyboardShortcuts/AbstractKeyHandler.cpp
${CMAKE_SOURCE_DIR}/src/gui/keyboardShortcuts/KeyHandlerFactory.cpp
)


if (APPLE)
set(KSNIP_SRCS
${KSNIP_SRCS}
${CMAKE_SOURCE_DIR}/src/backend/imageGrabber/MacImageGrabber.cpp
${CMAKE_SOURCE_DIR}/src/backend/imageGrabber/MacWrapper.cpp
${CMAKE_SOURCE_DIR}/src/gui/snippingArea/MacSnippingArea.cpp
${CMAKE_SOURCE_DIR}/src/gui/keyboardShortcuts/DummyKeyHandler.cpp
)
elseif (UNIX)
set(KSNIP_SRCS
Expand All @@ -54,13 +58,15 @@ elseif (UNIX)
${CMAKE_SOURCE_DIR}/src/common/platform/PlatformChecker.cpp
${CMAKE_SOURCE_DIR}/src/common/platform/CommandRunner.cpp
${CMAKE_SOURCE_DIR}/src/gui/snippingArea/LinuxSnippingArea.cpp
${CMAKE_SOURCE_DIR}/src/gui/keyboardShortcuts/DummyKeyHandler.cpp
)
elseif (WIN32)
set(KSNIP_SRCS
${KSNIP_SRCS}
${CMAKE_SOURCE_DIR}/src/backend/imageGrabber/WinImageGrabber.cpp
${CMAKE_SOURCE_DIR}/src/backend/imageGrabber/WinWrapper.cpp
${CMAKE_SOURCE_DIR}/src/gui/snippingArea/WinSnippingArea.cpp
${CMAKE_SOURCE_DIR}/src/gui/keyboardShortcuts/WinKeyHandler.cpp
)
endif ()

Expand Down
2 changes: 1 addition & 1 deletion src/gui/ImgurHistoryDialog.h
Expand Up @@ -36,7 +36,7 @@ class ImgurHistoryDialog : public QDialog
Q_OBJECT
public:
explicit ImgurHistoryDialog();
~ImgurHistoryDialog();
~ImgurHistoryDialog() override;

private:
QVBoxLayout *mLayout;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/MainWindow.cpp
Expand Up @@ -134,6 +134,11 @@ void MainWindow::showEmpty()
QMainWindow::show();
}

void MainWindow::triggerNewDefaultCapture()
{
triggerNewCapture(mConfig->captureMode());
}

QMenu* MainWindow::createPopupMenu()
{
// Filtering out the option to hide main toolbar which should no be allowed.
Expand Down
7 changes: 4 additions & 3 deletions src/gui/MainWindow.h
Expand Up @@ -45,16 +45,17 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow(AbstractImageGrabber *imageGrabber, RunMode mode = RunMode::GUI);
void showEmpty();
void captureScreenshot(CaptureModes captureMode, bool capureCursor, int delay);
QMenu *createPopupMenu() override;
QSize sizeHint() const override;
void captureScreenshot(CaptureModes captureMode, bool captureCursor, int delay);

public slots:
void showCapture(const QPixmap &screenshot);
void triggerNewDefaultCapture();

protected:
void moveEvent(QMoveEvent *event) override;
void closeEvent(QCloseEvent *event) override;
QMenu *createPopupMenu() override;
QSize sizeHint() const override;

private:
AbstractImageGrabber *mImageGrabber;
Expand Down
20 changes: 20 additions & 0 deletions src/gui/keyboardShortcuts/AbstractKeyHandler.cpp
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "AbstractKeyHandler.h"
33 changes: 33 additions & 0 deletions src/gui/keyboardShortcuts/AbstractKeyHandler.h
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef KSNIP_ABSTRACTKEYHANDLER_H
#define KSNIP_ABSTRACTKEYHANDLER_H

class AbstractKeyHandler
{
public:
AbstractKeyHandler() = default;
virtual ~AbstractKeyHandler() = default;

virtual bool registerKey() = 0;
virtual bool isKeyPressed(void* message) = 0;
};

#endif //KSNIP_ABSTRACTKEYHANDLER_H
30 changes: 30 additions & 0 deletions src/gui/keyboardShortcuts/DummyKeyHandler.cpp
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "DummyKeyHandler.h"

bool DummyKeyHandler::registerKey()
{
return false;
}

bool DummyKeyHandler::isKeyPressed(void* message)
{
return false;
}
35 changes: 35 additions & 0 deletions src/gui/keyboardShortcuts/DummyKeyHandler.h
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef KSNIP_DUMMYKEYHANDLER_H
#define KSNIP_DUMMYKEYHANDLER_H

#include "AbstractKeyHandler.h"

class DummyKeyHandler : public AbstractKeyHandler
{
public:
DummyKeyHandler() = default;
~DummyKeyHandler() override = default;

bool registerKey() override;
bool isKeyPressed(void* message) override;
};

#endif //KSNIP_DUMMYKEYHANDLER_H
36 changes: 36 additions & 0 deletions src/gui/keyboardShortcuts/GlobalKeyboardShortcut.cpp
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "GlobalKeyboardShortcut.h"

GlobalKeyboardShortcut::GlobalKeyboardShortcut(QApplication &app) : mKeyHandler(nullptr)
{
mKeyHandler = KeyHandlerFactory::create();

mKeyHandler->registerKey();

auto keyEventFilter = new NativeKeyEventFilter(mKeyHandler);
connect(keyEventFilter, &NativeKeyEventFilter::triggered, this, &GlobalKeyboardShortcut::pressed);
app.installNativeEventFilter(keyEventFilter);
}

GlobalKeyboardShortcut::~GlobalKeyboardShortcut()
{
delete mKeyHandler;
}
43 changes: 43 additions & 0 deletions src/gui/keyboardShortcuts/GlobalKeyboardShortcut.h
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef KSNIP_GLOBALKEYBOARDSHORTCUT_H
#define KSNIP_GLOBALKEYBOARDSHORTCUT_H

#include <QApplication>
#include <QObject>

#include "NativeKeyEventFilter.h"
#include "KeyHandlerFactory.h"

class GlobalKeyboardShortcut : public QObject
{
Q_OBJECT
public:
explicit GlobalKeyboardShortcut(QApplication &app);
~GlobalKeyboardShortcut() override;

signals:
void pressed() const;

private:
AbstractKeyHandler *mKeyHandler;
};

#endif //KSNIP_GLOBALKEYBOARDSHORTCUT_H
35 changes: 35 additions & 0 deletions src/gui/keyboardShortcuts/KeyHandlerFactory.cpp
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "KeyHandlerFactory.h"

AbstractKeyHandler* KeyHandlerFactory::create()
{
#if defined(__APPLE__)
return new DummyKeyHandler;
#endif

#if defined(__linux__)
return new DummyKeyHandler;
#endif

#if defined(_WIN32)
return new WinKeyHandler;
#endif
}
41 changes: 41 additions & 0 deletions src/gui/keyboardShortcuts/KeyHandlerFactory.h
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef KSNIP_KEYHANDLERFACTORY_H
#define KSNIP_KEYHANDLERFACTORY_H

#if defined(__APPLE__)
#include "DummyKeyHandler.h"
#endif

#if defined(__linux__)
#include "DummyKeyHandler.h"
#endif

#if defined(_WIN32)
#include "WinKeyHandler.h"
#endif

class KeyHandlerFactory
{
public:
static AbstractKeyHandler* create();
};

#endif //KSNIP_KEYHANDLERFACTORY_H
33 changes: 33 additions & 0 deletions src/gui/keyboardShortcuts/NativeKeyEventFilter.cpp
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2019 Damir Porobic <https://github.com/damirporobic>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "NativeKeyEventFilter.h"

NativeKeyEventFilter::NativeKeyEventFilter(AbstractKeyHandler *keyHandler)
{
mKeyHandler = keyHandler;
}

bool NativeKeyEventFilter::nativeEventFilter(const QByteArray&, void* message, long*) {

if(mKeyHandler->isKeyPressed(message)) {
emit triggered();
}
return false;
}

0 comments on commit 88bd51b

Please sign in to comment.