63 lines
2.6 KiB
C++
63 lines
2.6 KiB
C++
|
|
import Crafter.CppDOM;
|
||
|
|
using namespace Crafter;
|
||
|
|
import std;
|
||
|
|
|
||
|
|
HtmlElement* body = new HtmlElement("body", "<div id='container'>"
|
||
|
|
"<h1>Enhanced Event Handling Demo</h1>"
|
||
|
|
"<button id='clickButton'>Click Me!</button>"
|
||
|
|
"<button id='mouseButton'>Mouse Events</button>"
|
||
|
|
"<input type='text' id='keyInput' placeholder='Press keys here'>"
|
||
|
|
"<input type='text' id='changeInput' placeholder='Change me'>"
|
||
|
|
"<div id='output'></div>"
|
||
|
|
"</div>");
|
||
|
|
HtmlElement* clickButton = new HtmlElement("clickButton");
|
||
|
|
HtmlElement* mouseButton = new HtmlElement("mouseButton");
|
||
|
|
HtmlElement* keyInput = new HtmlElement("keyInput");
|
||
|
|
HtmlElement* changeInput = new HtmlElement("changeInput");
|
||
|
|
HtmlElement* output = new HtmlElement("output");
|
||
|
|
|
||
|
|
int main(){
|
||
|
|
// Click event - simple handler
|
||
|
|
clickButton->AddClickListener([&]() {
|
||
|
|
output->SetInnerHTML("<p>Simple click event triggered!</p>");
|
||
|
|
});
|
||
|
|
|
||
|
|
// Mouse events with position data
|
||
|
|
// mouseButton->AddMouseMoveListener([&](MouseEvent event) {
|
||
|
|
// output->SetInnerHTML("<p>Mouse moved to: (" + std::to_string(event.clientX) + ", " + std::to_string(event.clientY) + ")</p>");
|
||
|
|
// });
|
||
|
|
|
||
|
|
// Keyboard events with rich data
|
||
|
|
keyInput->AddKeyDownListener([&](KeyboardEvent event) {
|
||
|
|
std::string keyInfo = std::format("<p>Key pressed: {}</p>", event.key);
|
||
|
|
if (event.ctrlKey) keyInfo += "<p>Ctrl key pressed</p>";
|
||
|
|
if (event.shiftKey) keyInfo += "<p>Shift key pressed</p>";
|
||
|
|
if (event.altKey) keyInfo += "<p>Alt key pressed</p>";
|
||
|
|
output->SetInnerHTML(keyInfo);
|
||
|
|
});
|
||
|
|
|
||
|
|
// // Input event with data
|
||
|
|
// keyInput->AddInputListener([&](InputEvent event) {
|
||
|
|
// output->SetInnerHTML(std::format("<p>Input data: {} (composing: {})</p>", event.data, event.isComposing));
|
||
|
|
// });
|
||
|
|
|
||
|
|
// Change event with value
|
||
|
|
changeInput->AddChangeListener([&](ChangeEvent event) {
|
||
|
|
output->SetInnerHTML("<p>Input value changed to: '" + event.value + "'</p>");
|
||
|
|
});
|
||
|
|
|
||
|
|
// Context menu with position
|
||
|
|
mouseButton->AddContextMenuListener([&](MouseEvent event) {
|
||
|
|
output->SetInnerHTML("<p>Context menu at: (" + std::to_string(event.clientX) + ", " + std::to_string(event.clientY) + ")</p>");
|
||
|
|
});
|
||
|
|
|
||
|
|
// Resize event
|
||
|
|
body->AddResizeListener([&](ResizeEvent event) {
|
||
|
|
output->SetInnerHTML("<p>Window resized to: " + std::to_string(event.width) + "x" + std::to_string(event.height) + "</p>");
|
||
|
|
});
|
||
|
|
|
||
|
|
// Scroll event
|
||
|
|
body->AddScrollListener([&](ScrollEvent event) {
|
||
|
|
output->SetInnerHTML("<p>Scrolled to: (" + std::to_string(event.scrollX) + ", " + std::to_string(event.scrollY) + ")</p>");
|
||
|
|
});
|
||
|
|
}
|