Best practice for listener loop in system hotkey class



  • 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???
    }
    @



  • 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);
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.