Modifier key release

Can ‘key handler’ track just ctrl key press and release?

Couldn’t format text properly on forum so uploaded an image below.

It shows the barebones of a keyhandler function. The function receives the ‘key’ parameter when passed to ‘show_custom_dialog method’

renoise.app():show_custom_dialog(“TOOL NAME”, dialog_content, my_keyhandler_func)


The rprint for control key is shown at the bottom. It shows that for control we have access to modifiers, name and repeated.

///

key is an object passed with each keystroke in renoise. It has properties we can read/ test:

In the function key.modifiers is tested with an if statement (you could test key.name aswell to check for left or right control presses too.) When it passes the if statement as true you run your code/flag there.

NOTE: for testing you could add rprint(key) on line 10 to check properties of other keys.

key handler

code not tested

Just to add: The more recent version of Renoise add key release events to the key handler function.

Shows a non modal dialog (a floating tool window) with custom content.
Again see Renoise.ViewBuilder.API for more info about custom views.
key_handler is an optional notifier function for keyboard events that are 
received by the dialog. 

key_handler_options is an optional table with the 
fields { "send_key_repeat": true/false, "send_key_release": true/false }

renoise.app():show_custom_dialog(title, content_view 
  [, key_handler, key_handler_options])
  -> [renoise.Dialog object]

So you probably also then get the state of the key that is passed to your key handler.

rprint(key)

[modifiers] =>  control
[name] =>  lcontrol
[repeated] =>  false
[state] =>  pressed

[modifiers] =>  control
[name] =>  lcontrol
[repeated] =>  false
[state] => released
1 Like

I usually separate the key detection into a separate function like so:

function get_keypress (key)
  local modifiers = key.modifiers
    if modifiers ~= '' then
      modifiers = key.modifiers .. ' + '
    end
  return modifiers .. key.name
end

So in my keyhandler function, I can use simple syntax to check against what key was pressed, instead of dealing with modifiers, etc every time:

local keyhandler = function ( dialog, key )
  local keypress = get_keypress(key)
  if keypress == 'esc' then
    dialog:close()
    return
  elseif keypress == 'shift + return' then
    -- do something here
  elseif keypress == 'lcontrol + ' then
    -- only left control was pressed
  -- etc, etc...
  end
  return key
end

It’s not super pretty, but it makes simple key bindings a little less hassle. With a little modification, you could get it to differentiate between press and release, ala “pressed: shift + return” vs “released: shift + return”

-- Tested working
function get_keypress (key)
  local keycode = key.state .. ': '
  if key.modifiers ~= '' then
    keycode = keycode .. key.modifiers .. ' + '
  end
  return keycode .. key.name
end

But for this to work, you do need to pass {send_key_release=true} in key_handler_options when you construct your dialog as @4tey said above.

Hope this helps!

P.S. - Sorry for the flurry of edits, I’m still getting used to this post editor.

1 Like

Thanks, guys. I just forgot that key name should be “lcontrol”/“rcontrol”, not “control”. Works ok now.

1 Like