for key in pairs(reaper) do _G[key]=reaper[key]  end
  -------------------------------------------------------------------------
  function MoveNotesVertically(take, dir)
    local dir_int = 1
    if dir then dir_int = -1 end
    local tableEvents = {}
    local t = 0
    local gotAllOK, MIDIstring = MIDI_GetAllEvts(take, "")
    local MIDIlen = MIDIstring:len()
    local stringPos = 1
    local offset, flags, msg
           
    while stringPos < MIDIlen-12 do
      offset, flags, msg, stringPos = string.unpack("i4Bs4", MIDIstring, stringPos)
      out_val = msg:byte(2)
      if msg:len() > 1 and ( msg:byte(1)>>4 == 0x9 or msg:byte(1)>>4 == 0x8 ) and flags&1==1 then  out_val = math.max(0,math.min(msg:byte(2)  - dir_int,127)) end
      t = t + 1
      tableEvents[t] = string.pack("i4Bi4BBB", offset, flags, 3, msg:byte(1), out_val, msg:byte(3) )
    end
           
    MIDI_SetAllEvts(take, table.concat(tableEvents) .. MIDIstring:sub(-12))
    MIDI_Sort(take)
  end
  -------------------------------------------------------------------------
  function main(dir)
    if val == 0 then return end
    local midieditor = MIDIEditor_GetActive()
    if not midieditor then return end
    local take =  MIDIEditor_GetTake( midieditor )
    if not take then return end
    Undo_BeginBlock()
    MoveNotesVertically(take, val>0)
    Undo_EndBlock('Move notes vertically', 1)
  end
  is_new_value,filename,sectionID,cmdID,mode,resolution,val = reaper.get_action_context()
  main(val)