
' (c) opk44 2011
' version 0.1.0 - 2011-10-20
' Использовано "Appendix A - Virtual Key codes" AkelPad Plugins Manual (Surveyor)
'
' Usage:
' Call("Scripts::Main", 1, "MacroDecoder.vbs")

' Known BUGS:
' 1) Работает только на Win2000/WinXP (на Vista/Win7 не проверялось, но, по-видимому, работать не будет)
' 2) Инициализация директории в диалоге выбора файла-макроса иногда не срабатывает (вместо указанной
'    стартовой папки макросов можем оказаться в папке, указанной в LastDirectory (см. "AkelPad.ini")
'
' Примеч.: 1) Для разбора результатов предпочтительнее моноширинный шрифт и отключенный режим переноса слов;
' 2) Макрос содержит информацию о нажатой клавише, а не о букве, поэтому и "Ы" и "S", кодируются одинаково.
'    (отслеживается по комбинациям переключения раскладки)

Option Explicit

 Const ForReading = 1, ForWriting = 2, ForAppending = 8

 Dim dic_f, dic_k
 Dim WM_COMMAND, IDM_FILE_NEW, hMainWnd
 Dim i, fn_macro, fsi, fi_size, fi, VbCrLf
 Dim objDialog, intResult, pAkelDir
 Dim keyword, flag_key, myitem, myflag

'--------- СЛОВАРИ флагов и ключей ----------
' Словарь флагов (состояний нажато/отпущено)
Set dic_f = CreateObject("Scripting.Dictionary")
dic_f.add &H00, "{0x00}-{normal key was press    (down)}"
dic_f.add &H01, "{0x01}-{extended key was press  (down)}"
dic_f.add &H02, "{0x02}-{normal key was released   (up)}"
dic_f.add &H03, "{0x03}-{extended key was released (up)}"

