{***********************************************************************}
{									}
{ __DefKyMnu  Add a _Key record to the linked list of default keys.	}
{									}
{ function  __DefKyMnu(   Key : _Keys;					}
{		       Action : byte;					}
{		       Motion : _MenuMotion) : _KeyPtr; 		}
{									}
{	      Key	Keystroke definition of type _Keys		}
{	      Action	One or more of the following constants, which	}
{			define an action taken upon the keystroke:	}
{									}
{			_KNOACTION_MNU	 No action (may have motion)	}
{			_KTRANSMIT_MNU	 Transmit menu on this key	}
{			_KABORT_MNU	 Transmit menu with error code	}
{			_KBEEP_MNU	 Beep on this key		}
{			_KDISABLE_MNU	 Ignore this key until enabled	}
{									}
{	      Motion	Movement of the menu highlight bar which is to	}
{			take place upon the keystroke:			}
{									}
{			_NoMove   Don't move the highlight bar		}
{			_Up	  To the next higher item, same column	}
{			_Down	  To the next lower item, same column	}
{			_Right	  Right to the next item, same row	}
{			_Left	  Left to the next item, same row	}
{			_Next	  To the next item in the list		}
{			_Previous To the previous item in the list	}
{			_First	  To the first item in the list 	}
{			_Last	  To the last item in the list		}
{			_NextCh   To the next item with a highlighted	}
{				  character defined by Key		}
{									}
{			Notice that _Goto is not allowed, since all	}
{			keys in the default list have a NIL _KeyItem.	}
{									}
{ Description __DefKyMnu  adds or replaces records in the list of	}
{	      default keystroke definitions.				}
{									}
{	      When __MakeMnu creates a new _Menu record, it		}
{	      creates a copy of the default list, then sets _MKeysPtr	}
{	      to point at the first element in the copied list.  The	}
{	      initial contents of the default list are set by the	}
{	      unit initialization code.  __DefKyMnu allows you to	}
{	      add or replace keys in the list, so that your		}
{	      customized defaults will be attached to any subse-	}
{	      quent menu you create.   __DelDKMnu  deletes keys 	}
{	      from the default list.  The functions __KeyMnu and	}
{	      __DelKyMnu  add and delete keys for a specific  menu	}
{	      only.							}
{									}
{	      __DefKyMnu searches the list for a character and		}
{	      scan code that matches those of Key.  If a match is	}
{	      found  the existing record is replaced, otherwise a	}
{	      new _Key record is added.  The function return value	}
{	      is a pointer to the newly created _Key, or NIL if an	}
{	      error occurred.						}
{									}
{	      On error _MenuError may be set to:  _InvalidKey		}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc. 1987		}
{_______________________________________________________________________}

function __DefKyMnu(Key : _Keys; Action : byte;
				 Motion : _MenuMotion) : _KeyPtr;

  var
    TempPtr,LastPtr : _KeyPtr;
    Found	    : boolean;
    ChSc	    : _KeySeq;

  begin {__DefKyMnu}

    __DefKyMnu := NIL;
    ChSc       := Unit_Key._KeyTable[Key];

    { Traverse the list of _Key records, if any, looking for a match:	}

    Found   := FALSE;
    TempPtr := DefaultKeyPtr_;	    { Point at the head of the list	}
    LastPtr := TempPtr;
    while ((TempPtr <> NIL) and (not Found)) do
      begin
	if (not ValidSig(TempPtr,KEYSIG_)) then
	   Exit;

	if (TempPtr^._KChSc._ChScan = ChSc._ChScan) then
	   Found := TRUE
	else
	   Found := FALSE;
	if (not Found) then
	   begin
	     LastPtr := TempPtr;
	     TempPtr := TempPtr^._NextKey
	   end
      end; { while }

      if (not Found) then	       { Add the default key record	}
	 begin
	   TempPtr := AddKey(NIL,_KeyTable[Key],Action,Motion);
	   if (DefaultKeyPtr_ = NIL) then
	      DefaultKeyPtr_ := TempPtr
	   else
	      LastPtr^._NextKey := TempPtr;
	   __DefKyMnu := TempPtr
	 end
      else			       { Replace the existing record	}
	 if (Motion = _Goto) then
	    SetMenuError(_InvalidKey)  { Can not move to a NIL item	}
	 else
	    with TempPtr^ do
	      begin
		_Action     := Action;	     { Only these fields can	}
		_Motion     := Motion;	     { logically be updated.	}
		__DefKyMnu  := TempPtr
	      end

  end;	{__DefKyMnu}
