Old 01-20-2024, 10:23 AM   #1961
Rockum
Human being with feelings
 
Join Date: Apr 2009
Location: Nashville
Posts: 178
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,913
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: 178
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,913
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: 565
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,913
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: 565
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,598
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: 565
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,598
Default

yeah
Sexan 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 02:28 AM.


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