if not reaper.APIExists("JSVKeysGetState") then reaper.ShowMessageBox("SWS extension is…
- User
if not reaper.APIExists("JS_VKeys_GetState") then reaper.ShowMessageBox("SWS extension is required for this script to work.", "Error", 0) return end
function main() -- Get the current state of all virtual keys (256 keys, 1 byte each) local keyState = reaper.JS_VKeys_GetState(0) -- 0 is the default device index
for i = 1, 256 do if keyState:byte(i) ~= 0 then -- If a key is pressed, keyState:byte(i) will not be 0 reaper.ShowConsoleMsg("Key Pressed: " .. i .. "\n") end end
-- Continuously run the script to detect key presses reaper.defer(main) end
-- Call the main function main()
when i'm not pressing any key, it outputs 256 to the console. why? shortest answer please
- Assistant
The script outputs "Key Pressed: 256" because Lua strings are 1-based, so
keyState:byte(256)accesses one byte beyond the 256 bytes representing keys 1 to 255. Removeifrom 1 to 256, replace it with 0 to 255.Corrected loop:
luafor i = 0, 255 do if keyState:byte(i + 1) ~= 0 then -- Adjust for Lua's 1-based strings reaper.ShowConsoleMsg("Key Pressed: " .. i .. "\n") end end - User
if not reaper.APIExists("JS_VKeys_GetState") then reaper.ShowMessageBox("SWS extension is required for this script to work.", "Error", 0) return end
function main() -- Get the current state of all virtual keys (256 keys, 1 byte each) local keyState = reaper.JS_VKeys_GetState(0) -- 0 is the default device index
for i = 1, 256 do if keyState:byte(i) ~= 0 then -- If a key is pressed, keyState:byte(i) will not be 0 reaper.ShowConsoleMsg("Key Pressed: " .. i .. "\n") end end
-- Continuously run the script to detect key presses reaper.defer(main) end
-- Call the main function main()
i only want to get output in the console if i'm pressing a button (currently im not pressing any buttons and it is outputing 256
- Assistant
Given your issue, it seems the script is mistakenly interpreting the lack of key presses due to the Lua string index issue. To fix this, ensure your script correctly identifies pressed keys and only triggers output when an actual key is pressed.
Here's the corrected version of your script:
luaif not reaper.APIExists("JS_VKeys_GetState") then reaper.ShowMessageBox("SWS extension is required for this script to work.", "Error", 0) return end function main() -- Get the current state of all virtual keys (256 keys, 1 byte each) local keyState = reaper.JS_VKeys_GetState(0) -- 0 is the default device index for i = 0, 255 do if keyState:byte(i + 1) ~= 0 then -- Adjusted for Lua's 1-based indexing -- If a key is pressed, keyState:byte(i + 1) will not be 0 reaper.ShowConsoleMsg("Key Pressed: " .. i .. "\n") end end -- Continuously run the script to detect key presses reaper.defer(main) end -- Ensure the console is cleared initially for a clean start reaper.ClearConsole() -- Call the main function main() - User
now it outputs 255 if im not pressing any key
- Assistant
- User
string state = reaper.JS_VKeys_GetState(number cutoffTime)
what is number cutofftime
- Assistant
The
cutoffTimeparameter in thereaper.JS_VKeys_GetStatefunction is used to determine the period within which the state of the virtual keys is checked. Essentially, it is a timestamp that the function uses to filter key press events that occurred before a certain point in time.Here's a brief explanation:
cutoffTimeis a timestamp in milliseconds or seconds (based on the specific API documentation) that specifies the latest time for which key press events are considered valid.- Events that occurred before this cutoff time are ignored; only those happening at or after the cutoff time are included in the returned key state.
- User
if not reaper.APIExists("JS_VKeys_GetState") then reaper.ShowMessageBox("SWS extension is required for this script to work.", "Error", 0) return end
function main() -- Get the current state of all virtual keys (256 keys, 1 byte each) local keyState = reaper.JS_VKeys_GetState(0) -- 0 is the default device index
for i = 1, 255 do if keyState:byte(i) ~= 0 then -- If a key is pressed, keyState:byte(i) will not be 0 reaper.ShowConsoleMsg("Key Pressed: " .. i .. "\n") end end
-- Continuously run the script to detect key presses reaper.defer(main) end
-- Call the main function main()
how to only get the key if I pressed it once
- Assistant
If you want to detect only key presses when a key is pressed once (on a key down event), you need to keep track of the previous key states and compare them with the new key states. This way, you can differentiate between a key being held down and a key being pressed.
Here's how you can adjust your script to only log key presses on the key down events:
lua