In the previous KitLab article, we drew simple text using BView.
Now it's time to go interactive!
In this article, we’ll add a native BButton, handle click events using Haiku’s message system, and dynamically update the interface.
BButton is a subclass of BControl and represents a clickable button in Haiku’s Interface Kit.
When clicked, it sends a BMessage to its parent window or view.
This is how most user interaction is handled in Haiku: through a message-passing system.
Source Code: ButtonApp.cpp
#include <Application.h>
#include <Window.h>
#include <View.h>
#include <Button.h>
#include <StringView.h>
#include <String.h>
// Message constant for the button click event
const uint32 kButtonClicked = 'btcl';
class ButtonWindow : public BWindow
{
public:
ButtonWindow(BRect frame)
:
BWindow(frame, "Button Example", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE),
fClickCount(0)
{
// Background view to host controls
BRect bounds = Bounds();
BView* background = new BView(bounds, "background", B_FOLLOW_ALL, B_WILL_DRAW);
background->SetViewColor(220, 220, 220);
AddChild(background);
// Initial label
BRect labelRect(20, 60, 200, 80);
fLabel = new BStringView(labelRect, "label", "Click the button");
background->AddChild(fLabel);
// Button that triggers the counter
BRect buttonRect(20, 20, 120, 50);
BButton* button = new BButton(buttonRect, "clickme", "Click Me", new BMessage(kButtonClicked));
background->AddChild(button);
}
void MessageReceived(BMessage* message) override
{
switch (message->what) {
case kButtonClicked:
fClickCount++;
{
BString labelText;
labelText.SetToFormat("Clicked %d time%s", fClickCount, fClickCount == 1 ? "" : "s");
fLabel->SetText(labelText.String());
}
break;
default:
BWindow::MessageReceived(message);
}
}
private:
BStringView* fLabel;
int32 fClickCount;
};
class ButtonApp : public BApplication
{
public:
ButtonApp()
:
BApplication("application/x-vnd.example-buttonapp")
{
BRect rect(100, 100, 300, 200);
ButtonWindow* window = new ButtonWindow(rect);
window->Show();
}
};
// Main program
int main()
{
ButtonApp app;
app.Run();
return 0;
}
This example creates a window with a button and a label.
When the button is clicked, the label text is updated with the number of clicks.
A BButton is added to a background view inside the window.
A BStringView is used as a label.
When the button is clicked, the window receives a BMessage ('btcl') and updates the label using SetText().
MessageReceived() is the central handler for events in Haiku.
Each BWindow (and BView) can receive BMessage objects, which are used for everything from button clicks to custom events.
Why not add the button directly to the window?
While you technically can add a BButton directly to a BWindow, the recommended practice is to create a background BView.
This gives you more control over layout, background color, and additional children.
So the structure looks like this:
Launch the application in a Terminal:
ButtonApp
When you click the button, the label is updated:

And if you make additional clicks, the label will update accordingly:

Now that we’re handling user interaction, we can go even further. In upcoming KitLab articles, we’ll explore:
Handling keyboard and mouse events in a BView
Drawing shapes and dynamic graphics
Avoiding hardcoded positions using layouts
In this article, you connected user interaction to visual feedback.
This is a foundational skill in GUI programming, and Haiku makes it clean and elegant using messages and views.
From here, your applications can become truly dynamic and interactive.