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. Best practice for listener loop in system hotkey class
Forum Updated to NodeBB v4.3 + New Features

Best practice for listener loop in system hotkey class

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.3k Views 1 Watching
  • 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.
  • W Offline
    W Offline
    weblife
    wrote on last edited by
    #1

    I am not exactly sure how to ask this question but I want this class I am building to be dynamic for other applications.

    I have a class that creates/listens/removes system hotkeys. Everything works well so far but I am trying to figure out how I should go about linking the class to functions of the parent without needing the parents data type.

    Here is what I have so far ("Github":https://github.com/CCi-BClark/SystemHotkey):
    @
    #define MOD_NOREPEAT 0x4000
    #define MOD_CONTROL 0x0002
    #define MOD_ALT 0x0001

    #include <string>
    #include <vector>
    #include "stdafx.h"

    class SystemHotkey {
    public:
    explicit SystemHotkey(void);
    ~SystemHotkey();

    void addKey(int keyID, UINT holdKey, char charKey);
    void connectFunction(int keyID, std::string func);
    void removeKey(int keyID);
    
    void beginHotkeys(void);
    void haltHotkeys(void);
    

    public:

    private:
    typedef std::pair<int, std::string> key;

    void listen(void);
    void run(int pos);
    
    bool running;
    MSG msg;
    std::vector<key> hotkeys;
    

    };

    SystemHotkey::SystemHotkey(void) {
    running = false;
    }

    SystemHotkey::~SystemHotkey(){

    }

    void SystemHotkey::removeKey(int keyID){
    for (int i = 0; i < (int)hotkeys.size(); ++i) {
    std::pair<int, std::string> val = hotkeys.at(i);
    if(val.first == keyID){
    UnregisterHotKey(NULL,val.first);
    hotkeys.erase(hotkeys.begin() + i);
    }
    }
    }

    void SystemHotkey::addKey(int keyID, UINT holdKey, char charKey){
    RegisterHotKey(NULL,keyID,holdKey | MOD_NOREPEAT,charKey);
    }

    void SystemHotkey::connectFunction(int keyID, std::string func){
    hotkeys.push_back(key(keyID,func));
    }

    void SystemHotkey::beginHotkeys(){
    running = true;
    listen();
    }

    void SystemHotkey::haltHotkeys(){
    running = false;
    }

    void SystemHotkey::listen(){
    while(running){
    GetMessage(&msg,NULL,0,0);
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    if (msg.message == WM_HOTKEY){
    for (int i = 0; i < (int)hotkeys.size(); ++i) {
    std::pair<int, std::string> val = hotkeys.at(i);
    if (msg.wParam == (unsigned int)val.first) run(i);
    }
    }
    }
    }

    void SystemHotkey::run(int pos){
    // Stuck here. How should I impliment function calls???
    }
    @

    Brandon Clark
    www.themindspot.com

    1 Reply Last reply
    0
    • W Offline
      W Offline
      weblife
      wrote on last edited by
      #2

      I really feel there is a better way to do this but for now I will use a signal to pass to a switch in my parent class. Here is the code (I left the keyPair value for now but will probably remove):

      @

      #define MOD_NOREPEAT 0x4000
      #define MOD_CONTROL 0x0002
      #define MOD_ALT 0x0001

      #include <QObject>
      #include <string>
      #include <vector>
      #include "stdafx.h"

      class SystemHotkey : public QObject {
      Q_OBJECT

      public:
      explicit SystemHotkey(QObject *parent = 0);
      ~SystemHotkey();

      void addKey(int keyID, UINT holdKey, char charKey);
      void connectFunction(int keyID, std::string func);
      void removeKey(int keyID);
      int getHotkey(int position);
      

      public slots:
      void beginHotkeys(void);
      void haltHotkeys(void);

      signals:
      void runHotkey(int position);

      private:
      typedef std::pair<int, std::string> key;

      void listen(void);
      void run(int pos);
      
      bool running;
      MSG msg;
      std::vector<key> hotkeys;
      

      SystemHotkey::SystemHotkey(QObject *parent) :
      QObject(parent) {
      running = false;
      }

      SystemHotkey::~SystemHotkey(){

      }

      void SystemHotkey::removeKey(int keyID){
      for (int i = 0; i < (int)hotkeys.size(); ++i) {
      std::pair<int, std::string> val = hotkeys.at(i);
      if(val.first == keyID){
      UnregisterHotKey(NULL,val.first);
      hotkeys.erase(hotkeys.begin() + i);
      }
      }
      }

      int SystemHotkey::getHotkey(int position){
      std::pair<int, std::string> val = hotkeys.at(position);
      return val.first;
      }

      void SystemHotkey::addKey(int keyID, UINT holdKey, char charKey){
      RegisterHotKey(NULL,keyID,holdKey | MOD_NOREPEAT,charKey);
      }

      void SystemHotkey::connectFunction(int keyID, std::string func){
      hotkeys.push_back(key(keyID,func));
      }

      void SystemHotkey::beginHotkeys(){
      running = true;
      listen();
      }

      void SystemHotkey::haltHotkeys(){
      running = false;
      }

      void SystemHotkey::listen(){
      while(running){
      GetMessage(&msg,NULL,0,0);
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      if (msg.message == WM_HOTKEY){
      for (int i = 0; i < (int)hotkeys.size(); ++i) {
      std::pair<int, std::string> val = hotkeys.at(i);
      if (msg.wParam == (unsigned int)val.first) run(i);
      }
      }
      }
      }

      void SystemHotkey::run(int pos){
      emit runHotkey(pos);
      }
      @

      Brandon Clark
      www.themindspot.com

      1 Reply Last reply
      0

      • Login

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