'--------------------------------------------------------------------------
' AMOS KEYBOARD DEFINER  
'--------------------------------------------------------------------------
' On start of a program, AREG(4) is loaded with the key map address! 
AKY=Areg(4)
'--------------------------------------------------------------------------
Screen Open 0,640,200,4,Hires : Curs Off 
Wind Open 1,0,0,80,12 : Window 0
Reserve Zone 10
Clw 
X=1 : Y=14
Print At(X,Y)+Border$(Zone$("  +++  ",1),2)
Print At(X,Y+3)+Border$(Zone$("   +   ",2),2)
Print At(X,Y+6)+Border$(Zone$("   -   ",3),2)
Print At(X,Y+9)+Border$(Zone$("  ---  ",4),2)
A=65 : XL=10 : YL=14 : L_DISPLAY : K_DISPLAY
YAL=5
AL$="... Please click or use arrows to change letter, press a key (with shift or alt) to set key, ESC erases all codes affected to A letter..."
INIT_MENU
On Menu Proc MENU_1,MENU_2
Do 
   On Menu On 
   '
   Repeat 
      Dec TAL : If TAL=0 : Locate 0,YAL : Centre Space$(72) : End If 
      For T=0 To 3 : Wait Vbl : Next 
      AL$=Mid$(AL$,2)+Left$(AL$,1) : Centre At(,2)+Left$(AL$,60)
      Dec C : If C=0 : K_DISPLAY : End If 
      Z=Mouse Zone : K$=Inkey$
   Until(Mouse Key=1 and Z<>0) or(K$<>"")
   ' -> Key effect
   If K$<>""
      Z=0
      S=Scancode : SH=Scanshift
      If S=$5F : KY_HELP : Z=5 : End If 
      If S=$4C or S=$4E : Z=2 : End If 
      If S=$4F or S=$4D or S=$41 : Z=3 : End If 
      If S=$45
         For X=0 To 3
            For Y=0 To $5F
               If Peek(AKY+$60*X+Y)=A
                  Poke AKY+$60*X+Y,0
               End If 
            Next 
         Next 
         Z=5
      End If 
      If SH and %11000000
         Z=5 : C=1
         If Asc(K$)=0
            Bell 20 : Centre At(,YAL)+"Key not affected!" : TAL=100
         Else 
            A=Asc(K$)
         End If 
      End If 
      If Z=0
         X=0
         If SH and %111 : X=1 : End If 
         If SH and %110000 : X=X+2 : End If 
         Poke AKY+X*$60+S,A
         C=1
      End If 
   End If 
   ' -> Mouse zone / change letter
   If(Z=1 or Z=2) and A<255 : Inc A : End If 
   If(Z=3 or Z=4) and A>2 : Dec A : End If 
   If Z=2 or Z=3
      C=1 : While Mouse Key : Wend 
   Else 
      C=2
   End If 
   L_DISPLAY
Loop 
'
Procedure K_DISPLAY
   Shared AKY,XL,YL,A,TAL
   Print At(XL,YL+1);"Scancode affected:"
   For Y=2 To 5 : Print At(XL,YL+Y);Space$(40); : Next 
   For X=0 To 3
      Print At(XL,YL+2+X);
      If X=0 : Print "-           "; : End If 
      If X=1 : Print "- Shift     "; : End If 
      If X=2 : Print "-      /Alt "; : End If 
      If X=3 : Print "- Shift/Alt "; : End If 
      For Y=0 To $5F
         If Peek(AKY+X*$60+Y)=A
            Print Hex$(Y,2);
         End If 
      Next 
   Next 
End Proc
'
Procedure L_DISPLAY
   Shared XL,YL,A
   A$="Ascii:"+Str$(A)+" "+Hex$(A,2)
   If A>=32 : A$=A$+" - Letter: "+Chr$(A) : End If 
   Print At(XL,YL);A$+"            "
End Proc
'
Procedure INIT_MENU
   Menu$(1)=" AMOS "
   Menu$(1,1)=" Help "
   Menu$(1,2)="------" : Menu Inactive(1,2)
   Menu$(1,3)=" Quit "
   Menu$(2)=" Editor "
   Menu$(2,1)=" Load Keymap     "
   Menu$(2,2)=" Save Keymap     "
   Menu$(2,3)=" Save As...      "
   Menu$(2,4)=" --------------- " : Menu Inactive(2,4)
   Menu$(2,5)=" Try keyboard    "
   Menu On 
End Proc
'
Procedure MENU_1
   If Choice(2)=1 : KY_HELP : End If 
   If Choice(2)=3 : Edit : End If 
   On Menu On 
End Proc
'
Procedure MENU_2
   Shared KY$
   If Choice(2)=1 : KY_LOAD : End If 
   If Choice(2)=2 : KY_SAVE : End If 
   If Choice(2)=3 : KY$="" : KY_SAVE : End If 
   If Choice(2)=5 : KY_TRY : End If 
   On Menu On 
End Proc
'
Procedure KY_LOAD
   Shared KY$,AKY
   KY$=Fsel$("*.key","","Please choose a keyboard")
   If KY$="" : Bell : Pop Proc : End If 
   Open In 1,KY$ : L=Lof(1) : Close 
   If L>$60*4+4 : Bell : Pop Proc : End If 
   Bload KY$,AKY
   K_DISPLAY
End Proc
'
Procedure KY_SAVE
   Shared KY$,AKY
   If KY$=""
      KY$=Fsel$("*.Key","","Please enter name")
      If KY$=""
         Bell : Pop Proc
      End If 
   End If 
   Bsave KY$,AKY To AKY+$60*4+4
End Proc
'
Procedure KY_TRY
   Home 
   Window 1
   Input "Enter what you want:";A$
   Clw 
   Window 0
End Proc
'
Procedure KY_HELP
   Window 1
   Clw : Print 
   Print " (1) First, you must choose the letter you want to change: use either ARROWS"
   Print "     or MOUSE. The letter and its ascii code are displayed."
   KY_WAIT
   Print " (2) If one (or more) code(s) is already affected to it, press ESC "
   Print "     to erase them."
   KY_WAIT
   Print " (3) Press a keyboard key to affect it to the letter displayed. The"
   Print "     scancode will be displayed below. You can press SHIFT or ALT or"
   Print "     both to have special combinations."
   KY_WAIT
   Print " (4) If you want to have more than one key for one ascii code, press"
   Print "     again (example: number on main keyboard and right one...)"
   KY_WAIT
   Print " (5) To try your keyboard, two ways: TRY KEYBOARD let you type"
   Print "     anything you want (it is an simple INPUT!) or leave AMIGA key down"
   Print "     and press the keyboard: this will select the letter..."
   KY_WAIT
   Print "     The changes done to the keyboard HAVE IMMEDIATE EFFECT! So if you"
   Print "     mess up totally your keyboard, using the editor won't be simple!!"
   Print "     You'll have to reload the default one with then file selector..."
   KY_WAIT
   Window 0
End Proc
'
Procedure KY_WAIT
   Print 
   Print "     Press any -redefined- key!"
   Clear Key : Wait Key 
   Clw : Print 
End Proc
'
Procedure UN_BUG
   Shared AKY
   Home 
   For X=0 To 3
      For Y=0 To 5
         For Z=0 To $F
            Print Hex$(Peek(AKY+X*$60+Y*$10+Z),2);
         Next 
         Print 
      Next 
      Print 
      Wait Key 
   Next 
End Proc
