How to use the PGEntry

My question is simple, how to callback a function when I press enter to finish my input in a pgEntry.

I refer to the manual and C++ reference for hours, I think the get_accept_event may work, however, I don’t know the exact way to use it and didn’t find related examples in the forums.

framework.get_event_handler().add_hook(ip_address->get_focus_in_event(), event_test, ip_address);

this code help me to callback a function when I focus in the pgEntry,
but I need to callback a function when I press the enter.

Thanks in advance

ip_address->get_accept_event(KeyboardButton::enter()) is event name for Enter key in PGEntry.
you need to add hook with the event name:

std::string enter_key_ev_name = ip_address->get_accept_event(KeyboardButton::enter());
framework.get_event_handler().add_hook(enter_key_ev_name, event_test, ip_address);

Thank you for your help, it works now!

Besides, how to set the text’s color of the entry?

I use
entry_X->set_text("100");

to set the initial text, but I don’t know how to set the text color

or both the text font?

Text of PGEntry uses TextNode, so you can get it using entry->get_text_def(entry->get_state()).
And you can set text color or font in TextNode.

However, default TextNode is global instance, so the changes may affect another text.
But I did not test this part. It is guess from Panda3D codes:

Instead, you can set new TextNode using TextEntry::set_text_def.

Thank you, it works now , I used the following codes:

TextNode * entry_x_text = entry_X->get_text_def(0);
	entry_x_text->set_text_color(LColor(1., 1., 1., 1));
	entry_X->set_text_def(0, entry_x_text);

However, the initial text, created by the following code

entry_X->set_text("100");

its color is still not changed.

I use
entry_X->set_text_node(entry_x_text);

but it don’t work also.

It there anyway?

Oh, I am sorry. I omitted a function. You can call setup_minimal function to apply new TextNode.

TextNode* entry_x_text = entry_X->get_text_def(0);
entry_x_text->set_text_color(LColor(1., 0, 0, 1));
entry_X->set_text_def(0, entry_x_text);
entry_X->setup_minimal(10, 1);
entry_X->set_text("100");

It works !!
Thank you so much, this function is wonderful

Oh, the problem you have considered occurred just now.
I only want to change the color of the text of one entry, but all the text of entries is changed, which is not expected.
Do you know how to avoid this problem? I do not understand your above advice well.

Thanks in advance

I found that when I use the same codes to set the TextNode again can set the color as I expected…

I think that you may need to use new TextNode for each PGEntry.
New TextNode can be created by this code:

// create new TextNode
PT(TextNode) text_node = new TextNode("pguiText");
text_node->set_text_color(1.0f, 0.0f, 0.0f, 1.0f);
text_node->set_align(TextNode::A_left);

// create PGEntry
PT(PGEntry) entry_Y = new PGEntry("e2");
entry_Y->set_text_def(entry->get_state(), text_node);        // with new TextNode
...

Thank you very much, I get it