Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to differentiate between hotkeys with registerHotKey()
Forum Updated to NodeBB v4.3 + New Features

How to differentiate between hotkeys with registerHotKey()

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 749 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hey there,
    I am trying to setup multiple global hotkeys for my application using RegisterHotKey() from "windows.h".
    So far I have successfully added hotkeys using the tutorial on this site: amin-ahmadi.com.

    Now I do not find anything about how to differentiate between multiple hotkeys using the eventfilter() method.
    My code looks like this:

    menu.h

    #ifndef MENU_H
    #define MENU_H
    
    #include <QWidget>
    #include "windows.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Menu; }
    QT_END_NAMESPACE
    
    class Menu : public QWidget
    {
        Q_OBJECT
    
    public:
        Menu(QWidget *parent = nullptr);
        ~Menu();
    
    protected:
        bool nativeEvent(const QByteArray &eventType, void *message, long *result); // From guide by amin-ahmadi.com
    
    private:
        Ui::Menu *ui;
        HWND desktop;
    
    };
    #endif // MENU_H
    

    menu.cpp

    #include "menu.h"
    #include "ui_menu.h"
    
    #include "windows.h"
    #include "QDebug"
    
    
    Menu::Menu(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Menu)
    {
        ui->setupUi(this);
    
        if (!RegisterHotKey((HWND)Menu::winId(), 100, MOD_ALT | MOD_SHIFT, 'D'))
        {
            qDebug() << "Can't register hotkey ALT+SHIFT+D";
        }
    
        if (!RegisterHotKey((HWND)Menu::winId(), 100, MOD_ALT | MOD_SHIFT, 'S'))
        {
            qDebug() << "Can't register hotkey ALT+SHIFT+S";
        }
    
    }
    
    Menu::~Menu()
    {
        delete ui;
    }
    
    bool Menu::nativeEvent(const QByteArray& eventType, void* message, long* result)
    {
        Q_UNUSED(eventType);
        Q_UNUSED(result);
        MSG* msg = static_cast<MSG*>(message);
        if (msg->message == WM_HOTKEY)
        {
            qDebug() << "Hotkey pressed!";
            true;
        }
        return false;
    }
    

    No matter which of the one I press I receive the "Hotkey pressed!" message in the debugger. How can I now differentiate between the two hotkeys being pressed.
    Thank you for your insight!

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Don't see any relation to Qt here but you should read the msdn documentation.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved