TGMainFrame Bind Key

Hi everyone!

I already posted my problem at the pyroot support wall ([url]TGMainFrame BindKey amd the pro there refered to this part of the forum. Can anybody show me how to bind keys in any root gui (Hope this woulld be easy to translate in python) and how to ask, whether a key is pressed or not. THANKS, Daniel


Use BindKey() to install a key binding and RemoveBind() to remove it. For example:

if (bind) { // bind the alt+F3 keys main->BindKey(this, gVirtualX->KeysymToKeycode(kKey_F3), kKeyMod1Mask); } else { // remove key binding on alt+F3 main->RemoveBind(this, gVirtualX->KeysymToKeycode(kKey_F3), kKeyMod1Mask); } kKeyMod1Mask is typically the Alt key (see GuiTypes.h for the compete list)

Cheers, Bertrand.

Thanks for your helpful reply. I think i got it to work now, but what is it with Signals or especially how can one ask wether the key(s) are pressed or not?


[quote=“dboeckenhoff”]Thanks for your helpful reply[/quote]You’re welcome!

[quote=“dboeckenhoff”]but what is it with Signals[/quote]Nothing (if you’re talking about signal/slot mechanism…)

[quote=“dboeckenhoff”]how can one ask wether the key(s) are pressed or not?[/quote]This is handled in HandleKey(Event_t *event) virtual method (that you have to handle/implement in your class)

Cheers, Bertrand.

I feel silly somehow, but i am not used to the event procedure. I tried to search for this for 4 hours now and did not find much. Can you give me some simple example for the usage? I hope this is easy to translate to python than … :slight_smile:. THANKS


There are plenty of example in the ROOT GUI source code itself. Just grep for BindKey and HandleKey. I have no simple example macro right now (I could prepare one tomorrow…). And BTW, it could even be easier (maybe) to post a piece of code illustrating what you’re trying to achieve…

Cheers, Bertrand.

Hi again :slight_smile:

I had no example, because i am so much stuck. Furthermore i am aware of i am in the ROOT support section, not the pyroot support, and i am not good at C++. Nevertheless i try some in python, which will have a lot of senseless lines, i guess, but i hope it shows what i want to do :confused:

This code is than part of a class which inherits from ROOT.TMainFrame, so self is a ROOT.TMainFrame

    self.f3Key = self.BindKey(self, ROOT.gVirtualX.KeysymToKeycode(ROOT.kKey_F3),
    # Now i don't know how to connect the event tot my handleEvent mehtod
    def HandleEvent(event):
        # I dont' know wheter event has attribute fType , I found this in a gui-example
        if not event.fType == ROOT.kGKeyPress:             
            # I guess the event fCode for f3Key is not self.f3Key
            if event.fCode == self.f3Key:         
                self.f3KeyOn = True
                self.d3KeyOn = False

You see the problem of giving an example, when you don’t have a clue :question:

Did i mention thank you :slight_smile:?

Hi Daniel,

Here is a C++ example (no Python, sorry…) that works fine on Linux (Windows has a problem with BindKey on functions keys…). You can execute it in a ROOT session (.x testgui.cxx), or compile it and create a standalone app (the instructions on how to compile it are in the source code)

[quote=“dboeckenhoff”]Did i mention thank you ?[/quote]Yes, and you’re very welcome! :slight_smile:

Cheers, Bertrand.
testgui.cxx (1.46 KB)

Hi Bertrand!

I fear it is not working with me. I guess i am right if i assume that the gui should print “Alt&F3 key pressed!” if i hit alt & F3 together. That somehow does not work. (I made shure the gui was selected when i hit alt&F3)

I am running on Scientific Linux 6.5

Cheers, Daniel

Hi Daniel,

Did you try to compile it and run it as a standalone app? Which version of ROOT are you using?

Cheers, Bertrand.

Hi Bertrand!
I am using ROOT version 5.34/17 (I know it’s an old one). I did not try to compile the program also, but when i do, i get:

root [1] .x testgui.cxx+
Info in <TUnixSystem::ACLiC>: creating shared library /.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/./
Error in <MyMainFrame>: MyMainFrame inherits from TObject but does not have its own ClassDef
In file included from /.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/testgui_cxx_ACLiC_dict.h:34,
                 from /.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/testgui_cxx_ACLiC_dict.cxx:17:
/.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/./testgui.cxx: In function ‘int main(int, char**)’:
/.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/./testgui.cxx:56: warning: unused variable ‘main_frame’
/.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/testgui_cxx_ACLiC_dict.cxx: In function ‘int G__testgui_cxx_ACLiC_dict__0_1426(G__value*, const char*, G__param*, int)’:
/.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/testgui_cxx_ACLiC_dict.cxx:221: error: ‘testgui’ was not declared in this scope
g++: /.automount/home/home__home2/institut_3b/boeckenhoff/Downloads/testgui_cxx_ACLiC_dict.o: No such file or directory
Error in <ACLiC>: Compilation failed!
Error: Function testgui() is not defined in current scope  :0:
*** Interpreter error recovered ***

Is running it as a standalone app another thing than compiling it?
Thanks so much,

Hi Daniel,

Try (as commented in the source):

And then run ./testgui

Cheers, Bertrand.

Thanks for the explanation, it compiled without problems, but still there is no output when hitting alt&F3


Edit: Also with the other setup (newest ROOT version)

Well, it works for me on SLC6 with ROOT 6.00 and ROOT 5 (5-34-00-patches), in compiled mode:

[bellenot@bbslc6x64-dev rootdev]$ g++ `root-config --cflags --glibs` -o testgui testgui.cxx [bellenot@bbslc6x64-dev rootdev]$ ./testgui Alt&F3 key pressed! Alt&F3 key pressed! Alt&F3 key pressed!
And interpreted mode (ROOT 6 only):

[bellenot@bbslc6x64-dev rootdev]$ root -l root [0] .x testgui.cxx root [1] Alt&F3 key pressed! Alt&F3 key pressed! Alt&F3 key pressed!
Cheers, Bertrand.