Skip to content

Commit

Permalink
Github Issue #75: Added full feature to show/hide Remini with keyboar…
Browse files Browse the repository at this point in the history
…d shortcut "alt + j". This is now fully implemented
  • Loading branch information
AngryFender committed Feb 25, 2024
1 parent 7bc96ff commit bfae4ae
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
8 changes: 5 additions & 3 deletions iwindowapi.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#ifndef IWINDOWAPI_H
#define IWINDOWAPI_H

#include <QObject>
#include <Windows.h>

class IWindowApi{

class IWindowApi: public QObject
{
Q_OBJECT
public:

IWindowApi(){};
virtual ~IWindowApi()=default;
virtual void SetWindowsHookExInvoke(int idHook,
virtual HHOOK SetWindowsHookExInvoke(int idHook,
HOOKPROC lpfn,
HINSTANCE hmod,
DWORD dwThreadId)=0;
Expand Down
15 changes: 14 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
win = &WindowApi::instance();
ui->setupUi(this);
setup_views(this, *ui);

Expand Down Expand Up @@ -36,6 +37,9 @@ MainWindow::MainWindow(QWidget *parent)

QObject::connect(this,&MainWindow::startFileSearch,
view_handler.get(),&ViewsHandler::startFileSearchHandle);

QObject::connect(win, &WindowApi::showApp,
this,&MainWindow::showHideApp);
}

MainWindow::~MainWindow()
Expand Down Expand Up @@ -132,7 +136,16 @@ void MainWindow::viewChosenHandler(Qt::Key key)

void MainWindow::recentFilesHandler(bool show)
{
emit openRecentFilesDialog(show);
emit openRecentFilesDialog(show);
}

void MainWindow::showHideApp()
{
if(this->isMinimized()){
this->showNormal();
}else{
this->showMinimized();
}
}

void MainWindow::shiftTimerHandle()
Expand Down
3 changes: 3 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "./ui_mainwindow.h"
#include "transparentdialog.h"
#include "views_handler.h"
#include "windowapi.h"
#include <QTimer>
#include <QStyleFactory>

Expand All @@ -31,6 +32,7 @@ public slots:
void viewChosenHandler(Qt::Key key);
void recentFilesHandler(bool show);
private slots:
void showHideApp();
void shiftTimerHandle();

private:
Expand All @@ -39,6 +41,7 @@ private slots:
darkThemeState,
};

WindowApi * win;
QTimer *rightShiftTimer;
QTimer *leftShiftTimer;
Ui::MainWindow *ui;
Expand Down
42 changes: 40 additions & 2 deletions windowapi.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "windowapi.h"

void WindowApi::SetWindowsHookExInvoke(int idHook, HOOKPROC lpfn, HINSTANCE hmod, DWORD dwThreadId)
HHOOK WindowApi::SetWindowsHookExInvoke(int idHook, HOOKPROC lpfn, HINSTANCE hmod, DWORD dwThreadId)
{
keyboardProcHook = SetWindowsHookExW(idHook, (HOOKPROC)lpfn ,(HINSTANCE) hmod, (DWORD)dwThreadId);
return SetWindowsHookExW(idHook, lpfn , hmod, dwThreadId);
}

BOOL WindowApi::UnhookWindowsHookExInoke(HHOOK hhk)
Expand All @@ -22,5 +22,43 @@ void WindowApi::cleanUp()

LRESULT WindowApi::detectKeys(int code, WPARAM wParam, LPARAM lParam)
{
if(code >= 0){
bool isKeyDown = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN;
bool isKeyUp = wParam == WM_KEYUP || wParam == WM_SYSKEYUP;

KBDLLHOOKSTRUCT* kbStruct = (KBDLLHOOKSTRUCT*)lParam;

DWORD vkCode = kbStruct->vkCode;
if (vkCode == KEY_J || vkCode == KEY_ALT)
{
if (isKeyDown)
{
if(vkCode == KEY_J){
isKeyJPressedDown = true;
}

if(vkCode == KEY_ALT){
isKeyAltPressedDown = true;
}
}

if (isKeyUp){
if(vkCode == KEY_J){
isKeyJPressedDown = false;
}

if(vkCode == KEY_ALT){
isKeyAltPressedDown = false;
}
}

if(isKeyAltPressedDown && isKeyJPressedDown){
emit WindowApi::instance().showApp();
isKeyAltPressedDown = false;
isKeyJPressedDown = false;
return 1;
}
}
}
return instance().CallNextHookExInvoke(keyboardProcHook,code ,wParam,lParam) ;
}
20 changes: 17 additions & 3 deletions windowapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,42 @@
#include <Windows.h>
#include <QSharedPointer>
#include "iwindowapi.h"
#include <QDebug>
#include <QObject>

#define KEY_J 0x4A
#define KEY_ALT 164

class WindowApi : public IWindowApi
{
Q_OBJECT
private:
static HHOOK keyboardProcHook;
static inline HHOOK keyboardProcHook;
static inline bool isKeyJPressedDown = false;
static inline bool isKeyAltPressedDown = false;
WindowApi(){};
~WindowApi(){ cleanUp();};

public:
static WindowApi& instance(){
static WindowApi instance;
instance.SetWindowsHookExInvoke((int)WH_KEYBOARD_LL, detectKeys, 0, 0);

if(keyboardProcHook == nullptr){
keyboardProcHook = instance.SetWindowsHookExInvoke((int)WH_KEYBOARD_LL, detectKeys, 0, 0);
}
return instance;
};

static LRESULT CALLBACK detectKeys(int code, WPARAM wParam, LPARAM lParam);

void SetWindowsHookExInvoke(int idHook, HOOKPROC lpfn, HINSTANCE hmod, DWORD dwThreadId) override;
HHOOK SetWindowsHookExInvoke(int idHook, HOOKPROC lpfn, HINSTANCE hmod, DWORD dwThreadId) override;
BOOL UnhookWindowsHookExInoke(HHOOK hhk) override;

LRESULT CallNextHookExInvoke(HHOOK hhk, int nCode, WPARAM wParam, LPARAM lParam) override;

signals:
void showApp();

protected:
void cleanUp();
};
Expand Down

0 comments on commit bfae4ae

Please sign in to comment.