Keyboard hotkey to replace button

@Mahdiyar what would need to be done so this script outputs null in normal state and 1 when a key is pressed? I can’t figure it out and Bing-AI wasn’t helpful on this one either.

public class Script_Instance : GH_ScriptInstance
{

  int a;

  private void RunScript(bool active, out object a)
  {
    
    a = null;

    Instances.DocumentEditor.KeyDown -= new KeyEventHandler(KeyDownEventHandler);
    Instances.DocumentEditor.KeyUp -= new KeyEventHandler(KeyUpEventHandler);
    if(active)
    {
      Instances.DocumentEditor.KeyDown += new KeyEventHandler(KeyDownEventHandler);
      Instances.DocumentEditor.KeyUp += new KeyEventHandler(KeyUpEventHandler);
    }
  }

  // <Custom additional code> 
  
  void KeyDownEventHandler(object sender, KeyEventArgs e)
  {
    if(e.KeyCode != Keys.Insert);
    a = 1;

  }
  void KeyUpEventHandler(object sender, KeyEventArgs e)
  {
    if(e.KeyCode != Keys.Insert) return;
    
  }

}

hotkey_null_1.gh (11.4 KB)

public class Script_Instance : GH_ScriptInstance
{
  private void RunScript(bool active, out object a)
  {
    Instances.DocumentEditor.KeyDown -= new KeyEventHandler(KeyDownEventHandler);
    Instances.DocumentEditor.KeyUp -= new KeyEventHandler(KeyUpEventHandler);
    if(active)
    {
      Instances.DocumentEditor.KeyDown += new KeyEventHandler(KeyDownEventHandler);
      Instances.DocumentEditor.KeyUp += new KeyEventHandler(KeyUpEventHandler);
    }
    a = i == 0 ? null:i;
  }
  int i = 0;
  // <Custom additional code> 
  void KeyDownEventHandler(object sender, KeyEventArgs e)
  {
    if(e.KeyCode != Keys.Insert || i == 1) return;
    i = 1;
    Component.ExpireSolution(true);
  }
  void KeyUpEventHandler(object sender, KeyEventArgs e)
  {
   if(e.KeyCode != Keys.Insert || i == 0) return;
    i = 0;
    Component.ExpireSolution(true);
  }
}

hotkey.gh (6.6 KB)

2 Likes

Thank you so much!

I can’t load this script. What’s wrong?

Sorry this is Rhino 8 WIP