Old 01-20-2024, 10:23 AM   #1961
Rockum
Human being with feelings
 
Join Date: Apr 2009
Location: Nashville
Posts: 228
Default

Could I use this to get and set these options in the MIDI editor?

Rockum is offline   Reply With Quote
Old 01-20-2024, 03:20 PM   #1962
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 3,988
Default

Quote:
Originally Posted by Rockum View Post
Could I use this to get and set these options in the MIDI editor?

Hope you can follow this (see code comments ).
To set the scale menu item I had to revert to sending keys to the menu
Good luck!

Code:
function msg(str) reaper.ShowConsoleMsg(tostring(str) .. '\n')end

function SetComboBoxIndex(hwnd, index)
  local id = reaper.JS_Window_AddressFromHandle(reaper.JS_Window_GetLongPtr(hwnd, "ID"))
  reaper.JS_WindowMessage_Send(hwnd, "CB_SETCURSEL", index,0,0,0)
  reaper.JS_WindowMessage_Send(reaper.JS_Window_GetParent(hwnd), "WM_COMMAND", id, 1, reaper.JS_Window_AddressFromHandle(hwnd), 0) -- 1 = CBN_SELCHANGE
end

local VK_RIGHT = 0x27  -- RIGHT ARROW key
local VK_DOWN = 0x28   -- DOWN ARROW key
local VK_RETURN = 0x0D -- ENTER key
function PostKey(hwnd, vk_code)
  reaper.JS_WindowMessage_Post(hwnd, "WM_KEYDOWN", vk_code, 0,0,0)
  reaper.JS_WindowMessage_Post(hwnd, "WM_KEYUP", vk_code, 0,0,0)
end

local is_windows = reaper.GetOS():match('Win')
local class_name = is_windows and '#32768' or '__SWELL_MENU' -- Thanks to FTC
function SelectMenuItem()
    local ret, list = reaper.JS_Window_ListAllTop()
    for address in string.gmatch(list .. ',', '[^,]+') do
      local hwnd = reaper.JS_Window_HandleFromAddress(address)
      if reaper.JS_Window_GetClassName(hwnd) == class_name then
        -- Send keys to select menu item!
        -- EXAMPLE #1: Select menu item "Blues", (7th item in menu)
        -- for i = 1, 7 do PostKey(hwnd, VK_DOWN) end
        -- PostKey(hwnd, VK_RETURN)
        --
        -- EXAMPLE #2 Select menu item "Chords > Major 7th"
        PostKey(hwnd, VK_DOWN)
        PostKey(hwnd, VK_RIGHT)
        PostKey(hwnd, VK_DOWN)
        PostKey(hwnd, VK_DOWN)
        PostKey(hwnd, VK_RETURN)
        return  -- All done!
      end
    end
    if reaper.time_precise() - init_time > 2 then msg('TimeOut: Menu not found!') return end
    reaper.defer(SelectMenuItem)
end

-- NOTE: Testing with "One ME pre-project"
hwnd = reaper.MIDIEditor_GetActive()                             -- get active MIDI Editor
chk = reaper.JS_Window_FindChildByID(hwnd, 0x4EC)                -- get handle to KeySnap checkbox
state = reaper.JS_WindowMessage_Send(chk, "BM_GETCHECK", 0,0,0,0)-- get KeySnap checkbox checked state
--
msg(state) -- 1 = checked, 0 = not checked
-- EXAMPLE to toggle key snap checkbox
--reaper.JS_WindowMessage_Send(chk, "BM_SETCHECK", 1-state, 0,0,0) -- set checkbox checked state
--reaper.JS_WindowMessage_Send(hwnd, "WM_COMMAND", 0x4EC, 0,0,0)   -- apply the setting

if state == 1 then
  -- get current key & scale settings
  cbo_key = reaper.JS_Window_FindChildByID(hwnd, 0x4ED)
  cbo_scale = reaper.JS_Window_FindChildByID(hwnd, 0x4EE)
  key_txt = reaper.JS_Window_GetTitle(cbo_key)
  scale_txt = reaper.JS_Window_GetTitle(cbo_scale)
  --
  msg(key_txt)  msg(scale_txt)

  -- Set Key to "A"  (index 4)
  SetComboBoxIndex(cbo_key, 4)

  -- Set Scale: Open scale menu,
  reaper.JS_WindowMessage_Post(hwnd, "WM_COMMAND", 0x4EE, 0,0,0)
  -- Send keys to menu to make selection
  init_time = reaper.time_precise()
  SelectMenuItem()
