bug-xnee
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-xnee] Update on bug 8138


From: Henrik Sandklef
Subject: Re: [Bug-xnee] Update on bug 8138
Date: Thu, 1 Apr 2004 12:05:50 +0200 (CEST)
User-agent: SquirrelMail/1.5.0

Hi!

I have tested both solution. Both have drawbacks :(

With my solution we have to create an event to lookup what keysym will be
generated with keycode+komdifier(s).

With your solution we have to create a table with mappings such as
  "slash" <----> "/"

Correct me if I am wrong!!!!!!!!!!!!!!!!




My testcode (not very nice or "fail safe", I know). To use it add
arguments such as "\" (really you need to type "\\") "/" "@" ....only the
first arg is used :(


#include <stdio.h>
#include <Xlib.h>
#include <X.h>
#include <Xutil.h>
#include <keysym.h>
#include <keysymdef.h>

static XModifierKeymap *map = NULL;
  int masks[] =
    {
      0,
      ShiftMask,
      ControlMask,
      LockMask,
      Mod1Mask,
      Mod2Mask,
      Mod3Mask,
      Mod4Mask,
      Mod5Mask,
      -1
    } ;


  char *mask_names[] =
    {
      "none" ,
      "shift",
      "control",
      "lock",
      "mod1",
      "mod2",
      "mod3",
      "mod4",
      "mod5",
      "-1",
      "-1"
    };


void
key_to_ascii (Display *dpy,
              KeySym *keysym,
              int keycode,
              int mod_state,
              char *string,
              char *pretty)
{
  XEvent event;
  int size;
  int i ;
  char *p = pretty ;

  event.xkey.type = KeyPress;
  event.xkey.display = dpy ;
  event.xkey.time = CurrentTime;
  event.xkey.x = event.xkey.y = 0;
  event.xkey.x_root = event.xkey.y_root = 0;
  event.xkey.state = mod_state;
  event.xkey.keycode = keycode;

  size = XLookupString ((XKeyEvent *) &event, string, 50, keysym, 0);
  string [size] = 0;
  for (i = 0; i < size; i++)
    {
      unsigned char c = (unsigned char) string [i];
      unsigned char hic = 0;


      if (c >= 0200)
        {
          *p++ = 'M';
          *p++ = '-';
          hic = c;
          c -= 0200;
        }

      if (c < 040)
        {
          *p++ = '^';
          *p++ = c + ('A'-1);
          if (! hic) hic = c;
        }
      else if (c == 0177)
        {
          *p++ = '^';
          *p++ = '?';
          if (! hic) hic = c;
        }
      else
        *p++ = c;
    }
  *p = 0;
}

int
joe(Display *dpy, KeySym req_keysym, char *keystring)
{
  int index;
  int return_value;
  char *check_string;
  KeyCode check_keycode;
  KeySym check_keysym;
  KeyCode keycode;

  index = 1;
  keycode = XKeysymToKeycode(dpy,req_keysym);
  printf ("J keycode = %d \n", keycode);
  for (index=0;index<8;index++)
    {
      check_keysym = XKeycodeToKeysym(dpy, keycode, index);
      check_string = XKeysymToString(check_keysym);
      if (check_string!=NULL)
        {
          printf ("........ is  '%s' == '%s'  ?????\n", check_string, 
keystring);
          if (strcmp(check_string, keystring ) == 0 )
            {
              printf ("........ the power of equality '%s' '%s'  :)\n",
check_string, keystring);
              break;
            }
        }
    }
  printf (" modifier index=%d\n", index);
  return return_value;
}


int
hesa(Display *dpy, KeySym req_keysym, char *keyname)
{
  KeySym ksym ;
  int ks_per_kc;
  int i ;
  int j ;
  int k ;
  int keycode = 0;
  char buf[50];
  char buf2[50];
  int mod_index = 0;



  keycode = XKeysymToKeycode(dpy,req_keysym);
  printf ("H keycode = %d \n", keycode);


  for (i=0;masks[i]!=-1;i++)
    {
      key_to_ascii (dpy, &ksym, keycode, masks[i],
                    buf, buf2);
/*        printf ("keycode=%d i=%d   argv=%s  mask=%d    '%s'   '%s' \n", 
 */
/*                keycode, i,keyname,  */
/*                masks[i], buf, buf2);  */

      if (strcmp(keyname,buf)==0)
        {
          break;
        }
    }

  if ( i == 0 )
    {
      printf ("H modifier = 0\n");
    }
  else if ( masks[i] == -1 )
    {
      printf (" error when finding mods\n");
    }
  else
    {
      KeySym ks ;
      char *nm ;
      k = (i-1)*map->max_keypermod ;
      ks = XKeycodeToKeysym(dpy,
                            map->modifiermap[k],
                            0);
      nm = XKeysymToString(ks);



      printf ("H keycode = %d ",map->modifiermap[k]);
      printf ("\n");
    }


}

int main(int argc, char **argv)
{
  char *ks ;
  Display *dpy = XOpenDisplay(NULL);

  int TEST_KEYSYM ;
  int k =0;

  map = XGetModifierMapping(dpy);

  if (argc==1)
    TEST_KEYSYM = XK_slash;
  else
    {
      if (strncmp(argv[1],"amp",3)==0)
        {
          TEST_KEYSYM = XK_ampersand;
        }
      else if (strncmp(argv[1],"que",3)==0)
        {
          TEST_KEYSYM = XK_question;
        }
      else if (strncmp(argv[1],"back",4)==0)
        {
          TEST_KEYSYM = XK_backslash;
        }
      else if (strncmp(argv[1],"\\",4)==0)
        {
          TEST_KEYSYM = XK_backslash;
        }
      else if (strncmp(argv[1],"/",4)==0)
        {
          TEST_KEYSYM = XK_slash;
        }
      else if (strncmp(argv[1],"a",1)==0)
        {
          TEST_KEYSYM = XK_a;
        }
      else if (strncmp(argv[1],"@",1)==0)
        {
          TEST_KEYSYM = XK_at;
        }
      else if (strncmp(argv[1],"shiftl",6)==0)
        {
          TEST_KEYSYM = XK_Shift_L;
        }
      else if (strncmp(argv[1],"shiftr",6)==0)
        {
          TEST_KEYSYM = XK_Shift_R;
        }
      else if (strncmp(argv[1],"plus",4)==0)
        {
          TEST_KEYSYM = XK_plus;
        }
      else
        {
          TEST_KEYSYM = XStringToKeysym(argv[1]);
        }
    }



  hesa(dpy, TEST_KEYSYM, argv[1]);
  joe(dpy, TEST_KEYSYM, argv[1]);

  XCloseDisplay(dpy);

}







> Henrik -
>
> As I wrote in my previous email, I have only tested this concept with
> regular keys and key with the SHIFT modifier. If expanded to work with
> all modifiers (which I can't test since my keyboards don't require the
> use of other modifiers in their standard configuration) my idea is
> remarkably similar to yours.
>
> 0. Create a modifier map with XGetModifierMapping.
> 1. Read a character from a file and get it's keysym string. ("slash",
> "question", "space" , etc.)
> 2. Get the keycode based on the keysym obtained in step 1.
> 3. Start with a modifier index of 0 (which will be a key without
> modifiers).
> 4. Perform a XKeycodeToKeysym on the keycode obtained in step 2 and the
> modifier index.
> 5. Perform a XKeysymToString on the keysym obtained in step 4.
> 6. If the keysym obtained in step 5 is the same as the keysym obtained
> in step 1, then we have a match. If not (and we still have more
> modifiers to check), increment the modifier index and go back to step 4.
> 7. If step 6 found a match, we now have the keysym and the modifier and
> are ready to generate a fake key event.
>
> My solution attempts to avoid generating events and the interpreting
> them. Even though my solution has more steps, I think would actually be
> simpler to implement. I don't claim that what I am attempting will work
> for anything other than standard keys and ones that require pressing the
> SHIFT key.
>
> Here is the test code that I wrote. It only checks for the results of
> the SHIFT key modifier. If it doesn't find a match, it assumes a key
> with no modifiers. Obviously, the real world is more complicated.
> Nevertheless, if the concepts behind my solution are correct, you should
> have no problem changing the code to check all modifiers.
>
> int
> xnee_shiftkeysetting(xnee_data *xd, char *keystring, xnee_key_code *kc)
> {
>
>   int index;
>   int return_value;
>   char *check_string;
>   KeyCode check_keycode;
>   KeySym check_keysym;
>
>   index = 1;
>   check_keysym = XKeycodeToKeysym(xd->fake, kc->kc, index);
>   check_string = XKeysymToString(check_keysym);
>   xnee_verbose ((xd, "The character to check is %s\n", keystring));
>   xnee_verbose ((xd, "The shifted version is %s\n", check_string));
>   if (strcmp(check_string, keystring ) == 0 )
>     {
>     return_value = 1;
>     xnee_verbose ((xd, "The character to retype is shifted!\n"));
>     }
>   else
>     {
>     return_value = 0;
>     xnee_verbose ((xd, "The character to retype is NOT shifted!\n"));
>     }
>     return return_value;
> }
>
> Here is a sample call of that routine from a modified version of
> xnee_char2keycode. I am sure you will recognize the code segment despite
> my changes.
>
>     case '/':
>       /* kc->shift_press=1; */
>       kc->kc = xnee_str2keycode(xd,"slash");
>       kc->shift_press = xnee_shiftkeysetting(xd, "slash", kc);
>       break;
>
> In theory, this should work on any keyboard that doesn't require XIM.
>
> So, what do you think? Is this a viable solution or did I just wander
> down a dead end?
>
> Also, thank you for the output of xmodmap. It gave me a little more
> insight into the relationship between keysyms and keycodes.
>
> - Joe
>
>
>>
>>
>
>


-- 
"There is no system but Gnu and Linux is one of its kernels"
   - rms -




reply via email to

[Prev in Thread] Current Thread [Next in Thread]