Solved: keygrabber or keypress event should be used for a game?



  • Hello guys,

    I am trying to fetch key pressed for a game. I tried to use keypress and keyrelease event but as long as I am on the us-en local keyboard (MAC) the event is fired. When I switch to Arabic keyboard, event does not fire?

    I don't know how to use keygrabber, can any body help with a small example?

    thanks



  • I found the solution in the following site in German:
    "
    http://www.c-plusplus.de/forum/268851-full":http://www.c-plusplus.de/forum/268851-full

    however, the code does not allow getting keys when focus is lost. is there a way for this?
    thanks

    main.cpp

    @
    #include "keygrabber.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    keygrabber blub;
    blub.resize(200, 1);
    blub.show();

    //QObject::connect(blub.abbrechen, SIGNAL(clicked()), &app, SLOT(quit()));
    
    return app.exec();
    

    } @

    keygrabber.h
    @
    #ifndef KEYGRABBER_H
    #define KEYGRABBER_H

    #include <QApplication> // QT
    #include <QWidget> // QT
    #include <QKeyEvent> // QT

    #include <fstream> //ofstream C++

    class keygrabber : public QWidget
    {
    public:
    keygrabber(QWidget *parent = 0);

        int taste_pressed[2];
        bool sonderzeichen_b;
        std::string sonderzeichen;
    
        char itoc(int taste_i);
        void schreiben(char taste_c);
        void eintragen(int taste_i);
    
    protected:
        virtual void keyPressEvent(QKeyEvent *event);
        virtual void keyReleaseEvent(QKeyEvent *event);
    

    };

    #endif
    @

    keygrabber.cpp
    @
    #include "keygrabber.h"

    keygrabber::keygrabber(QWidget *parent)
    : QWidget(parent)
    {
    for (int i=0; i<2; i++){taste_pressed[i] = -1;}
    }

    void keygrabber::keyPressEvent(QKeyEvent *event)
    {
    int taste_i;
    taste_i = event->key();
    eintragen(taste_i);
    char taste_c;
    taste_c = itoc(taste_i);
    schreiben(taste_c);
    }

    void keygrabber::eintragen(int taste_i)//traegt gedrueckte tasten in das array taste_pressed[] ein; es duerfen max. 2 tasten gleichzeitig gedrueckt werden; fuer shift benoetigt
    {
    for (int i=0; i<2; i++)
    {
    if (taste_pressed[i] == -1)
    {
    taste_pressed[i] = taste_i;
    return;
    }
    }
    }

    char keygrabber::itoc(int taste_i)//korrigiert A zu a und konvertiert das char zu int; sonderzeichen werden anders geschrieben
    {
    if ((taste_i >=32)&&(taste_i <=126))//nur lesbare zeichen
    {
    bool shift=false, altgr=false;
    for (int i=0; i<2; i++)
    {
    if (taste_pressed[i] == 16777248){shift = true;}
    if (taste_pressed[i] == 16781571){altgr = true;}
    }
    sonderzeichen_b = false;
    if (true)//if (altgr == false)
    {
    if ((taste_i >= 65)&&(taste_i <= 90))
    {
    if (shift == false)
    {
    taste_i += 32;//grosse buchstaben aber kein shift!
    }
    }
    if ((taste_i >= 97)&&(taste_i <= 122))
    {
    if (shift == true)
    {
    taste_i -= 32;//kleine buchstaben trotz shift!
    }
    }
    }
    else//if (altgr == true)
    {

        }
        char taste_c;
        taste_c = (char) taste_i;
        return taste_c;
    }
    else
    {
        sonderzeichen_b = true;
        switch (taste_i)
        {
            case 16777223:
                sonderzeichen = "[DELETE]";
                return '\0';
            case 16777219:
                sonderzeichen = "[BACKSPACE]";
                return '\0';
            case 16777220:
                sonderzeichen = "[ENTER]";
                return '\0';
            case 16777217:
                sonderzeichen = "[TABULATOR]";
                return '\0';
        }
    }
    return '\0';
    

    }

    void keygrabber::schreiben(char taste_c)//schreibt die keygeloggten daten in eine datei
    {
    std::ofstream write;
    write.open("keylog.txt", std::ios_base::out|std::ios_base::app);
    if (sonderzeichen_b == false){write << taste_c;}
    else {write << sonderzeichen;}
    write.close();
    }

    void keygrabber::keyReleaseEvent(QKeyEvent *event)//traegt die zeichen wieder aus dem array taste_pressed[] aus
    {
    for (int i=0; i<2; i++)
    {
    if (taste_pressed[i] == event->key()){taste_pressed[i] = -1;}
    }
    }
    @



  • Problem found:

    In the GUI, I have a QTextBrowser when i gets the focus it does not other languages (ex. Arabic, Russian) Key press events


Log in to reply
 

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