API Docs

A MIDI device or virtual source, created via hs.midi.deviceNamed() or hs.midi.virtualSourceNamed().

Properties

identifier

string

A unique identifier for this device object.

displayName

string

The device's user-facing display name. Falls back to name if unavailable.

manufacturer

string

The device's manufacturer name, or an empty string if unavailable.

model

string

The device's model name, or an empty string if unavailable.

isOnline

boolean

Whether the device is currently online (connected).

isVirtual

boolean

Whether this object was created for a single source via hs.midi.virtualSourceNamed(), rather than for a whole device via hs.midi.deviceNamed().

Methods

setCallback(fn) -> HSMIDIDevice

Sets or removes the callback fired when a MIDI message is received.

The callback receives five arguments: this device object, the device's name, the command type as a string (e.g. "noteOn", "controlChange", "systemExclusive" — see hs.midi.commandTypes for the full set), a human-readable description, and a metadata table of command-specific fields.

when released, but some send noteOn with velocity 0 instead of noteOff.

setCallback(fn) -> HSMIDIDevice
Name Type Description
fn JSValue The function to call on each received message, or null to remove it
HSMIDIDevice
This device object, for chaining
A device with a callback, and the callback, stay alive until the callback is removed or the device is destroyed, even if you don't keep a reference to it
kb.setCallback((device, deviceName, commandType, description, metadata) => {
    if (commandType === "noteOn") console.log("Note " + metadata.note + " velocity " + metadata.velocity)
})

sendCommand(commandType, metadata) -> boolean

Sends a MIDI command to the device.

Supported commandType values and their metadata fields:

  • noteOn, noteOff: note (0-127), velocity (0-127), channel (0-15)
  • polyphonicKeyPressure: note (0-127), pressure (0-127), channel (0-15)
  • controlChange: controllerNumber (0-127), controllerValue (0-127), channel (0-15)
  • programChange: programNumber (0-127), channel (0-15)
  • channelPressure: pressure (0-127), channel (0-15)
  • pitchWheelChange: pitchChange (0-16383, center 8192), channel (0-15)
sendCommand(commandType, metadata) -> boolean
Name Type Description
commandType string The command type string
metadata {[key: string]: any} A table of command-specific fields
boolean
true if the command was sent, false if the device has no
kb.sendCommand("noteOn", { note: 60, velocity: 100, channel: 0 })

sendSysex(command) -> None

Sends a System Exclusive command to the device. For a device with several ports, the command is sent to every port.

sendSysex(command) -> None
Name Type Description
command string A hex string (whitespace ignored), e.g. "F0 7E 7F 06 01 F7"
None
kb.sendSysex("F0 7E 7F 06 01 F7")

identityRequest() -> None

Sends a MIDI Identity Request. The device's reply, if any, arrives via the callback set with setCallback() as a systemExclusive message.

identityRequest() -> None
None
kb.identityRequest()

destroy() -> None

Stops receiving from and releases all resources held by this device object. Called automatically when Hammerspoon reloads.

destroy() -> None
None
kb.destroy()

toJSON() -> {[key: string]: any}

Returns a plain object describing this device, for JSON serialisation

JSON.stringify() calls this automatically, so you rarely need to call it yourself.

toJSON() -> {[key: string]: any}
{[key: string]: any}
An object with the device's name, displayName, manufacturer, model, isOnline and isVirtual
console.log(JSON.stringify(kb))