--  keyboard type-in routines for silicon (pretty picture) program

DIRECTORY
  StringDefs,
  InlineDefs,
  ppdefs,
  ppdddefs,ppddefs,
  multiGraphicsDefs;
ppkb: PROGRAM IMPORTS StringDefs, ppddefs, InlineDefs EXPORTS ppdefs =
  BEGIN OPEN StringDefs, ppdefs, ppdddefs,ppddefs, InlineDefs;

  ss: STRING ← [200];

  currentNumber: INTEGER ← 1;

  -- NOTE: all numbers are Octal !

  --key encodings (k field of keyEvent):
  --	0-11: "0" to "9" (add 60 for ascii)
  --	12-43: "A" to "Z" (add 67 for ascii upper case)
  --	44: ";" key
  --	45: "=" key
  --	46: "," key
  --	47: "-" key
  --	50: "." key
  --	51: "/" key
  --	52: "'" key
  --	53: "[" key
  --	54: "\" key
  --	55: "]" key
  --	56: "DONE" key (blank one below DEL)(UNdo)
  --	57: "←" key
  --	60: CR key
  --	61: LF key
  --	62: BS key
  --	63: DEL key
  --	64: space bar
  --	65: ESC
  --	66: blank to right of RET (not used)
  --	67: swat key (not used)
  --	70: TAB (not used, - a ctrl key instead)
  --	71: ???
  --	72: ???
  --	73: yellow mouse button up (release)
  --	74: ???
  --	75: ???
  --	76: ???
  --	77: No key (for use with mouse buttons)

  --ctrl key encodings (ctl field of keyEvent):
  --	1,2: Mouse buttons: 0-none, 1-red, 2-yellow, 3-blue
  --	4: CTRL
  --	10: TAB
  --	20: Shift (either)

lcAr:ARRAY[44B..77B] OF CHARACTER = [';,'=,',,'-,'.,'/,'',
	'[,'\,'],'?,'←,15C,'?,'?,'?,' ,'?,'?,'?,'?,'?,'?,'?,'?,'?,'?,'?];

ucAr:ARRAY[44B..77B] OF CHARACTER = [':,'+,'<,'`,'>,'?,'",
	'{,'|,'},'?,'↑,15C,'?,'?,'?,' ,'?,'?,'?,'?,'?,'?,'?,'?,'?,'?,'?];

digAr:ARRAY[0..9] OF CHARACTER = ['),'!,'@,'#,'$,'%,'~,'&,'*,'(];

  getchr: PUBLIC PROCEDURE RETURNS [ke: keyEvent] =
    BEGIN
    ke ← getKchr[];
    IF ke.ctl = 0 AND ke.k < 12B THEN currentNumber ← 0 ELSE currentNumber ← 1;
    WHILE ke.ctl = 0 AND ke.k < 12B DO
      currentNumber ← currentNumber*10 + ke.k; ke ← getKchr[]; ENDLOOP;
    RETURN;
    END;

  getNumber: PUBLIC PROCEDURE RETURNS [INTEGER] =
    BEGIN RETURN[currentNumber]; END;

  getstr: PUBLIC PROCEDURE [x, y: CARDINAL] RETURNS [BOOLEAN, STRING, keyEvent] =
    BEGIN
    s: STRING ← [200];
    k: keyEvent;
    flg: BOOLEAN;
    t: CARDINAL;
    flg ← FALSE;
    s.length ← 0;
    ss.length ← 0;
    UNTIL flg DO
      k ← getKchr[];
      t ← k.k;
      IF t < 12B THEN {IF BITAND[k.ctl,20B]=0 THEN AppendChar[s, t + '0]
		ELSE AppendChar[s, digAr[t]];}
      ELSE
        IF t < 44B THEN AppendChar[s, t + '7]
        ELSE
          IF t = 60B OR t = 65B OR t = 61B THEN flg ← TRUE
          ELSE
            IF t = 56B THEN RETURN[FALSE, ss, k]
            ELSE
	IF t = 63B THEN s.length ← 0
                          ELSE
	IF t = 62B THEN
                              BEGIN
                              IF s.length > 0 THEN s.length ← s.length - 1;
                              END
	ELSE IF t<65B THEN
	    BEGIN
		IF BITAND[k.ctl,20B]=0 THEN AppendChar[s, lcAr[t]]
		  ELSE AppendChar[s, ucAr[t]];

	    END;
      drawString[s, x, y];
      ENDLOOP;
    AppendString[ss, s];
    RETURN[TRUE, ss, k];
    END;

  END.