' Словарь ключей
Set dic_k = CreateObject("Scripting.Dictionary")
dic_k.add &H00, "{0x00}-{Undefined}"
dic_k.add &H01, "{0x01}-{Left mouse button}"
dic_k.add &H02, "{0x02}-{Right mouse button}"
dic_k.add &H03, "{0x03}-{Control-break processing}"
dic_k.add &H04, "{0x04}-{Middle mouse button (three-button mouse)}"
dic_k.add &H05, "{0x05}-{X1 mouse button}"
dic_k.add &H06, "{0x06}-{X2 mouse button}"
dic_k.add &H07, "{0x07}-{Undefined}"
dic_k.add &H08, "{0x08}-{BACKSPACE key}"
dic_k.add &H09, "{0x09}-{TAB key}"
dic_k.add &H0A, "{0x0A}-{Reserved}"
dic_k.add &H0B, "{0x0B}-{Reserved}"
dic_k.add &H0C, "{0x0C}-{CLEAR key}"
dic_k.add &H0D, "{0x0D}-{ENTER key}"
dic_k.add &H0E, "{0x0E}-{Undefined}"
dic_k.add &H0F, "{0x0F}-{Undefined}"
dic_k.add &H10, "{0x10}-{SHIFT key}"
dic_k.add &H11, "{0x11}-{CTRL key}"
dic_k.add &H12, "{0x12}-{ALT key}"
dic_k.add &H13, "{0x13}-{PAUSE key}"
dic_k.add &H14, "{0x14}-{CAPS LOCK key}"
dic_k.add &H15, "{0x15}-{IME Kana mode | IME Hanguel mode (maintained for compatibility; use VK_HANGUL) | IME Hangul mode}"
dic_k.add &H16, "{0x16}-{Undefined}"
dic_k.add &H17, "{0x17}-{IME Junja mode}"
dic_k.add &H18, "{0x18}-{IME final mode}"
dic_k.add &H19, "{0x19}-{IME Hanja mode | IME Kanji mode}"
dic_k.add &H1A, "{0x1A}-{Undefined}"
dic_k.add &H1B, "{0x1B}-{ESC key}"
dic_k.add &H1C, "{0x1C}-{IME convert}"
dic_k.add &H1D, "{0x1D}-{IME nonconvert}"
dic_k.add &H1E, "{0x1E}-{IME accept}"
dic_k.add &H1F, "{0x1F}-{IME mode change request}"
dic_k.add &H20, "{0x20}-{SPACEBAR}"
dic_k.add &H21, "{0x21}-{PAGE UP key}"
dic_k.add &H22, "{0x22}-{PAGE DOWN key}"
dic_k.add &H23, "{0x23}-{END key}"
dic_k.add &H24, "{0x24}-{HOME key}"
dic_k.add &H25, "{0x25}-{LEFT ARROW key}"
dic_k.add &H26, "{0x26}-{UP ARROW key}"
dic_k.add &H27, "{0x27}-{RIGHT ARROW key}"
dic_k.add &H28, "{0x28}-{DOWN ARROW key}"
dic_k.add &H29, "{0x29}-{SELECT key}"
dic_k.add &H2A, "{0x2A}-{PRINT key}"
dic_k.add &H2B, "{0x2B}-{EXECUTE key}"
dic_k.add &H2C, "{0x2C}-{PRINT SCREEN key}"
dic_k.add &H2D, "{0x2D}-{INS key}"
dic_k.add &H2E, "{0x2E}-{DEL key}"
dic_k.add &H2F, "{0x2F}-{HELP key}"
dic_k.add &H30, "{0x30}-{0 key}"
dic_k.add &H31, "{0x31}-{1 key}"
dic_k.add &H32, "{0x32}-{2 key}"
dic_k.add &H33, "{0x33}-{3 key}"
dic_k.add &H34, "{0x34}-{4 key}"
dic_k.add &H35, "{0x35}-{5 key}"
dic_k.add &H36, "{0x36}-{6 key}"
dic_k.add &H37, "{0x37}-{7 key}"
dic_k.add &H38, "{0x38}-{8 key}"
dic_k.add &H39, "{0x39}-{9 key}"
dic_k.add &H3A, "{0x3A}-{Undefined}"
dic_k.add &H3B, "{0x3B}-{Undefined}"
dic_k.add &H3C, "{0x3C}-{Undefined}"
dic_k.add &H3D, "{0x3D}-{Undefined}"
dic_k.add &H3E, "{0x3E}-{Undefined}"
dic_k.add &H3F, "{0x3F}-{Undefined}"
dic_k.add &H40, "{0x40}-{Undefined}"
dic_k.add &H41, "{0x41}-{A key}"
dic_k.add &H42, "{0x42}-{B key}"
dic_k.add &H43, "{0x43}-{C key}"
dic_k.add &H44, "{0x44}-{D key}"
dic_k.add &H45, "{0x45}-{E key}"
dic_k.add &H46, "{0x46}-{F key}"
dic_k.add &H47, "{0x47}-{G key}"
dic_k.add &H48, "{0x48}-{H key}"
dic_k.add &H49, "{0x49}-{I key}"
dic_k.add &H4A, "{0x4A}-{J key}"
dic_k.add &H4B, "{0x4B}-{K key}"
dic_k.add &H4C, "{0x4C}-{L key}"
dic_k.add &H4D, "{0x4D}-{M key}"
dic_k.add &H4E, "{0x4E}-{N key}"
dic_k.add &H4F, "{0x4F}-{O key}"
dic_k.add &H50, "{0x50}-{P key}"
dic_k.add &H51, "{0x51}-{Q key}"
dic_k.add &H52, "{0x52}-{R key}"
dic_k.add &H53, "{0x53}-{S key}"
dic_k.add &H54, "{0x54}-{T key}"
dic_k.add &H55, "{0x55}-{U key}"
dic_k.add &H56, "{0x56}-{V key}"
dic_k.add &H57, "{0x57}-{W key}"
dic_k.add &H58, "{0x58}-{X key}"
dic_k.add &H59, "{0x59}-{Y key}"
dic_k.add &H5A, "{0x5A}-{Z key}"
dic_k.add &H5B, "{0x5B}-{Left Windows key (Microsoft® Natural® keyboard)}"
dic_k.add &H5C, "{0x5C}-{Right Windows key (Natural keyboard)}"
dic_k.add &H5D, "{0x5D}-{Applications key (Natural keyboard)}"
dic_k.add &H5E, "{0x5E}-{Reserved}"
dic_k.add &H5F, "{0x5F}-{Computer Sleep key}"
dic_k.add &H60, "{0x60}-{Numeric keypad 0 key}"
dic_k.add &H61, "{0x61}-{Numeric keypad 1 key}"
dic_k.add &H62, "{0x62}-{Numeric keypad 2 key}"
dic_k.add &H63, "{0x63}-{Numeric keypad 3 key}"
dic_k.add &H64, "{0x64}-{Numeric keypad 4 key}"
dic_k.add &H65, "{0x65}-{Numeric keypad 5 key}"
dic_k.add &H66, "{0x66}-{Numeric keypad 6 key}"
dic_k.add &H67, "{0x67}-{Numeric keypad 7 key}"
dic_k.add &H68, "{0x68}-{Numeric keypad 8 key}"
dic_k.add &H69, "{0x69}-{Numeric keypad 9 key}"
dic_k.add &H6A, "{0x6A}-{Multiply key}"
dic_k.add &H6B, "{0x6B}-{Add key}"
dic_k.add &H6C, "{0x6C}-{Separator key}"
dic_k.add &H6D, "{0x6D}-{Subtract key}"
dic_k.add &H6E, "{0x6E}-{Decimal key}"
dic_k.add &H6F, "{0x6F}-{Divide key}"
dic_k.add &H70, "{0x70}-{F1 key}"
dic_k.add &H71, "{0x71}-{F2 key}"
dic_k.add &H72, "{0x72}-{F3 key}"
dic_k.add &H73, "{0x73}-{F4 key}"
dic_k.add &H74, "{0x74}-{F5 key}"
dic_k.add &H75, "{0x75}-{F6 key}"
dic_k.add &H76, "{0x76}-{F7 key}"
dic_k.add &H77, "{0x77}-{F8 key}"
dic_k.add &H78, "{0x78}-{F9 key}"
dic_k.add &H79, "{0x79}-{F10 key}"
dic_k.add &H7A, "{0x7A}-{F11 key}"
dic_k.add &H7B, "{0x7B}-{F12 key}"
dic_k.add &H7C, "{0x7C}-{F13 key}"
dic_k.add &H7D, "{0x7D}-{F14 key}"
dic_k.add &H7E, "{0x7E}-{F15 key}"
dic_k.add &H7F, "{0x7F}-{F16 key}"
dic_k.add &H80, "{0x80}-{F17 key}"
dic_k.add &H81, "{0x81}-{F18 key}"
dic_k.add &H82, "{0x82}-{F19 key}"
dic_k.add &H83, "{0x83}-{F20 key}"
dic_k.add &H84, "{0x84}-{F21 key}"
dic_k.add &H85, "{0x85}-{F22 key}"
dic_k.add &H86, "{0x86}-{F23 key}"
dic_k.add &H87, "{0x87}-{F24 key}"
dic_k.add &H88, "{0x88}-{Unassigned}"
dic_k.add &H89, "{0x89}-{Unassigned}"
dic_k.add &H8A, "{0x8A}-{Unassigned}"
dic_k.add &H8B, "{0x8B}-{Unassigned}"
dic_k.add &H8C, "{0x8C}-{Unassigned}"
dic_k.add &H8D, "{0x8D}-{Unassigned}"
dic_k.add &H8E, "{0x8E}-{Unassigned}"
dic_k.add &H8F, "{0x8F}-{Unassigned}"
dic_k.add &H90, "{0x90}-{NUM LOCK key}"
dic_k.add &H91, "{0x91}-{SCROLL LOCK key}"
dic_k.add &H92, "{0x92}-{OEM specific}"
dic_k.add &H93, "{0x93}-{OEM specific}"
dic_k.add &H94, "{0x94}-{OEM specific}"
dic_k.add &H95, "{0x95}-{OEM specific}"
dic_k.add &H96, "{0x96}-{OEM specific}"
dic_k.add &H97, "{0x97}-{Unassigned}"
dic_k.add &H98, "{0x98}-{Unassigned}"
dic_k.add &H99, "{0x99}-{Unassigned}"
dic_k.add &H9A, "{0x9A}-{Unassigned}"
dic_k.add &H9B, "{0x9B}-{Unassigned}"
dic_k.add &H9C, "{0x9C}-{Unassigned}"
dic_k.add &H9D, "{0x9D}-{Unassigned}"
dic_k.add &H9E, "{0x9E}-{Unassigned}"
dic_k.add &H9F, "{0x9F}-{Unassigned}"
dic_k.add &HA0, "{0xA0}-{Left SHIFT key}"
dic_k.add &HA1, "{0xA1}-{Right SHIFT key}"
dic_k.add &HA2, "{0xA2}-{Left CONTROL key}"
dic_k.add &HA3, "{0xA3}-{Right CONTROL key}"
dic_k.add &HA4, "{0xA4}-{Left MENU key}"
dic_k.add &HA5, "{0xA5}-{Right MENU key}"
dic_k.add &HA6, "{0xA6}-{Browser Back key}"
dic_k.add &HA7, "{0xA7}-{Browser Forward key}"
dic_k.add &HA8, "{0xA8}-{Browser Refresh key}"
dic_k.add &HA9, "{0xA9}-{Browser Stop key}"
dic_k.add &HAA, "{0xAA}-{Browser Search key}"
dic_k.add &HAB, "{0xAB}-{Browser Favorites key}"
dic_k.add &HAC, "{0xAC}-{Browser Start and Home key}"
dic_k.add &HAD, "{0xAD}-{Volume Mute key}"
dic_k.add &HAE, "{0xAE}-{Volume Down key}"
dic_k.add &HAF, "{0xAF}-{Volume Up key}"
dic_k.add &HB0, "{0xB0}-{Next Track key}"
dic_k.add &HB1, "{0xB1}-{Previous Track key}"
dic_k.add &HB2, "{0xB2}-{Stop Media key}"
dic_k.add &HB3, "{0xB3}-{Play/Pause Media key}"
dic_k.add &HB4, "{0xB4}-{Start Mail key}"
dic_k.add &HB5, "{0xB5}-{Select Media key}"
dic_k.add &HB6, "{0xB6}-{Start Application 1 key}"
dic_k.add &HB7, "{0xB7}-{Start Application 2 key}"
dic_k.add &HB8, "{0xB8}-{Reserved}"
dic_k.add &HB9, "{0xB9}-{Reserved}"
dic_k.add &HBA, "{0xBA}-{Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key}"
dic_k.add &HBB, "{0xBB}-{For any country/region, the '+' key}"
dic_k.add &HBC, "{0xBC}-{For any country/region, the ',' key}"
dic_k.add &HBD, "{0xBD}-{For any country/region, the '-' key}"
dic_k.add &HBE, "{0xBE}-{For any country/region, the '.' key}"
dic_k.add &HBF, "{0xBF}-{Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key}"
dic_k.add &HC0, "{0xC0}-{Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key}"
dic_k.add &HC1, "{0xC1}-{Reserved}"
dic_k.add &HC2, "{0xC2}-{Reserved}"
dic_k.add &HC3, "{0xC3}-{Reserved}"
dic_k.add &HC4, "{0xC4}-{Reserved}"
dic_k.add &HC5, "{0xC5}-{Reserved}"
dic_k.add &HC6, "{0xC6}-{Reserved}"
dic_k.add &HC7, "{0xC7}-{Reserved}"
dic_k.add &HC8, "{0xC8}-{Reserved}"
dic_k.add &HC9, "{0xC9}-{Reserved}"
dic_k.add &HCA, "{0xCA}-{Reserved}"
dic_k.add &HCB, "{0xCB}-{Reserved}"
dic_k.add &HCC, "{0xCC}-{Reserved}"
dic_k.add &HCD, "{0xCD}-{Reserved}"
dic_k.add &HCE, "{0xCE}-{Reserved}"
dic_k.add &HCF, "{0xCF}-{Reserved}"
dic_k.add &HD0, "{0xD0}-{Reserved}"
dic_k.add &HD1, "{0xD1}-{Reserved}"
dic_k.add &HD2, "{0xD2}-{Reserved}"
dic_k.add &HD3, "{0xD3}-{Reserved}"
dic_k.add &HD4, "{0xD4}-{Reserved}"
dic_k.add &HD5, "{0xD5}-{Reserved}"
dic_k.add &HD6, "{0xD6}-{Reserved}"
dic_k.add &HD7, "{0xD7}-{Reserved}"
dic_k.add &HD8, "{0xD8}-{Unassigned}"
dic_k.add &HD9, "{0xD9}-{Unassigned}"
dic_k.add &HDA, "{0xDA}-{Unassigned}"
dic_k.add &HDB, "{0xDB}-{Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key}"
dic_k.add &HDC, "{0xDC}-{Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\|' key}"
dic_k.add &HDD, "{0xDD}-{Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key}"
dic_k.add &HDE, "{0xDE}-{Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the 'single-quote/double-quote' key}"
dic_k.add &HDF, "{0xDF}-{Used for miscellaneous characters; it can vary by keyboard.}"
dic_k.add &HE0, "{0xE0}-{Reserved}"
dic_k.add &HE1, "{0xE1}-{OEM specific}"
dic_k.add &HE2, "{0xE2}-{Either the angle bracket key or the backslash key on the RT 102-key keyboard}"
dic_k.add &HE3, "{0xE3}-{OEM specific}"
dic_k.add &HE4, "{0xE4}-{OEM specific}"
dic_k.add &HE5, "{0xE5}-{IME PROCESS key}"
dic_k.add &HE6, "{0xE6}-{OEM specific}"
dic_k.add &HE7, "{0xE7}-{Used to pass Unicode characters as if they were keystrokes.}"
dic_k.add &HE8, "{0xE8}-{Unassigned}"
dic_k.add &HE9, "{0xE9}-{OEM specific}"
dic_k.add &HEA, "{0xEA}-{OEM specific}"
dic_k.add &HEB, "{0xEB}-{OEM specific}"
dic_k.add &HEC, "{0xEC}-{OEM specific}"
dic_k.add &HED, "{0xED}-{OEM specific}"
dic_k.add &HEE, "{0xEE}-{OEM specific}"
dic_k.add &HEF, "{0xEF}-{OEM specific}"
dic_k.add &HF0, "{0xF0}-{OEM specific}"
dic_k.add &HF1, "{0xF1}-{OEM specific}"
dic_k.add &HF2, "{0xF2}-{OEM specific}"
dic_k.add &HF3, "{0xF3}-{OEM specific}"
dic_k.add &HF4, "{0xF4}-{OEM specific}"
dic_k.add &HF5, "{0xF5}-{OEM specific}"
dic_k.add &HF6, "{0xF6}-{Attn key}"
dic_k.add &HF7, "{0xF7}-{CrSel key}"
dic_k.add &HF8, "{0xF8}-{ExSel key}"
dic_k.add &HF9, "{0xF9}-{Erase EOF key}"
dic_k.add &HFA, "{0xFA}-{Play key}"
dic_k.add &HFB, "{0xFB}-{Zoom key}"
dic_k.add &HFC, "{0xFC}-{Reserved for future use}"
dic_k.add &HFD, "{0xFD}-{PA1 key}"
dic_k.add &HFE, "{0xFE}-{Clear key}"
'--------------------------------------

