LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting Key and Create events from all window children with Xt (https://www.linuxquestions.org/questions/programming-9/getting-key-and-create-events-from-all-window-children-with-xt-587536/)

LonelyStar 09-26-2007 11:07 AM

Getting Key and Create events from all window children with Xt
 
Hello,

I want to write a plugin for codeblocks, that can record Keyboard macros.

I am currently trying around with Xt how to get keyevents. This is my Program so far:

Code:

#include <iostream>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xmd.h>
#include <X11/Xlibint.h>  /* for resource_mask */

using namespace std;

void AddWindow(Display* disp,Window* w)
{
    XSelectInput(disp,*w,KeyPressMask|KeyReleaseMask|SubstructureNotifyMask);
    Window root1,parent,*children;
    unsigned children_num,i;
    XQueryTree(disp,*w,&root1,&parent,&children,&children_num);
    for(i=0;i<children_num;++i)
    {
        AddWindow(disp,&children[i]);
    }
    Xfree(children);
}

int main(int argc,char *argv[])
{
    XtToolkitInitialize();
    XtSetLanguageProc(0,0,0);
    XtAppContext app_context=XtCreateApplicationContext();
    Display *disp=XtOpenDisplay(app_context,0,"XtTest","XtTest",0,0,&argc,argv);

    Window w=0;
    int revert_to=0;
    XGetInputFocus(disp,&w,&revert_to);

    AddWindow(disp,&w);

    XEvent ev;
    while(1)
    {
        XtAppNextEvent(app_context,&ev);
        XtDispatchEvent(&ev);
        switch(ev.type)
        {
            case KeyPress:
            cout<<"KeyPress"<<std::endl;
            break;
            case KeyRelease:
            cout<<"KeyRelease"<<std::endl;
            break;
            case MapNotify:
            cout<<"Window creation/deletion"<<std::endl;
            break;
            case CreateNotify:
            std::cout<<"Create window event"<<std::endl;
            break;
            default:
            std::cout<<"Unkown event type="<<ev.type<<std::endl;
            break;
        }
    }
    return 0;
}

It works, when i start it in gnome-terminal but only as long as I do not enter the menu or do anything which involves a new window.
As you can see, in AddWindow I am trying to get events from all childwindows. It does not help, I do not get key or CreateNotify events nor do I get Key events while in the menu or a dialog.

Any help?
Thanks!
Nathan

bigearsbilly 09-27-2007 03:56 AM

I don't get a window at all.

Wouldn't you better adding an eventhandler
man XtAddEventHandler to grab keypresses on the mainwindow?
these should(?) propogate down.

Is there a reason why you are processing the Event queue directly
instead of using XtAppMainLoop
and event handlers?

what distro?

LonelyStar 09-27-2007 04:10 AM

Hi,

Thanks for your reply!
It does not open a window, it just "connects" to the focues window, which is the gnome-terminal in my case.

Using XtAddEventHandler and XtAppMainLoop does not make a difference. I did not use them beacuse ... well, bevause I am used to have a event loop as in my example.

My distro is gentoo.

Nathan

bigearsbilly 09-27-2007 06:05 AM

well, a new window such as a new terminal will not be a child window
the menu probably has an event handler for the keypress
with the propogate set to off

LonelyStar 09-27-2007 07:27 AM

I just find out about the X record extension. Maybe I have more luck with that.
Thanks for the replies.
Nathan


All times are GMT -5. The time now is 07:30 AM.