end
Edgemeal is offline   Reply With Quote
Old 01-20-2024, 05:39 PM   #1963
Rockum
Human being with feelings
 
Join Date: Apr 2009
Location: Nashville
Posts: 228
Default

Wow that was a lot of work. Thank you for taking the time.
Rockum is offline   Reply With Quote
Old 01-21-2024, 02:11 PM   #1964
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 3,988
Default

Quote:
Originally Posted by Rockum View Post
Wow that was a lot of work. Thank you for taking the time.
Most of it was copied/pasted from previous scripts/screwing around.

For setting the Key combobox, don't really need function SetComboBoxIndex() since we already have the combo parent and ID, you could simply do it this way..

Code:
  -- Set Key to "A"  (index 4) 
  reaper.JS_WindowMessage_Send(cbo_key, "CB_SETCURSEL", 4 ,0,0,0) -- select index 4 in combobox
  reaper.JS_WindowMessage_Send(hwnd, "WM_COMMAND", 0x4ED, 1, reaper.JS_Window_AddressFromHandle(cbo_key), 0) -- notify parent of combo index change

Last edited by Edgemeal; 01-22-2024 at 10:10 AM.
Edgemeal is offline   Reply With Quote
Old 03-18-2024, 10:29 AM   #1965
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 570
Default

Is there a way to "reset" the value obtained by reaper.JS_VKeys_GetState? For example, if a key is detected to have a state value of 1, can I forcibly reset it back to 0?

Typically, when a key is pressed, reaper.JS_VKeys_GetState will detect its value as 1, and when released, the value will return to 0.

However, I've noticed a special case. Suppose I set the function of a key to import an audio file (reaper.InsertMedia), when it starts running, an import file popup window will appear. This window impacts the judgment of the key's state by reaper.JS_VKeys_GetState, it erroneously considers the key as still being pressed, even though it has actually been released. It won't be correctly identified until I press it again.

If there's no way to forcibly reset it, is there any way to avoid the impact on the judgment of the key state by the pop-up window when importing an audio file?
dsyrock is offline   Reply With Quote
Old 03-18-2024, 01:37 PM   #1966
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 3,988
Default

Quote:
Originally Posted by dsyrock View Post
However, I've noticed a special case. Suppose I set the function of a key to import an audio file (reaper.InsertMedia), when it starts running, an import file popup window will appear. This window impacts the judgment of the key's state by reaper.JS_VKeys_GetState, it erroneously considers the key as still being pressed, even though it has actually been released. It won't be correctly identified until I press it again.
Confirmed. Not sure you can reset the key states, Maybe workaround would be to detect the key's released/Up state?, quick test seems to work OK here,..

Code:
  -- If "A" key Up... 
  if reaper.JS_VKeys_GetUp(reaper.time_precise()-0.1):byte(65) == 1 then
    reaper.InsertMedia(file, 1) 
  end
Edgemeal is offline   Reply With Quote
Old 03-18-2024, 07:52 PM   #1967
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 570
Default

Quote:
Originally Posted by Edgemeal View Post
Confirmed. Not sure you can reset the key states, Maybe workaround would be to detect the key's released/Up state?, quick test seems to work OK here,..

Code:
  -- If "A" key Up... 
  if reaper.JS_VKeys_GetUp(reaper.time_precise()-0.1):byte(65) == 1 then
    reaper.InsertMedia(file, 1) 
  end
Thanks for your advise, I will try it later.
dsyrock is offline   Reply With Quote
Old 03-19-2024, 06:31 AM   #1968
Sexan
Human being with feelings
 
Sexan's Avatar
 
Join Date: Jun 2009
Location: Croatia
Posts: 4,760
Default

There is a config variable that drastically influences VKeys Getstate.
Code:
r.SNM_SetIntConfigVar("alwaysallowkb", 1)
It can be only changed via SWS. Setting this var to 1 solves a lot of issues:
Blocked by popups, other windows etc. (where state hangs while some popup is blocking it)

Example:
1. Setting turned off (0)
Here I open the script (via key press) and immediately right click and release the key. Result is script is always open since the state of key is interrupted by right click context (VKeyGetState reports 1 always here since it hangs)


2. Setting turned on (1)
Same as above but script is not running anymore since key is not held down (VKeyGetState reports proper state, no hangs anymore)