' Формируем путь к папке с макросами
  pAkelDir = AkelPad.GetAkelDir(4)
  pAkelDir = """" & pAkelDir & "\Macros\" & """"

' Диалог выбора файла-макроса для декодирования
 Set objDialog = CreateObject("UserAccounts.CommonDialog")
 objDialog.InitialDir = pAkelDir
 objDialog.Filter = "Файлы macro (*.macro)|*.macro|Все файлы (*.*)|*.*"
 objDialog.FilterIndex = 1
 intResult = objDialog.ShowOpen
 If intResult = 0 Then
  Wscript.Quit      ' если файл-макрос не выбран, то выходим
 Else
  WM_COMMAND=273
  IDM_FILE_NEW=4101      ' команда на создание нового файла
  hMainWnd=AkelPad.GetMainWnd()
  AkelPad.SendMessage hMainWnd, WM_COMMAND, IDM_FILE_NEW, 0
  VbCrLf = Chr(13) & Chr(10)
  fn_macro = objDialog.FileName
  Set fsi = CreateObject("Scripting.FileSystemObject")
  fi_size = fsi.GetFile(fn_macro).Size    ' получить размер файла-макроса
  AkelPad.ReplaceSel(fn_macro & VbCrLf & "Size: " & fi_size & " bytes (" & fi_size/8 & " keys)" & VbCrLf & String(40, "-") & VbCrLf)
  Set fi = fsi.OpenTextFile(fn_macro, ForReading, True)
  For i = 1 To fi_size Step 8     ' перебираем 8-байтные цепочки "XX-00-00-00-YY-00-00-00"
    keyword = Asc(fi.Read(1))
    fi.Skip(3)
    flag_key = Asc(fi.Read(1))
    myitem = dic_k.Item(keyword)
    myflag = dic_f.Item(flag_key)
    AkelPad.ReplaceSel "{" & myflag & myitem & "}" & VbCrLf
    fi.Skip(3)
  Next
  fi.Close()
  AkelPad.ReplaceSel(String(40, "-") & VbCrLf)
 End If
