[Qt-interest] Help to Implement Easter Egg after certain keypattern is entered

Darryl Hunter darryl.hunter at codersoft.com.au
Wed Feb 3 23:48:32 CET 2010


Hi Mike,

 

I found this code when I wanted to do a similar thing - I think on this list
but don't remember (it was quite a while ago):

 

 

*** EasterEgg.h ***

#include <QObject>

#include <QByteArray>

 

class EasterEgg : public QObject

{

      Q_OBJECT

 

public:

      EasterEgg(const char *magicWord, QObject *target, const char *slot);

 

      virtual bool eventFilter (QObject *obj, QEvent *event);

 

signals:

      void found();

 

private:

      QByteArray magic;

      int j;

      QObject *_aparent;

};

 

#endif // EASTEREGG_H

 

*** EasterEgg.cpp ***

#include "EasterEgg.h"

 

#include <QApplication>

#include <QKeyEvent>

 

EasterEgg::EasterEgg(const char *magicWord, QObject *target, const char
*slot)

      : magic (magicWord), j (0)

{

      qApp->installEventFilter (this);

      connect (this, SIGNAL (found()), target, slot);

      _aparent=target;

}

 

bool EasterEgg::eventFilter (QObject *obj, QEvent *event)

{

      if (_aparent==obj)

      {

            if (event->type() == QEvent::KeyPress)

            {

                  QKeyEvent *keyEvent = (QKeyEvent *)event;

                  int key=keyEvent->key();

                  if ((int)magic[j] == key)

                  {

                        j++;

                        if (j==magic.length())

                        {

                              emit (found());

                              j=0;

                        }

                  }

                  else

                  {

                        int right=j;

                        while (j > 0)

                        {

                              if ((int)magic[j-1] == key &&

                                    magic.left (j-1) == magic.mid (right - j
+ 1, j - 1))

                              {

                                    break;

                              }

                              j--;

                        }

                  }

            }

      }

      return FALSE;

}

 

To use it, you just need to do something like:

 

EasterEgg *saveEgg=new EasterEgg ("SAVE", this, SLOT
(saveEasterEggTriggered()));

 

Enjoy!

 

Darryl

 

 

  _____  

From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of Mike Short
Sent: Thursday, 4 February 2010 5:33 AM
To: qt-interest at trolltech.com
Subject: [Qt-interest] Help to Implement Easter Egg after certain keypattern
is entered

 

I have written a small app in Qt that consists of a QWiget that contains a
QGroupBox, and inside that there are 2 layouts, one that has 3 QPushButtons,
and the other has various QRadioButtons, and depending on what QRadioButton
is selected when a certain QPushButton is clicked, dictates how the utility
functions or what task it completes.

 

I would like to add in a special feature or Easter Egg that appears only
after the user has pushed certain keys on the keyboard in a certain order or
pattern.

 

Has anyone tried anything like this?

 

I would appreciate any ideas on how something like this can be done in an
efficient manner, or if anyone else has done this successfully,then what
approach did you take to achieve the desired results?

 

Thanks in advance..

 

-Mike Short

Fawkes Engineering, LLC

 

 

 

 

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.733 / Virus Database: 271.1.1/2664 - Release Date: 02/03/10
06:35:00


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100204/8221c941/attachment.html 


More information about the Qt-interest-old mailing list