Last edited by Sexan; 03-19-2024 at 06:40 AM.
Sexan is offline   Reply With Quote
Old 03-19-2024, 08:37 AM   #1969
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 570
Default

Quote:
Originally Posted by Sexan View Post
There is a config variable that drastically influences VKeys Getstate.
Code:
r.SNM_SetIntConfigVar("alwaysallowkb", 1)
Oh you mean the option in the "Advanced UI/system tweaks"? Thanks, didn't know that before
dsyrock is offline   Reply With Quote
Old 03-19-2024, 08:52 AM   #1970
Sexan
Human being with feelings
 
Sexan's Avatar
 
Join Date: Jun 2009
Location: Croatia
Posts: 4,760
Default

yeah
Sexan is offline   Reply With Quote
Old 04-07-2024, 09:32 AM   #1971
kamalamalamalam
Human being with feelings
 
Join Date: Aug 2023
Posts: 3
Default

Any plans for compiling a build for Linux aarch64 (ARM64)?
kamalamalamalam is offline   Reply With Quote
Old 06-07-2024, 04:02 PM   #1972
Sonorityscape
Human being with feelings
 
Sonorityscape's Avatar
 
Join Date: Aug 2016
Posts: 98
Default

I'm trying to set the rate knob in the media explorer to that of the selected item. For the most part it works, but sometimes the 'ctrl + a' key pattern doesn't work (see 2nd last block of code). Sometimes it will delete the text and sometimes it will add two A characters. Anyone know why?

Code:
function Msg(var)

    reaper.ShowConsoleMsg(tostring(var))

end

-----------------------------------------------------------------------------------------------------------------------

function Get_Sel_Item_Rate()

    local sel_item = reaper.GetSelectedMediaItem(0, 0)
    local take = reaper.GetActiveTake(sel_item)
    local rate = reaper.GetMediaItemTakeInfo_Value(take, "D_PLAYRATE")

    return rate

end

-----------------------------------------------------------------------------------------------------------------------

function post_text(hwnd, str) -- https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-char

    for char in string.gmatch(str, ".") do
    
      local ret = reaper.JS_WindowMessage_Post(hwnd, "WM_CHAR", string.byte(char),0,0,0)
      if not ret then break end
      
    end
  end

-----------------------------------------------------------------------------------------------------------------------

function Post_Key(hwnd, vk_code)
    
    -- highlight text
    reaper.JS_WindowMessage_Post(hwnd, "WM_KEYDOWN", vk_code, 0, 0, 0)
    reaper.JS_WindowMessage_Post(hwnd, "WM_KEYDOWN", 0x41, 0, 0, 0) 
    reaper.JS_WindowMessage_Post(hwnd, "WM_KEYUP", vk_code, 0, 0, 0)
    reaper.JS_WindowMessage_Post(hwnd, "WM_KEYUP", 0x41, 0, 0, 0)
    -- delete text
    reaper.JS_WindowMessage_Post(hwnd, "WM_KEYDOWN", 0x2E, 0, 0, 0)
    reaper.JS_WindowMessage_Post(hwnd, "WM_KEYUP", 0x2E, 0, 0, 0)

end

-----------------------------------------------------------------------------------------------------------------------

function Main()

    local take_rate = Get_Sel_Item_Rate()

    local hWnd = reaper.JS_Window_Find("Media Explorer", true)
    if hWnd == nil then return end  

    local rate = reaper.JS_Window_FindChildByID(hWnd, 1454)
    if rate == nil then return end  

    local delete = 0x2E
    local backspace = 0x08
    local tab = 0x09
    local ctrl = 0x11
    local a_key = 0x41

    Post_Key(rate, ctrl)
    
    post_text(rate, tostring(take_rate))


end

-----------------------------------------------------------------------------------------------------------------------

Main()
Sonorityscape is offline   Reply With Quote
Old 06-07-2024, 04:53 PM   #1973
Sonorityscape
Human being with feelings
 
Sonorityscape's Avatar
 
Join Date: Aug 2016
Posts: 98
Default

Seems like using different keyboard commands will have different results. One will work and the other will post two "A" characters in addition to the rate value.
Sonorityscape is offline   Reply With Quote
Old 06-07-2024, 08:58 PM   #1974
Sonorityscape
Human being with feelings
 
Sonorityscape's Avatar
 
Join Date: Aug 2016
Posts: 98
Default

Okay, I found the 'reaper.JS_Window_SetTitle(hwnd, "")' function which is solid
Sonorityscape is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 03:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.