home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / windows / x / 15332 < prev    next >
Encoding:
Text File  |  1992-08-17  |  3.0 KB  |  89 lines

  1. Newsgroups: comp.windows.x
  2. Path: sparky!uunet!gatech!bloom-beacon!INTERNET!dont-send-mail-to-path-lines
  3. From: deveau@oceanroutes.ns.ca ("Terry J. Deveau")
  4. Subject: Re: Keyboard input with X
  5. Message-ID: <9208172009.AA24116@expo.lcs.mit.edu>
  6. Sender: deveau@oceanroutes.ns.ca
  7. Organization: Oceanroutes Canada Inc.
  8. References: <1992Aug15.160543.28140@msuinfo.cl.msu.edu>
  9. Date: Mon, 17 Aug 1992 20:09:31 GMT
  10. Lines: 77
  11.  
  12. > Can someone tell me how to determine which keys have been pressed w/i and X
  13. > program? 
  14. > The book I'm using says to use the function XLookupString() in the
  15. > event->xkey.keycode structure, BUT it doesn;t say how to. I wrote this code to
  16. > determine if the last key pressed was a space, but all my attempts end with a seg
  17. > fault....
  18. >   if (XLookupString (event->xkey.keycode) == ' ')
  19. >     {
  20. >       etc.
  21. >     } 
  22. > I would be grateful for any insight into this problem someone can give me.
  23. The following will give you an idea of how I do it; sorry about not sending
  24. a piece of actual working code, but I pulled this together from a more
  25. complex function.
  26.  
  27.   char cmd[80], *ptr, *ptrc, *chr;
  28.   Window win;
  29.   XSetWindowAttributes Wattrib;
  30.   XGCValues GCval;
  31.   GC GCa;
  32.   XCharStruct extent;
  33.   XEvent evnt;
  34.   long compse[5];
  35.   XSizeHints szHints;
  36.   KeySym ks;
  37.  
  38.     XNextEvent (dsp, &evnt);
  39.     switch (evnt.type) {
  40.       case ButtonPress : XRaiseWindow (dsp, win); break;
  41.       case KeyPress : nc = XLookupString (&(evnt.xkey),cmd,sizeof cmd,NULL,compse);
  42.         if (!str) { nc = -1; break; }
  43.         if (IsCursorKey(ks=XLookupKeysym(&(evnt.xkey),0))) {
  44.           int in, jn, go = 1, k = -1, kn, i, j, nch=0, ich[10];
  45.  
  46.           switch (ks) {
  47.             case XK_Up : if (sz==1) { k = 0; kn = 1; } break;
  48.             case XK_Down : if (sz==1) { k = data->nopt-1; kn = -1; } break;
  49.             case XK_Right : if (sz>1) { ... }
  50.               break;
  51.             case XK_Left : if (sz>1) { ... }
  52.               break;
  53.           } }
  54.         else {
  55.           switch (ks) {
  56.             case XK_Delete :
  57.             case XK_DeleteChar : if (sz>1) { ... }
  58.               break; 
  59.           default :
  60.           for (chr=cmd; chr-cmd<nc; chr++) {
  61.             switch (*chr) {
  62.               case '\b' : ...
  63.                 break;
  64.               case '\r' : nc = -1; break;
  65.               default : if (ptr-str==sz) XBell (dsp, 100);
  66.                 else {
  67.                   if (sz>1) { ... }
  68.                   else if (NULL==strchr(str,*chr)) XBell (dsp, 100);
  69.                   else {
  70.                     *str = *chr;
  71.                     nc = -1; } } } } } }
  72.         break;
  73.       case MappingNotify : XRefreshKeyboardMapping (&(evnt.xmapping)); break;
  74.       case Expose : if (!evnt.xexpose.count) {
  75.           paintWindow(dsp, win, &GCa, data);
  76.           if (str && sz>1) {
  77.             XSetForeground (dsp, GCa, colour_text);
  78.             XDrawString (dsp, win, GCa, left, line, str, ptr-str);
  79.             XSetForeground (dsp, GCa, colour_cursor);
  80.             XDrawString (dsp, win, GCa, cursor-2, line, "_", 1); } }
  81.         break; }
  82.  
  83.