home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff339.lzh / PCQ / Examples / DeadKeys.p < prev    next >
Text File  |  1990-03-19  |  4KB  |  190 lines

  1. Program DeadKeys;
  2.  
  3. {
  4.     This program simply tests the DeadKeyConvert() function,
  5. which in turn exercises the RawKeyConvert() function.  Press keys
  6. with the window that's opened is active, and this program will
  7. print the converted raw keys to the standard output.
  8. }
  9.  
  10. {$I ":Include/Exec.i" for Forbid, Permit and library things }
  11. {$I ":Include/Ports.i" for the Message stuff }
  12. {$I ":Include/ExecIO.i"}
  13. {$I ":Include/ExecIOUtils.i"}
  14. {$I ":Include/Intuition.i" for window business }
  15. {$I ":Include/InputEvent.i"}
  16. {$I ":Include/ConsoleUtils.i" for Open and CloseConsoleDevice}
  17. {$I ":Include/ConsoleIO.i"}
  18. {$I ":Include/DeadKeyConvert.i" for DeadKeyConvert}
  19.  
  20. var
  21.     w  : WindowPtr;
  22.     s  : ScreenPtr;
  23.  
  24. Function OpenTheScreen : Boolean;
  25. var
  26.     ns : NewScreenPtr;
  27. begin
  28.     new(ns);
  29.     with ns^ do begin
  30.     LeftEdge := 0;
  31.     TopEdge  := 0;
  32.     Width    := 640;
  33.     Height   := 200;
  34.     Depth    := 2;
  35.     DetailPen := 3;
  36.     BlockPen  := 2;
  37.     ViewModes := 32768;
  38.     SType     := CUSTOMSCREEN_f;
  39.     Font      := nil;
  40.     DefaultTitle := "Press ESC to End the Demonstration";
  41.     Gadgets   := nil;
  42.     CustomBitMap := nil;
  43.     end;
  44.     s := OpenScreen(ns);
  45.     dispose(ns);
  46.     OpenTheScreen := s <> nil;
  47. end;
  48.  
  49. Function OpenTheWindow : Boolean;
  50. var
  51.     nw : NewWindowPtr;
  52. begin
  53.     new(nw);
  54.     with nw^ do begin
  55.     LeftEdge := 0;
  56.     TopEdge := 2;
  57.     Width := 640;
  58.     Height := 198;
  59.  
  60.     DetailPen := -1;
  61.     BlockPen  := -1;
  62.     IDCMPFlags := RAWKEY_f;
  63.     Flags := SMART_REFRESH_f + ACTIVATE_f +
  64.             BORDERLESS_f + BACKDROP_f;
  65.     FirstGadget := Nil;
  66.     CheckMark := Nil;
  67.     Title := "";
  68.     Screen := s;
  69.     BitMap := Nil;
  70.     MinWidth := 0;
  71.     MaxWidth := -1;
  72.     MinHeight := 0;
  73.     MaxHeight := -1;
  74.     WType := CUSTOMSCREEN_f;
  75.     end;
  76.  
  77.     w := OpenWindow(nw);
  78.     dispose(nw);
  79.     OpenTheWindow := w <> nil;
  80. end;
  81.  
  82. var
  83.     IMessage    : IntuiMessagePtr;
  84.     Buffer    : Array [0..9] of Char;
  85.     Length    : Integer;
  86.     Leave    : Boolean;
  87.     WriteReq    : IOStdReqPtr;
  88.     WritePort    : MsgPortPtr;
  89.  
  90. Procedure OpenEverything;
  91. var
  92.     Error : Short;
  93. begin
  94.     OpenConsoleDevice;
  95.     if OpenTheScreen then begin
  96.     if OpenTheWindow then begin
  97.         WritePort := CreatePort(Nil, 0);
  98.         if WritePort <> Nil then begin
  99.         WriteReq := CreateStdIO(WritePort);
  100.         if WriteReq <> Nil then begin
  101.             WriteReq^.ioData := Address(w);
  102.             WriteReq^.ioLength := SizeOf(Window);
  103.             Error := OpenDevice("console.device", 0,
  104.             IORequestPtr(WriteReq), 0);
  105.             if Error = 0 then
  106.             return;
  107.             DeleteStdIO(WriteReq);
  108.             Writeln('Could not open the console.device');
  109.         end else
  110.             Writeln('Could not allocate memory');
  111.         DeletePort(WritePort);
  112.         end else
  113.         Writeln('Could not allocate a message port');
  114.         CloseWindow(w);
  115.     end else
  116.         Writeln('Could not open the window');
  117.     CloseScreen(s);
  118.     end else
  119.     Writeln('Could not open the screen');
  120.     CloseConsoleDevice;
  121.     Exit(20);
  122. end;
  123.  
  124. Procedure CloseEverything;
  125. begin
  126.     CloseDevice(IORequestPtr(WriteReq));
  127.     DeleteStdIO(WriteReq);
  128.     DeletePort(WritePort);
  129.     CloseWindow(w);
  130.     CloseScreen(s);
  131.     CloseConsoleDevice;
  132. end;
  133.  
  134. Procedure ConvertControl;
  135. begin
  136.     case Ord(Buffer[0]) of
  137.       8 : ConPutStr(WriteReq, "\b\cP");
  138.      13 : ConPutStr(WriteReq, "\n\cL");
  139.      127 : ConPutStr(WriteReq, "\cP");
  140.     else
  141.     ConPutChar(WriteReq, Buffer[0]);
  142.     end;
  143. end;
  144.  
  145. Procedure ConvertTwoChar;
  146. begin
  147.     case Buffer[1] of
  148.       'A'..'D' : ConWrite(WriteReq, Adr(Buffer), 2);
  149.     end;
  150. end;
  151.  
  152. begin
  153.     OpenEverything;
  154.     Leave := False;
  155.     repeat
  156.     IMessage := IntuiMessagePtr(WaitPort(w^.UserPort));
  157.     IMessage := IntuiMessagePtr(GetMsg(w^.UserPort));
  158.     if IMessage^.Class = RAWKEY_f then begin
  159.         if IMessage^.Code < 128 then begin { Key Down }
  160.         Length := DeadKeyConvert(IMessage, Adr(Buffer), 10, Nil);
  161.         case Length of
  162.           -MaxInt..-1 : Writeln('DeadKeyConvert error ', Length);
  163.            1 : if Buffer[0] = '\e' then
  164.                Leave := True
  165.             else begin
  166.                 if (Buffer[0] < ' ') or
  167.                 (Ord(Buffer[0]) > 126) then
  168.                 ConvertControl
  169.                 else begin
  170.                 Buffer[2] := Buffer[0];
  171.                 Buffer[0] := '\c';
  172.                 Buffer[1] := '@'; { Insert }
  173.                 ConWrite(WriteReq, Adr(Buffer), 3);
  174.                 end;
  175.             end;
  176.            2 : ConvertTwoChar;
  177.         end;
  178.         end;
  179.     end else
  180.         Leave := True;
  181.     ReplyMsg(MessagePtr(IMessage));
  182.     until Leave;
  183.     Forbid;
  184.     repeat
  185.     IMessage := IntuiMessagePtr(GetMsg(w^.UserPort));
  186.     until IMessage = nil;
  187.     Permit;
  188.     CloseEverything;
  189. end.
  190.