TGTextEdit disable/change return key

Hi rooters,
I’m writing a GUI and I have a TGTextEntry where I can write the cut to apply and
I have a line like this to connect the return key to a function

fEntryCutExpression->Connect("ReturnPressed()","TPulseViewer",this,"ApplyCut()");

Sometimes I need to write very long string and the TGTextEntry is not always very useful.
I thought to use a TGTextEdit instead, but in the TGTextEdit environment
I cannot write a code line like the one above, because the return key is equal to a break line.

So that’s my question: is it possible to change the return key command from break line to a something like the ReturnPressed() of TGTextEntry Class?

Cheers,
Stefano

You can try to connect to the ProcessedEvent(Event_t *event) signal of your TGTextEdit widget (or its container) and properly handle the return key event into the slot

Hi,
you mean something similar to what is shown here?

How to detect mouse roll over or hover for TGPictureButton

I tried something similar changing the kEnterNotify with a kGKeyPress but then I don’t know how to detect the return key and use it then. Any suggestions?

Stefano

Yes, for example… (you can also use a class method)

You could try something like this:

   if (event->fType == kGKeyPress) {
      UInt_t keysym;
      char input[10];
      gVirtualX->LookupString(event, input, sizeof(input), keysym);
      if ((EKeySym)keysym == kKey_Enter) {
         // Your code goes here ...
      }
   }

Sorry for the late answer.
I tried the solution you proposed but doesn’t seem to work :confused:
I also tried to put a cout after the LookupString but nothing happens.

Cheers
Stefano

   fTextEdit->GetCanvas()->Connect("ProcessedEvent(Event_t*)", ...);

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);
}

1 Like

Thanks a lot it work perfectly.

Cheers
Stefano

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.