Mirror

How to create an 'Easter Egg' in an application (Views: 100)


Problem/Question/Abstract:

How to create an 'Easter Egg' in an application

Answer:

1. Give the form a field of type String:


Match: string;


2. Declare a constant that represents the character sequence that needs to be typed in order
     for the Easter Egg to appear. For example:

const
  Target = ' abc ' #1;


(In this example, you have to type "a"  "b"  "c" and finally CTRL - A)


3. Set the forms KeyPreview property to True.


4. In the dialog's OnCreate event handler, do this:


procedure TMyAboutBox.FormCreate(Sender: TObject);
begin
  Match := ''
end;


5. In the dialog's OnKeyPress event handler, do this:


procedure TMyAboutBox.FormKeyPress(Sender: TObject; var Key: Char);
begin
  Match := Match + Key;
  if Pos(Match, Target) <> 1 then
    Match := ''
  else if Match = Target then
    ShowMessage('Congratulations')
end;

<< Back to main page