ksp

Last modifiedby & filed under Kontakt Scripting (KSP) - Basics, UIs, Tutorials, Scripts and Tools, Kontakt Scripting (KSP) :: Basics.

This is also a follow up to the Button Picture State tutorial and shows various types of buttons like toggles and disabled buttons and some common use cases. Below are the six possible button’s picture states again, listed in the right order. However via KSP (script) we can only address the on and off state. So unfortunately, the hover & pressed state can neither be read nor set. Well fortunately in most cases at least the pressed state equals the on state in terms of the functionality.

  • off
  • on
  • pressed off
  • pressed on
  • mouse hover off
  • mouse hover on

CONTENT

  1. How to SET a button state

  2. How to read a button state

  3. How to SET a button state via set_control_par

  4. How to read a button state via set_control_par

  5. simple use cases

  6. precursor: customizing buttons (animations & picture states)

 


How to SET a button state

on init 
  declare ui_button $myButton
  $myButton := 1 {sets the button into the on state}
end on

This is a simple as it can be.

declare ui_button $myButton first we declare a button of any name. In this case $myButton.
And then we change the button’s state by simply setting the $myButton variable to 0 or 1.
But keep in mind again that there are only 2 options: 0 (off state) & 1 (on state), no hover or pressed state.

$myButton1 := 1 sets the button to the on state.
This automatically affects the picture state if you have set any. You really don’t have to take care about anything other than setting a picture with the 6 button states.
Learn more about creating & setting a picture with all 6 button states

$myButton1 := 0 sets the button to the off state.
This automatically affects the picture state if you have set any. You really don’t have to take care about anything other than setting a picture with the 6 button states.
Learn more about creating & setting a picture with all 6 button states

 

How to read a button state

on init 
  declare ui_button $myButton
end on 

on ui_control($myButton)
  message($myButton)
end on

This is as simple as setting a button state.

$myButton always holds the actual Button state (0 = off, 1 = on), and gets automatically updated whenever the button is used

The state can then can be read throughout the whole code, by simply using $myButton as it is a global variable.

on init 
  declare ui_button $myButton
  declare $buttonState
end on 

on ui_control($myButton)
  $buttonState := $myButton
end on
on note
  message($myButton & $buttonState) {in this case both $buttonState & $myButton hold the same values, but are still independent variables}
end on

Of course, we can also use the $myButton variable like any other variable and pass its value on to another variable.
The example above doesn’t seem to make much sense since both variables always hold the same value but these variables are independent from each other.
So we can work with the $buttonState variable without ever affecting the actual button state which may be a big advantage in some cases.

How to SET a button state via set_control_par

on init 
  declare ui_button $myButton
  set_control_par(get_ui_id($myButton), $CONTROL_PAR_VALUE, 1) {sets the button into the on state, including the picture state}
end on

Well, this method requires some more code, but has the advantage that we can now toggle several buttons at once like shown in the Switching Multiple Buttons example, below

How to read a button state via set_control_par

on init 
  declare ui_button $myButton
  declare $buttonState
end on
on ui_control($myButton)
  $buttonState := get_control_par(get_ui_id($myButton), $CONTROL_PAR_VALUE)
  message($buttonState)
end on

get_control_par(get_ui_id($myButton),...  simply gets the button state. Again its more code like simply reading the $myButton variable, but this way we can read several button states at once in a while loop.

examples & use cases

free premium content

How useful was this article?

something you didn't like? Please tell us before you rate!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

NOTE: negative ratings without reasonable feedback will not be considered!

Tell us how we can improve this post?

Leave a Reply

use <pre> </pre> to wrap code blocks

use <code> </code> to wrap small code snippets

use basic html to style your comment

Your email address will not be published. Required fields are marked *