<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Courier New";}
span.EmailStyle19
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.kwd
        {mso-style-name:kwd;}
span.pln
        {mso-style-name:pln;}
span.pun
        {mso-style-name:pun;}
span.typ
        {mso-style-name:typ;}
span.str
        {mso-style-name:str;}
span.lit
        {mso-style-name:lit;}
span.EmailStyle26
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal style='line-height:13.5pt;background:white;vertical-align:baseline'><span style='font-size:10.5pt;font-family:Consolas;color:#1F497D;border:none windowtext 1.0pt;padding:0in'>Hello All,<o:p></o:p></span></p><p class=MsoNormal style='line-height:13.5pt;background:white;vertical-align:baseline'><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>I want to record all incoming key pressed events no matter what window is in focus or where the pointer is.<o:p></o:p></span></p><p class=MsoNormal style='line-height:13.5pt;background:white;vertical-align:baseline'><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>I have written a sample code which should capture the key pressed events of the current Window in focus.<o:p></o:p></span></p><p class=MsoNormal style='line-height:13.5pt;background:white;vertical-align:baseline'><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'><o:p> </o:p></span></p><p class=MsoNormal style='line-height:13.5pt;background:white;vertical-align:baseline'><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>int</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> main() <br>{<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>Display</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> *display = </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XOpenDisplay</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(NULL); <br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>int</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> iError;<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>KeySym</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> k;<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>int</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> revert_to;<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>Window</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> window;<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XEvent</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> event;<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>Time</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> time;<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XSetErrorHandler</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(_invalid_window_handler);<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XGetInputFocus</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(display, &window, &revert_to);<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XSelectInput</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(display, window, </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>KeyPressMask</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> | </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>KeyReleaseMask</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> );<br>    iError = </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XGrabKeyboard</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(display, window,<br>                          </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>KeyPressMask</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> | </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>KeyReleaseMask</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>,<br>                          </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>GrabModeAsync</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>,<br>                          </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>GrabModeAsync</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>,<br>                          </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>CurrentTime</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>); <br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>if</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> (iError != </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>GrabSuccess</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> && iError == </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>AlreadyGrabbed</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>) {<br>        </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XUngrabPointer</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(display, </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>CurrentTime</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>);<br>        </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XFlush</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(display);<br>        printf(</span><span style='font-size:10.5pt;font-family:Consolas;color:maroon;border:none windowtext 1.0pt;padding:0in'>"Already Grabbed\n"</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>);    <br>    } </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>else</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>if</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> (iError == </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>GrabSuccess</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>) {<br>        printf(</span><span style='font-size:10.5pt;font-family:Consolas;color:maroon;border:none windowtext 1.0pt;padding:0in'>"Grabbed\n"</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>);<br>    }<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>while</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(</span><span style='font-size:10.5pt;font-family:Consolas;color:maroon;border:none windowtext 1.0pt;padding:0in'>1</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>) {<br>          </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XNextEvent</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(display,&event);<br>          </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>switch</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> (event.type) {<br>              </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>case</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>KeyPress</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> : printf(</span><span style='font-size:10.5pt;font-family:Consolas;color:maroon;border:none windowtext 1.0pt;padding:0in'>"Key Pressed\n"</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>); </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>break</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>;<br>              </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>case</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>KeyRelease</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> : printf(</span><span style='font-size:10.5pt;font-family:Consolas;color:maroon;border:none windowtext 1.0pt;padding:0in'>"Key Released\n"</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>); </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>break</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>;<br>              </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>case</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>EnterNotify</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> : printf(</span><span style='font-size:10.5pt;font-family:Consolas;color:maroon;border:none windowtext 1.0pt;padding:0in'>"Enter\n"</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>); </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>break</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>;<br>          }<br>    }<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:#2B91AF;border:none windowtext 1.0pt;padding:0in'>XCloseDisplay</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>(display);<br>    </span><span style='font-size:10.5pt;font-family:Consolas;color:darkblue;border:none windowtext 1.0pt;padding:0in'>return</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'> </span><span style='font-size:10.5pt;font-family:Consolas;color:maroon;border:none windowtext 1.0pt;padding:0in'>0</span><span style='font-size:10.5pt;font-family:Consolas;color:black;border:none windowtext 1.0pt;padding:0in'>;<br>}</span><span style='font-size:10.5pt;font-family:Consolas;color:black'><o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I am calling XGrabKeyboard to capture the keyboard as the application who created the window might have the keyboard events already grabbed. With the above mentioned code I am able to grab the keyboard but am unable to receive either of KeyPress or KeyRelease events for any keys on the keyboard inside the while loop.<o:p></o:p></p><p class=MsoNormal>Is there anything that I am missing in the code due to which I am unable to receive the events ? <o:p></o:p></p><p class=MsoNormal>Any help is highly appreciated.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Thanks<o:p></o:p></p><p class=MsoNormal>-Harshad<o:p></o:p></p></div></body></html>