Old 06-29-2018, 01:08 PM   #1
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default Hold key to run script?

Hello!

I was wondering if it is possible to hold down a key to run a script? So say I am working in reaper and I hold down the A key, it would run a script until I let go of the A key and then terminate the script.

Based on searching, my current solution is really clunky. It requires a graphics window to be open, but my script doesn't need that. It also requires the script be run for around 0.5 seconds at least, which is really annoying.

Here is my example:

Code:
function msg(m)
  reaper.ShowConsoleMsg(tostring(m).."\n")
end

-- The A key is the hotkey
local hotkey = 97
local timeWhenPressed, timeHeld, charHotkey

-- This is called when the script starts
function example_init()
  msg("key down")
  timeWhenPressed = reaper.time_precise()

  reaper.defer(example_update)
end

function example_update()
  charHotkey = gfx.getchar(hotkey)

  timeHeld = reaper.time_precise() - timeWhenPressed

  if timeHeld > 0 then
    msg("running")
  end

  -- Hotkey up
  if charHotkey == 0 and timeHeld > 0.5 then
    msg("key up")
  else
    reaper.defer(example_update)
  end

  gfx.update()
  reaper.UpdateArrange()
end

gfx.init("test", 0, 0, 0, 0, 0)
example_init()
If you bind this script to the A key, when you hold the key down it runs, and then it will terminate when you let go.
Alkamist is offline   Reply With Quote
Old 06-29-2018, 02:07 PM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Scripts can't check the keyboard state without a graphics window open, and the keyboard/OS doesn't actually start sending repeated signals for a key for a half second or so after you first click it.

Radial Menu uses roughly the same setup you've worked out - if I get a chance later I'll try to extract the appropriate function to see if there are any notable differences.

Worth noting that some keys and some foreign keyboards won't behave for this application - I ran into a few situations where I had to tell people "sorry, bind it to a different key" because of Reaper/OS limitations.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 06-30-2018, 07:13 AM   #3
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by Lokasenna View Post
Scripts can't check the keyboard state without a graphics window open, and the keyboard/OS doesn't actually start sending repeated signals for a key for a half second or so after you first click it.

Radial Menu uses roughly the same setup you've worked out - if I get a chance later I'll try to extract the appropriate function to see if there are any notable differences.

Worth noting that some keys and some foreign keyboards won't behave for this application - I ran into a few situations where I had to tell people "sorry, bind it to a different key" because of Reaper/OS limitations.
Well shoot, that sucks. I don't really mind the graphics windows popping up because its in the corner and goes away when you let go of the button. It's the half second delay that is a deal breaker. It looks like what I wanted to do isn't really an option I suppose.

Thanks for the help though!
Alkamist is offline   Reply With Quote
Old 06-30-2018, 07:33 AM   #4
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Incidentally, you might want to have a look on ReaPack. I forgot about this one: Repeat Action.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 06-30-2018, 08:09 AM   #5
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by Lokasenna View Post
Incidentally, you might want to have a look on ReaPack. I forgot about this one: Repeat Action.
That looks cool. Having trouble getting it working though. It's saying I have an invalid action ID when I follow the instructions. I'm not sure it would work for my purposes though.

I'm trying to make a Melodyne-ish zoom tool, where you hold a button and mouse movement in the x and y directions influences horizontal and vertical zoom. I would need each call to the script to remember the mouse position it was at in the previous call.
Alkamist is offline   Reply With Quote
Old 06-30-2018, 10:06 AM   #6
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Did you run the "Create a new action" script it installs? (Should show up as an action in your action list)
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 06-30-2018, 12:39 PM   #7
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by Lokasenna View Post
Did you run the "Create a new action" script it installs? (Should show up as an action in your action list)
Yeah I did, I get an error message if I try to run the action it creates.
Alkamist is offline   Reply With Quote
Old 06-30-2018, 12:41 PM   #8
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Will have a look, thanks. What action ID did you give it?
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 06-30-2018, 01:21 PM   #9
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by Lokasenna View Post
Will have a look, thanks. What action ID did you give it?
I wasn't sure what to do so I just called it "TEST".
Alkamist is offline   Reply With Quote
Old 06-30-2018, 02:14 PM   #10
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

No, the action you told it to run - first box in the dialog.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 06-30-2018, 05:14 PM   #11
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by Lokasenna View Post
No, the action you told it to run - first box in the dialog.
I'm guessing that's where I went wrong. I put that as TEST. Not quite sure how it works so I didn't know what to do.
Alkamist is offline   Reply With Quote
Old 06-30-2018, 08:00 PM   #12
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

You need to give it an action number from the Action List - there's a column listing them, or you can right-click an action and Copy the ID.

Paste that in the script window, give it a name, a time delay, and whether or not it's a MIDI Editor action.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-02-2018, 10:02 AM   #13
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by Lokasenna View Post
You need to give it an action number from the Action List - there's a column listing them, or you can right-click an action and Copy the ID.

Paste that in the script window, give it a name, a time delay, and whether or not it's a MIDI Editor action.
Ah ok, I see. Unfortunately I'm not sure it would work for me since the script would need to remember the position of the mouse between repeats.
Alkamist is offline   Reply With Quote
Old 07-02-2018, 12:29 PM   #14
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Scripts can store data in Reaper's .ini files or write directly to a text file themselves, so then the next instance of the script could look for that and read the data back in.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 07-02-2018, 02:35 PM   #15
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by Lokasenna View Post
Scripts can store data in Reaper's .ini files or write directly to a text file themselves, so then the next instance of the script could look for that and read the data back in.
Interesting. I'll take a look at that when I get the chance, thanks for the info!
Alkamist 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 08:34 AM.


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