Hi Xperts,

Can anyone help me how to grab all key events and send
them to a single window. Also when i do any mouse operations
it  should take only those operations done on that window.

Like say i opened a dialog box. Until i close the window the focus
shouldn't go to any other window. Any key i press should be directed
to the dialog

I tried something and i have attached. but the window couldn't
get  the key and mouse  input

Can somebody give me clear picture!

Thanks,
Jeyasudha.
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>


Display *dis;
Window win;
XEvent report;
int i=0;

int main()
{
        XWindowChanges windowChanges;
        Window screeninput;
        XSetWindowAttributes windowAttributes;

        dis = XOpenDisplay(NULL);
        win = XCreateSimpleWindow(dis, RootWindow(dis, 0), \
          1,1,300,300,0, BlackPixel(dis,0), BlackPixel(dis,0));
        XMapWindow(dis,win);
        fprintf(stdout, "The display is %d \n", dis);
        XFlush(dis);
        XSync(dis, False);
        XSelectInput(dis,win, EnterWindowMask| LeaveWindowMask| ExposureMask| 
KeyPressMask| ButtonPressMask| StructureNotifyMask| ResizeRedirectMask 
|StructureNotifyMask | SubstructureNotifyMask | PropertyChangeMask );
        if (XGrabPointer(dis,
                     win,
                     False,                     /* owner_events */
                     PointerMotionMask | PointerMotionHintMask |  ButtonPressMask | 
ButtonReleaseMask,          /* event mask */
                     GrabModeAsync,             /* pointer_mode */
                     GrabModeAsync,             /* keyboard_mode */
                     None,                      /* confine_to window */
                     None,                      /* cursor */
                     CurrentTime) == GrabSuccess)
        {
        printf("Grabbed the mouse pointer"); 

        if (XGrabKeyboard(dis,
                     win,
                     False,                     /* owner_events */
                     GrabModeAsync,             /* pointer_mode */
                     GrabModeAsync,             /* keyboard_mode */
                     CurrentTime) == GrabFrozen)
        {
                printf("Grabbed the keyboard\n");       
        }
        else
        {
                printf("Failed Grabbing the keyboard first time \n");
                XUngrabKeyboard (dis, CurrentTime);
                if (XGrabKeyboard(dis,
                     RootWindow(dis,0),
                     False,                     /* owner_events */
                     GrabModeAsync,             /* pointer_mode */
                     GrabModeAsync,             /* keyboard_mode */
                     CurrentTime) == GrabFrozen)
                {
                printf("Grabbed the keyboard second time\n");
                }
                else
                {
                printf("Failed grabbing keyboard second time");
                }
        }

    }
   else
    {
        printf("Cannot Grab the mouse pointer"); 
        XUngrabPointer (dis, CurrentTime);

        if (XGrabPointer(dis,
                     RootWindow(dis,0),
                     False,                     /* owner_events */
                     PointerMotionMask | PointerMotionHintMask |  ButtonPressMask | 
ButtonReleaseMask,          /* event mask */
                     GrabModeAsync,             /* pointer_mode */
                     GrabModeAsync,             /* keyboard_mode */
                     None,                      /* confine_to window */
                     None,                      /* cursor */
                     CurrentTime) == GrabSuccess)
          printf ("Grabbed the mouse pointer second time \n");
        else
          printf ("Failed the mouse pointer second time \n");
        }
 /*       
        windowAttributes.event_mask = ButtonPressMask | EnterWindowMask;
        windowAttributes.override_redirect = True;
        screeninput = XCreateWindow (dis, RootWindow(dis,0), 0, 0,
                               DisplayWidth (dis, XDefaultScreen(dis)),
                               DisplayHeight (dis, XDefaultScreen(dis)),
                               0,
                               0,
                               InputOnly,
                               CopyFromParent,
                               CWEventMask | CWOverrideRedirect | CWCursor,
                               &windowAttributes);

        windowChanges.sibling = win;
        windowChanges.stack_mode = Below;
        XConfigureWindow (dis,screeninput,
                      CWSibling | CWStackMode, &windowChanges);
        */
        while(1)
        {
                XNextEvent(dis, &report);
                fprintf(stdout, "Event type is %d", report.type);
                switch(report.type)
                {

                case EnterNotify:
                i++;
                fprintf(stdout, "U have entered the window %d", i);
                break;

                case ConfigureNotify:
                
                fprintf(stdout, "Configure Notify event position x,y %d, 
%d",report.xconfigurerequest.x, report.xconfigurerequest.y );
                case LeaveNotify:
                i++;
                fprintf(stdout, " left the window %d", i);
                break;
        
                case Expose:
                fprintf(stdout,"Window exposed %d \n", i);
                break;

                case KeyPress:
                if( XLookupKeysym(&report.xkey, 0) == XK_space)
                {
                fprintf(stdout, "The space bar was pressed.\n");
                }
                else
                 fprintf(stdout, "a key is pressed\n");
                break;

                case ButtonPress:
                fprintf(stdout, "The mouse button is clicked\n");
                break;

                case ResizeRequest:
                fprintf(stdout, "The resize event occured\n");
                break;  
        
                case ConfigureRequest:
                fprintf(stdout, "Configure Notify happened\n");
                break;
                }
        }
        sleep(5);
        return(0);
}

**************************Disclaimer************************************

Information contained in this E-MAIL being proprietary to Wipro Limited is 
'privileged' and 'confidential' and intended for use only by the individual
 or entity to which it is addressed. You are notified that any use, copying 
or dissemination of the information contained in the E-MAIL in any manner 
whatsoever is strictly prohibited.

***************************************************************************

Reply via email to