#Include Once "window9.bi"

ReDim Shared StructA__ACC(0) As ACCELERATORSTRUCT

Function AddKeyboardShortcut(ByVal hwnd As HWND,ByVal Syskey As Integer,ByVal Shortcut As integer, ByVal ID_Event As Integer)As HACCEL Export
  Dim As Integer UBOUND_=UBound(StructA__ACC)
  For a As Integer=0 To UBOUND_
    If StructA__ACC(a).hwnd=hwnd Then
      Dim As integer NACC=CopyAcceleratorTable(StructA__ACC(a).hAccel,0,0)
      Dim As ACCEL  AcceleratorRES(NACC)
      CopyAcceleratorTable(StructA__ACC(a).hAccel,@AcceleratorRes(0),NACC)
      DestroyAcceleratorTable(StructA__ACC(a).hAccel)
      For b As Integer=0 To NACC
        If AcceleratorRes(b).cmd=ID_Event Then
          AcceleratorRes(b).key=Shortcut
          AcceleratorRes(b).fVirt=FVIRTKEY Or Syskey
          StructA__ACC(a).hAccel=CreateAcceleratorTable(cast(LPACCEL,@AcceleratorRes(0)),NACC)
          GetAcceleratorInfo(@StructA__ACC(a))
          Return StructA__ACC(a).hAccel
        EndIf
      Next
      AcceleratorRes(NACC).cmd=ID_Event
      AcceleratorRes(NACC).key=Shortcut
      AcceleratorRes(NACC).fVirt=FVIRTKEY Or Syskey
      StructA__ACC(a).hAccel=CreateAcceleratorTable(cast(LPACCEL,@AcceleratorRes(0)),NACC+1)
      GetAcceleratorInfo(@StructA__ACC(a))
      Return StructA__ACC(a).hAccel
    EndIf
  Next
  If StructA__ACC(UBOUND_).hwnd<>0 Then
    UBOUND_+=1
    ReDim Preserve StructA__ACC(UBOUND_) As ACCELERATORSTRUCT
  EndIf
  Dim As ACCEL  Accelerator(0)
  Accelerator(0).cmd = ID_Event
  Accelerator(0).fVirt = FVIRTKEY Or Syskey
  Accelerator(0).key = Shortcut
  StructA__ACC(UBOUND_).hwnd=hwnd
  StructA__ACC(UBOUND_).hAccel=CreateAcceleratorTable(cast(LPACCEL,@Accelerator(0)),1)
  GetAcceleratorInfo(@StructA__ACC(UBOUND_))
  
  Return StructA__ACC(UBOUND_).hAccel

End Function
Function DeleteAllKeyboardShortcut(ByVal hwnd As HWND) As Integer Export
  Dim As Integer a=UBound(StructA__ACC)
  For b As Integer=0 To a
    If StructA__ACC(b).hwnd=hwnd Then
      Function = DestroyAcceleratorTable(StructA__ACC(b).hAccel)
      StructA__ACC(b).hAccel=0
      GetAcceleratorInfo(@StructA__ACC(b))
      StructA__ACC(b).hwnd=0
    EndIf
  Next
End Function
