Mega Code Archive

 
Categories / Delphi / Examples
 

Add style to your application implementing an easter egg

Title: Add style to your application implementing an easter egg Question: Many world class applications implement some easter egg to give its author(s) credit, so why not to use this feature in your own applications? Answer: An easter egg is some piece of code that executes only when the user uses some special keystrokes, they are used frequently to give credit to the author(s) of some program. For example in Delphis About box hold Shift + Alt and then type "team", you will expose an easter egg giving credit to Delphi Staff. To create your own easter egg take this steps: 1.- Start a new Project 2.- Create the following private fields: ... private FEgg: String; FCount: Integer; ... FCount holds a count of Keystrokes. FEgg holds the Keystrokes string. 3.- Create two constants ... const EE_CONTROL: TShiftState = [ssCtrl, ssAlt]; EASTER_EGG = 'SECRET'; .. EE_CONTROL contains the control keys that must be down when the user types the EASTER_EGG string 4.- In the OnCreate event of the form write procedure TForm1.FormCreate(Sender: TObject); begin FCount := 1; FEgg := EASTER_EGG; end; 5.- In the OnKeyDown event of the form write procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin //Are the correct control keys down? if Shift = EE_CONTROL then begin //was the proper key pressed? if Key = Ord(FEgg[FCount]) then begin //was this the last keystroke in the sequence? if FCount = Length(FEgg) then begin //Code of the easter egg ShowMessage('Add your own code here!'); //failure - reset the count FCount := 1; {} end else begin //success - increment the count Inc(FCount); end; end else begin //failure - reset the count FCount := 1; end; end; end; 6.- Finally set the Forms KeyPreview property to true. Now you just have to replace the ShowMessage with Something more creative, use your imagination!