protected override void Initialize()
        {
            if (_display != IntPtr.Zero)
            {
                LibX11.XCloseDisplay(_display);
            }
            _display = IntPtr.Zero;

            _display = LibX11.XOpenDisplay(IntPtr.Zero);
            if (_display == IntPtr.Zero)
            {
                throw new Exception("Could not open display.");
            }

            if (LibX11.XSelectInput(_display, _window, LibX11.KeyPressMask | LibX11.KeyReleaseMask) == LibX11.BadWindow)
            {
                throw new Exception("X Error!");
            }

            if (_grabKeyboard)
            {
                LibX11.XGrabKeyboard(_display, _window, true, LibX11.GrabModeAsync, LibX11.GrabModeAsync, LibX11.CurrentTime);
            }

            _keyFocusLost = false;
        }
        /// <summary>
        ///
        /// </summary>
        public override void Capture()
        {
            int             key;
            X11InputManager linMan = (X11InputManager)Creator;

            System.Diagnostics.Debug.Assert(linMan != null);

            while (LibX11.XPending(_display) > 0)
            {
                /*if ( LibX11.XCheckMaskEvent( _display, (int)(LibX11.XEventName.KeyPress | LibX11.XEventName.KeyRelease), ref xevent ) == true )*/
                LibX11.XNextEvent(_display, ref _xEvent);
                {
                    switch (_xEvent.type)
                    {
                    case LibX11.XEventName.KeyPress:

                        int character = 0;
                        if (TextMode != TextTranslationMode.Off)
                        {
                            StringBuilder sb = new StringBuilder(6);
                            LibX11.XLookupString(ref _xEvent.KeyEvent, sb, 6, out key, IntPtr.Zero);

                            if (TextMode == TextTranslationMode.Unicode)
                            {
                                character = UTF8toUTF32(sb);
                            }
                            else if (TextMode == TextTranslationMode.Ascii)
                            {
                                character = (int)sb[0];
                            }
                        }

                        //Mask out the modifier states X11 sets and read again
                        _xEvent.KeyEvent.state &= ~LibX11.ShiftMask;
                        _xEvent.KeyEvent.state &= ~LibX11.LockMask;
                        StringBuilder b = new StringBuilder();
                        LibX11.XLookupString(ref _xEvent.KeyEvent, b, 0, out key, IntPtr.Zero);

                        injectKeyDown(key, character);

                        //Check for Alt-Tab
                        if ((_xEvent.KeyEvent.state & LibX11.Mod1Mask) != 0 && key == (int)LibX11.KeySym.XK_Tab)
                        {
                            linMan.GrabState = false;
                        }
                        break;

                    case LibX11.XEventName.KeyRelease:

                        if (!IsKeyRepeat(_xEvent))
                        {
                            //Mask out the modifier states X sets.. or we will get improper values
                            _xEvent.KeyEvent.state &= ~LibX11.ShiftMask;
                            _xEvent.KeyEvent.state &= ~LibX11.LockMask;

                            StringBuilder sb = new StringBuilder(6);
                            LibX11.XLookupString(ref _xEvent.KeyEvent, sb, 0, out key, IntPtr.Zero);

                            injectKeyUp(key);
                        }
                        break;
                    }
                }
            }            //end while

            //If grabbing mode is on.. Handle focus lost/gained via Alt-Tab and mouse clicks
            if (_grabKeyboard)
            {
                System.Diagnostics.Debug.Assert(linMan != null);
                if (linMan.GrabState == false)
                {                       // are no longer grabbing
                    if (_keyFocusLost == false)
                    {
                        // ungrab keyboard
                        LibX11.XUngrabKeyboard(_display, LibX11.CurrentTime);
                        _keyFocusLost = true;
                    }
                }
                else
                {
                    // We are grabbing - and regained focus
                    if (_keyFocusLost == true)
                    {
                        // regrab keyboard
                        LibX11.XGrabKeyboard(_display, _window, true, LibX11.GrabModeAsync, LibX11.GrabModeAsync, LibX11.CurrentTime);
                        _keyFocusLost = false;
                    }
                }
            }
        }