And here is a simple working example:
#include "TGFrame.h"
#include "TGButton.h"
#include "TGTextEdit.h"
#include "Riostream.h"
#include "KeySymbols.h"
const char *eventNames[] = {
"KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease",
"MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn",
"FocusOut", "Expose", "ConfigureNotify", "MapNotify",
"UnmapNotify", "DestroyNotify", "ClientMessage", "SelectionClear",
"SelectionRequest", "SelectionNotify", "ColormapNotify",
"DoubleClick", "OtherEvent"
};
class MyMainFrame : public TGMainFrame
{
private:
TGTextButton *fButton;
TGTextEdit *fTextEdit;
public:
MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
virtual ~MyMainFrame() {}
void EventSlot(Event_t *event);
ClassDef(MyMainFrame, 0)
};
void PrintMessage(Event_t *event)
{
// Slot function connected to the ProcessedEvent signal.
UInt_t keysym;
char str[2];
if (event->fType == kGKeyPress) {
gVirtualX->LookupString(event, str, sizeof(str), keysym);
std::cout << "PrintMessage() : event = " << eventNames[event->fType];
std::cout << "; key (ASCII) value = " << (int)str[0] << std::endl;
}
}
//______________________________________________________________________________
MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) : TGMainFrame(p, w, h)
{
// Default constructor.
fButton = new TGTextButton(this, "Exit Application", "gApplication->Terminate()");
AddFrame(fButton, new TGLayoutHints(kLHintsExpandX | kLHintsCenterY, 20, 20, 20, 20));
fTextEdit = new TGTextEdit(this, 150, 50);
AddFrame(fTextEdit, new TGLayoutHints(kLHintsExpandX | kLHintsCenterY, 20, 20, 20, 20));
// connect the ProcessedEvent signal to a class method
fTextEdit->GetCanvas()->Connect("ProcessedEvent(Event_t*)", "MyMainFrame", this, "EventSlot(Event_t*)");
// connect the ProcessedEvent signal to a free function
fTextEdit->GetCanvas()->Connect("ProcessedEvent(Event_t*)", 0, 0, "PrintMessage(Event_t*)");
MapSubwindows();
Layout();
MapWindow();
Resize(150,100);
}
//______________________________________________________________________________
void MyMainFrame::EventSlot(Event_t *event)
{
// Slot method connected to the ProcessedEvent signal.
UInt_t keysym;
char str[2];
if (event->fType == kGKeyPress) {
gVirtualX->LookupString(event, str, sizeof(str), keysym);
std::cout << "MyMainFrame::EventSlot() : event = " << eventNames[event->fType];
std::cout << "; key (ASCII) value = " << (int)str[0] << std::endl;
}
}
void testgui3()
{
new MyMainFrame(gClient->GetRoot(), 200, 100);
}