local tobits = function(num, bits)
    bits = bits or (math.floor(math.log(num,2) / 8) + 1) * 8
    if bits == -(1/0) then bits = 8 end
    local value = ""
    for i = (bits - 1), 0, -1 do
        value = value..tostring((num >> i) & 0x1)
    end
    return value
end

desc = function(e)
    local typ = e:type()
    typ = hs.eventtap.event.types[typ] or "unknown:" .. tostring(typ)
    local data = { hs.timer.secondsSinceEpoch(), e:source(), e:timestamp(), typ, tobits(e:flags()), e:location() }
    table.insert(data, "st: " .. (e:subtype() and tobits(e:subtype()) or "n/a"))
    table.insert(data, "d1: " .. (e:data1() and tobits(e:data1()) or "n/a"))
    table.insert(data, "d2: " .. (e:data2() and tobits(e:data2()) or "n/a"))

    for k, v in hs.fnutils.sortByKeys(hs.eventtap.event.properties) do
--        table.insert(data, k .. ":" .. tostring(e:getProperty(v)))
        table.insert(data, e:getProperty(v))
    end
    print((hs.inspect(data):gsub("%s+", " "):gsub([[__luaSkinType = "NSPoint", ]], "")))
    return data
end

evt = hs.eventtap.new({"all"}, function(e)
    desc(e)
    return false
end)

doFor = function(secs)
    print("~~ starting event watcher")
    evt:start()
    tmr = hs.timer.doAfter(secs, function()
        print("~~ stopping event watcher")
        evt:stop()
    end)
end

--
--                                                                                                                                    A                 B      C      D   E
--
-- { 1476029831.1539, "HID", 338782543052435, "NSSystemDefined", "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029831.1586, "HID", 338782543052435, "flagsChanged",    "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0, 43, 57, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029831.3719, "HID", 338782838958560, "NSSystemDefined", "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
--
-- { 1476029832.1332, "HID", 338783598984780, "NSSystemDefined",         "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029832.1453, "HID", 338783598984780, "flagsChanged",            "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0, 43, 57, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029832.388,  "HID", 338783854983048, "NSSystemDefined",         "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
--
-- { 1476029833.2871, "HID", 338784671149510, "NSSystemDefined", "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029833.2923, "HID", 338784671149510, "flagsChanged",    "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0, 43, 57, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029833.4446, "HID", 338784911051479, "NSSystemDefined", "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
--
-- { 1476029834.1978, "HID", 338785663066640, "NSSystemDefined",         "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029834.203,  "HID", 338785663066640, "flagsChanged",            "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0, 43, 57, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029834.4923, "HID", 338785959039369, "NSSystemDefined",         "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
--
-- { 1476029835.493,  "HID", 338786879042046, "NSSystemDefined", "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029835.4973, "HID", 338786879042046, "flagsChanged",    "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0, 43, 57, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029835.5963, "HID", 338787063040571, "NSSystemDefined", "000000010000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
--
-- { 1476029836.2228, "HID", 338787687154627, "NSSystemDefined",         "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029836.228,  "HID", 338787687154627, "flagsChanged",            "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0, 43, 57, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
-- { 1476029836.3886, "HID", 338787855090038, "NSSystemDefined",         "0000000100000000", { x = 342.078125, y = 291.03515625 }, 0, 1, 0, 0, 0, 8038314, 90987, 0,  0,  0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
--
-- A   eventSourceStateID                probably set to match :source() -- verify
-- B   eventTargetProcessSerialNumber    probably set to match Hammerspoon and cannot be changed -- verify
-- C   eventTargetUnixProcessID          probably set to match Hammerspoon and cannot be changed -- verify
-- D   keyboardEventKeyboardType         research LMGetKbdType
-- E   keyboardEventKeycode              need to add new keycodes
