Action_callback function problem

I’m new to sketchup, ruby sdk and I’ve run into a problem while creating a simple extension, I can’t seem to make the action callback work, so basically there is a input textbox and on pressing the enter key the text should get appended to a list.
I defined this action callback in the ruby code named ‘enter_pressed’

  def self.show_dialog
        if @dialog && @dialog.visible?
          @dialog.bring_to_front
        else
          @dialog ||= self.create_dialog
          @dialog.add_action_callback('enter_pressed') do |action_context|
            on_enter_pressed
            puts 'This is a debug message'
            nil

          end

          @dialog.show
        end
      end
    end

    def self.on_enter_pressed 
      puts 'Enter key pressed'
      ChatHandler.sendMessage
    end

and this is the js code calling the ruby function

class InteractionHandler{
 
 
    static checkEnter(event) {
        if (event.key === "Enter"){
          sketchup.action_callback("enter_pressed");
        }
      }

}
And this is the html code 

 <input type="text" id="input-message" placeholder="Type your message..." onkeyup="InteractionHandler.checkEnter(event)"

can anyone help me on why this isn’t working

This should be

if (event.key === "Enter"){
          sketchup.enter_pressed();
        }
3 Likes

I have done the change you have mentioned, but the enter press is still not being recognized in my extension

Let’s try to print out something in console:
With JS:

console.log("Hello Ruby");

and in Ruby:

p "Hello JS"

To find out where is the error happen.

1 Like

Error:
#<NoMethodError: undefined method on_enter_pressed' for SketchupAssistant::Chatbot::LoadHandler:Class> C:/Users/yamunas/AppData/Roaming/SketchUp/SketchUp 2023/SketchUp/Plugins/html/main.rb:46:in block in show_dialog’
I got the error message like this on my ruby console

I found you have a syntax error with unexpected ‘end’.
Here’s the correct syntax code:

def self.show_dialog
  if @dialog && @dialog.visible?
    @dialog.bring_to_front
  else
    @dialog ||= self.create_dialog
    @dialog.add_action_callback('enter_pressed') do |action_context|
      on_enter_pressed
      puts 'This is a debug message'
      nil
    end

    @dialog.show
  end
end

def self.on_enter_pressed 
  puts 'Enter key pressed'
  ChatHandler.sendMessage
end

Is ‘on_enter_pressed’ is in the same module/class with ‘show_dialog’ in your actual code?

2 Likes

yes it is on the same module

:

          @dialog.add_action_callback('enter_pressed') do |action_context|
            self.on_enter_pressed
            puts 'This is a debug message'
2 Likes

Seems like I have not kept both the functions in the same class, Now then enter key is being recognized, Thank you for your help

1 Like

Thank you, now it’s working

3 Likes