View Single Post
Old 01-12-2020, 01:03 PM   #904
Sexan
Human being with feelings
 
Sexan's Avatar
 
Join Date: Jun 2009
Location: Croatia
Posts: 2,819
Default

Code:
local Key_TB = {}

local startTime = reaper.time_precise()
local thisCycleTime
local Element = {}

function Element:new(ID, name, func)
    local elm = {}
    elm.ID            = ID
    elm.name          = name
    elm.press         = function()  local start = true
                                    for i = 1, #elm.ID do
                                      if reaper.JS_VKeys_GetState(startTime-2):byte(elm.ID[i]) == 0 then start = false break -- BREAK IF NOT BOTH KEYS ARE PRESSED
                                     end
                                   end
                                   return start
                                   end
    elm.down_time     = 0
    elm.last_key_down = false
    elm.last_key_hold = false
    elm.last_key_up   = true
    elm.func          = func
    ----------------------
    setmetatable(elm, self)
    self.__index = self
    return elm
end

function extended(Child, Parent)
  setmetatable(Child,{__index = Parent})
end

Key = {}
extended(Key,     Element)

function Element:intercept(int)
  for i = 1, #self.ID do
    reaper.JS_VKeys_Intercept(self.ID[i], int)
  end
end

function Element:onKeyDown(kd)
  if kd and self.last_key_down == false then
    self.down_time = os.clock()
    self.last_key_down = true
    self.last_key_up   = false
    key["DOWN"] = self
    return self
  end
end

function Element:OnKeyUp(kd)
  if not kd and self.last_key_down == true and self.last_key_up == false then
    self.last_key_up   = true
    self.last_key_down = false
    self.last_key_hold = false
    key["UP"] = self
  end
end

function Element:onKeyHold()
  self.last_key_hold = true
  key["HOLD"] = self
  return self
end

function Element:GetKey()
  local KEY_DOWN = self.press()

  if KEY_DOWN then
    if self.last_key_up == true and self.last_key_down == false then
      self:onKeyDown(KEY_DOWN)
    elseif self.last_key_up == false and self.last_key_down == true then
      if os.clock() - self.down_time > 0.15 then
        self:onKeyHold()
      end
    end
  elseif not KEY_DOWN and self.last_key_up == false and self.last_key_down == true then
    self:OnKeyUp(KEY_DOWN)
  end

end

function Track_keys(tbl)
  local prevCycleTime = thisCycleTime or startTime
  thisCycleTime = reaper.time_precise()

  key = {}

  for k,v in pairs(tbl) do v:GetKey() end

  if key.DOWN or key.UP or key.HOLD then return key end
end

function main()

  key = Track_keys(Key_TB)
  
reaper.defer(main)
end

for i = 1, 255 do
   local func
   local name = string.char(i)
   if type(tonumber(name)) == "number" then
      func = select_as
   end
   if name == "S" then
      func = as_split
   end
   if i == 16 then
      name = "Shift"
   elseif i == 17 then
      name = "Ctrl"
   elseif i == 18 then
      name = "Alt"
   elseif i == 13 then
      name = "Return"
   elseif i == 8 then
      name = "Backspace"
   elseif i == 32 then
      name = "Space"
   elseif i == 20 then
      name = "Caps-Lock"
   elseif i == 27 then
      name = "ESC"
      func = remove
   elseif i == 9 then
      name = "TAB"
   elseif i == 192 then
      name = "~"
   elseif i == 91 then
      name = "Win"
   elseif i == 45 then
      name = "Insert"
   elseif i == 46 then
      name = "Del"
      func = del
   elseif i == 36 then
      name = "Home"
   elseif i == 35 then
      name = "End"
   elseif i == 33 then
      name = "PG-Up"
   elseif i == 34 then
      name = "PG-Down"
   end
   Key_TB[#Key_TB + 1] = Key:new({i}, name, func)
end

main()
Code:
elm.press         = function()  local start = true
                                    for i = 1, #elm.ID do
                                      if reaper.JS_VKeys_GetState(startTime-2):byte(elm.ID[i]) == 0 then start = false break -- BREAK IF NOT BOTH KEYS ARE PRESSED
                                     end
                                   end
                                   return start
                                   end
This function is made so you can set combination of keys to act as one key/action. For example :

Code:
Key_TB[#Key_TB + 1] = Key:new({17, 67}, "COPY", copy_mode, true) -- COPY (TOGGLE)
I you make a key like this you are putting two keys together in this case its : 17 - CTRL and 67 - C

The function above waits for both keys to be true in order to return the value:




This is just whole input class together with key call from main script.

The exact same thing you need to see in AreaScript, keys show like this in IDE.
Again this works on windows but its not working on OSX

Regarding drawing its in Area_51_Class script and its only this function.
Code:
function Element:draw()
  local cx,cy = reaper.JS_Window_ScreenToClient(track_window,self.x,self.y)
  reaper.JS_Composite(track_window, cx, cy, self.w, self.h, self.bm, 0, 0, 1, 1)
  refresh_reaper()
end
It reads the area tables and draws

Last edited by Sexan; 01-12-2020 at 01:16 PM.
Sexan is offline   Reply With Quote