home *** CD-ROM | disk | FTP | other *** search
- { Event.pas Version 2.2 86/08/25
-
- Author: Mike Babulic Compuserve ID: 72307,314 FIDO: 134/1
- 3827 Charleswood Dr. N.W.
- Calgary, Alberta,
- CANADA
- T2L 2C7
-
- This program demonstrates how to use procedure pointers to write an
- event-driven program. It doesn't do much more than illustrate the basic
- concepts BUT notice that there is no need for case statements. Thus
- it is possible to dynamically change the behavior of the program without
- extra coding. For example: suppose you had implemented a "pull down menu"
- system like in the Turbo Editor Toolbox (from Borland). You might want to
- activate & deactivate some choices in the sub-menus based on your last action.
- If the user choses to save a file your program should deactivate the "Save"
- menu choice; with case statements your program will have to consult a
- "file changed" flag to see if a "Save" should be done, but with procedure
- pointers you just assign "ofs(DoNothing)" to the proper entry in the
- ActionArray and assign "ofs(SaveProcedure)" when the file is changed.
- I've demonstrated this by "turning off" the '0' key after it is touched
- until the space bar is pressed.
- }
-
- {$i procparm.p} { Include Procedure Parameter Routines }
- {$i event.eng} { Include the "Event Engine" }
-
- {------------------------ Event Definitions --------------------------------}
-
- type
- KeyBoardEvents = (NoKeyboardEvent,
- Pressed_0,Pressed_1, Pressed_2, Pressed_3, Pressed_4,
- Pressed_5,Pressed_6, Pressed_7, Pressed_8, Pressed_9,
- Pressed_letter,
- Pressed_spacebar,
- Pressed_Quit );
-
- KeyActions = Array[NoKeyboardEvent..Pressed_Quit] of ProcPtr;
-
- var keyAction : ^KeyActions ABSOLUTE theAction;
-
- {------------------------------- Actions --------------------------------}
-
- procedure theSpacebar; forward;
-
- procedure n0;
- begin
- writeln('You pressed ZERO, you can''t use it again until you press the space bar');
- keyAction^[Pressed_0] := ofs(DoNothing);
- keyAction^[Pressed_spacebar] := ofs(theSpacebar);
- end;
-
- procedure n1;
- begin
- writeln('You pressed ONE');
- end;
-
- procedure n2;
- begin
- writeln('You pressed TWO');
- end;
-
- procedure n3;
- begin
- writeln('You pressed THREE');
- end;
-
- procedure n4;
- begin
- writeln('You pressed FOUR');
- end;
-
- procedure n5;
- begin
- writeln('You pressed FIVE');
- end;
-
- procedure n6;
- begin
- writeln('You pressed SIX');
- end;
-
- procedure n7;
- begin
- writeln('You pressed SEVEN');
- end;
-
- procedure n8;
- begin
- writeln('You pressed EIGHT');
- end;
-
- procedure n9;
- begin
- writeln('You pressed NINE');
- end;
-
- procedure letter;
- begin
- writeln('You Pressed a LETTER');
- end;
-
- procedure theSpacebar;
- begin
- writeln('You can use the ZERO key again');
- keyAction^[Pressed_0] := ofs(n0);
- keyAction^[Pressed_spacebar] := ofs(DoNothing);
- end;
-
- {------------------------------ Action Array --------------------------------}
-
- function KeyboardActions: ActionPtr;
- begin
- ACTIONS;
- inline( >13 { Thirteen Events }
- /n0 /n1 /n2 /n3 /n4 /n5 /n6 /n7 /n8 /n9
- /letter
- /DoNothing
- /Quit
- );
- end;
-
- {----------------------- GetNextEvent Procedure ----------------------------}
-
- procedure KeyBoardEvent(var theEvent: integer);
- var
- e: KeyboardEvents ABSOLUTE theEvent;
- c: Char;
- begin
- theEvent := 0;
- if keypressed then
- begin
- read(kbd,c);
- if c = #027 then
- begin {handle extended Ascii codes}
- read(kbd,c);
- e := Pressed_Quit;
- end
- else
- begin
- if (c>='0') and (c<='9') then
- theEvent := ord(Pressed_0) + ord(c) - ord('0')
- else if (c>='a') and (c<='z') or (c>='A') and (c<='Z') then
- e := Pressed_letter
- else if (c=' ') then
- e := Pressed_spacebar
- end;
- end;
- end;
-
- {----------------------------------------------------------------------------}
-
- begin
- ClrScr;
- gotoXY(1,1);
- writeln;
- writeln(' Event Driven Programming Example -- by Mike Babulic');
- writeln(' ---------------------------------------------------');
- writeln;
- writeln(' Press any Function Key to Quit');
- writeln(' ------------------------------');
- writeln;
- window(1,whereY,80,25); gotoXY(1,1);
- DoEvents(ofs(KeyboardEvent),KeyboardActions,ofs(MissingEvent));
- end.