home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / EVENT.ZIP / EVENT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-09-05  |  4.5 KB  |  166 lines

  1. { Event.pas              Version 2.2  86/08/25
  2.  
  3.  Author: Mike Babulic    Compuserve ID: 72307,314   FIDO: 134/1
  4.          3827 Charleswood Dr. N.W.
  5.          Calgary, Alberta,
  6.          CANADA
  7.          T2L 2C7
  8.  
  9.  This program demonstrates how to use procedure pointers to write an
  10.  event-driven program. It doesn't do much more than illustrate the basic
  11.  concepts BUT notice that there is no need for case statements. Thus
  12.  it is possible to dynamically change the behavior of the program without
  13.  extra coding. For example: suppose you had implemented a "pull down menu"
  14.  system like in the Turbo Editor Toolbox (from Borland). You might want to
  15.  activate & deactivate some choices in the sub-menus based on your last action.
  16.  If the user choses to save a file your program should deactivate the "Save"
  17.  menu choice; with case statements your program will have to consult a
  18.  "file changed" flag to see if a "Save" should be done, but with procedure
  19.  pointers you just assign "ofs(DoNothing)" to the proper entry in the
  20.  ActionArray and assign "ofs(SaveProcedure)" when the file is changed.
  21.  I've demonstrated this by "turning off" the '0' key after it is touched
  22.  until the space bar is pressed.
  23. }
  24.  
  25. {$i procparm.p}     { Include Procedure Parameter Routines }
  26. {$i event.eng}      { Include the "Event Engine" }
  27.  
  28. {------------------------ Event Definitions --------------------------------}
  29.  
  30. type
  31.   KeyBoardEvents = (NoKeyboardEvent,
  32.                     Pressed_0,Pressed_1, Pressed_2, Pressed_3, Pressed_4,
  33.                     Pressed_5,Pressed_6, Pressed_7, Pressed_8, Pressed_9,
  34.                     Pressed_letter,
  35.                     Pressed_spacebar,
  36.                     Pressed_Quit );
  37.  
  38.    KeyActions = Array[NoKeyboardEvent..Pressed_Quit] of ProcPtr;
  39.  
  40. var keyAction : ^KeyActions  ABSOLUTE  theAction;
  41.  
  42. {------------------------------- Actions --------------------------------}
  43.  
  44. procedure theSpacebar; forward;
  45.  
  46. procedure n0;
  47.   begin
  48.     writeln('You pressed ZERO, you can''t use it again until you press the space bar');
  49.     keyAction^[Pressed_0]        := ofs(DoNothing);
  50.     keyAction^[Pressed_spacebar] := ofs(theSpacebar);
  51.   end;
  52.  
  53. procedure n1;
  54.   begin
  55.     writeln('You pressed ONE');
  56.   end;
  57.  
  58. procedure n2;
  59.   begin
  60.     writeln('You pressed TWO');
  61.   end;
  62.  
  63. procedure n3;
  64.   begin
  65.     writeln('You pressed THREE');
  66.   end;
  67.  
  68. procedure n4;
  69.   begin
  70.     writeln('You pressed FOUR');
  71.   end;
  72.  
  73. procedure n5;
  74.   begin
  75.     writeln('You pressed FIVE');
  76.   end;
  77.  
  78. procedure n6;
  79.   begin
  80.     writeln('You pressed SIX');
  81.   end;
  82.  
  83. procedure n7;
  84.   begin
  85.     writeln('You pressed SEVEN');
  86.   end;
  87.  
  88. procedure n8;
  89.   begin
  90.     writeln('You pressed EIGHT');
  91.   end;
  92.  
  93. procedure n9;
  94.   begin
  95.     writeln('You pressed NINE');
  96.   end;
  97.  
  98. procedure letter;
  99.   begin
  100.     writeln('You Pressed a LETTER');
  101.   end;
  102.  
  103. procedure theSpacebar;
  104.   begin
  105.     writeln('You can use the ZERO key again');
  106.     keyAction^[Pressed_0]        := ofs(n0);
  107.     keyAction^[Pressed_spacebar] := ofs(DoNothing);
  108.   end;
  109.  
  110. {------------------------------ Action Array --------------------------------}
  111.  
  112. function KeyboardActions: ActionPtr;
  113.   begin
  114.     ACTIONS;
  115.     inline( >13  { Thirteen Events }
  116.       /n0 /n1 /n2 /n3 /n4 /n5 /n6 /n7 /n8 /n9
  117.       /letter
  118.       /DoNothing
  119.       /Quit
  120.     );
  121.   end;
  122.  
  123. {----------------------- GetNextEvent Procedure ----------------------------}
  124.  
  125. procedure KeyBoardEvent(var theEvent: integer);
  126.   var
  127.     e: KeyboardEvents ABSOLUTE theEvent;
  128.     c: Char;
  129.   begin
  130.     theEvent := 0;
  131.     if keypressed then
  132.     begin
  133.       read(kbd,c);
  134.       if c = #027 then
  135.       begin {handle extended Ascii codes}
  136.         read(kbd,c);
  137.         e := Pressed_Quit;
  138.       end
  139.       else
  140.       begin
  141.         if (c>='0') and (c<='9') then
  142.           theEvent := ord(Pressed_0) + ord(c) - ord('0')
  143.         else if (c>='a') and (c<='z') or (c>='A') and (c<='Z') then
  144.           e := Pressed_letter
  145.         else if (c=' ') then
  146.           e := Pressed_spacebar
  147.       end;
  148.     end;
  149.   end;
  150.  
  151. {----------------------------------------------------------------------------}
  152.  
  153. begin
  154.   ClrScr;
  155.   gotoXY(1,1);
  156.   writeln;
  157.   writeln('            Event Driven Programming Example -- by Mike Babulic');
  158.   writeln('            ---------------------------------------------------');
  159.   writeln;
  160.   writeln('                    Press any Function Key to Quit');
  161.   writeln('                    ------------------------------');
  162.   writeln;
  163.   window(1,whereY,80,25);  gotoXY(1,1);
  164.   DoEvents(ofs(KeyboardEvent),KeyboardActions,ofs(MissingEvent));
  165. end.
  166.