Keysym to modifiers state

Jim Gettys jg at freedesktop.org
Wed Nov 2 07:59:56 PST 2005


You are still telling us *what* you are doing, rather than *why* and
your *goal* is....

1) many applications will ignore keyboard presses sent via SendEvent,
as it can be a security hole (e.g. terminal emulators do so routinely).

2) if you want a capital A, on a conventional keyboard, I'd expect you
to send a XK_A keysym, and *set the shift modifier* in the same event.
The code in Xlib would presume that a western A keysym should get
converted to a lower case "a" if shift is not set.

Uppercase is a fiction of western languages, and shift in this form does
not necessarily mean anything in other languages.

The reason keyboard events include modifier bits is that tracking the
state of modifier keys can be difficult, and in earlier versions of X,
we found that many applications just got the wrong answer and didn't
track the state of modifier keys reliably.
				Regards,
					- Jim


On Wed, 2005-11-02 at 17:44 +0200, Cvetelin Andreev wrote:
> Hi Jim,
> 
> I thought that my goal was clear.
> I've asked "So is there a way to get the modifiers state from the keysym?".
> For example how could i differ XK_A and XK_a.
> For both keysym XKeysymToKeycode returns one and the same value.
> 
> XSendEvent requires KeyEvent structure as parameter.
> KeyEvent has 'keycode' and 'state' fields. I can fill the 'keycode' field  
> with XKeysymToKeycode, but what about the 'state' field?
> 
> I'll explain it in details
> 
> I want to have function sendEvent
> 
> void sendEvent(int down, Keysym key)
> 
> where down indicates KeyPress or KeyRelease event and 'key' defines a  
> keysum.
> 
> This function sends a KeyEvent to the window that has the input focus
> 
> If I use XTest extension the function will looks like that:
> 
> void sendEvent(Bool down, KeySym key)
> {
>          KeyCode k;
>          int isbutton = 0;
> 
>          k = XKeysymToKeycode(dpy, (KeySym) keysym);
> 
>          if (k != NoSymbol)
>          {
>                  XTestFakeKeyEvent(dpy, k, down, CurrentTime);
>                  XFlush(dpy);
>          }
> }
> 
> Or if I don't use XTest extension:
> void sendEvent(Bool down, KeySym key)
> {
>          KeyCode k;
> 
>          k = XKeysymToKeycode(dpy, (KeySym) keysym);
> 
>          if (k != NoSymbol)
>          {
>                  XKeyEvent event;
> 
>                  event.display = dpy;
>                  event.window = root;
>                  event.root = root;
>                  event.subwindow = None;
>                  event.time = CurrentTime;
>                  event.x = 0;
>                  event.y = 0;
>                  event.x_root = 0;
>                  event.y_root = 0;
>                  event.same_screen = True;
> 
>                  event.type = down ? KeyPress : KeyRelease;
>                  event.keycode = k;
>                  event.state = 0;
> 
>                  XSendEvent(event.display, InputFocus, True,  
> KeyPressMask|KeyReleaseMask, (XEvent *)&event);
> 
>                  XFlush(dpy);
>          }
> }
> 
> Look at the second implementation (no XTest extension).
> there is an assignment event.state = 0; which tells that there is no  
> modifiers mask. No shift/ctrl/alt/Mod etc was pressed.
> 
> The problem is the following:
> 
> What happens if you call the function as shown:
> 
> sendEvent(True, XK_a);
> sendEvent(False, XK_a);
> 
> ?
> 
> The answer is: The input window will recieve XKeyEvent and (if possible)  
> will display 'a' character.
> 
> What happens if you call the function like this: (with XK_A keysym, not  
> with XK_a)
> 
> sendEvent(True, XK_A);
> sendEvent(False, XK_A);
> ?
> 
> The result will be the same: printed 'a' character.
> 
> But in the second case the expected result is A (uppercase).
> 
> Why is that?
> The answer is in the assignmen event.state = 0;
> 
> If you pass event.state = ShiftMask; then the output will be the response  
> key with Shiftmask (upprecase).
> 
> How can I determine when to pass ShiftMask, when to pass ContorlMask ot  
> ModMask?
> 
> 
> The solution i found:
> If I want capital 'a' - 'A' here is the sequence i must perform:
> 
> sendEvent(True, XK_Shift_L);
> sendEvent(True, XK_a);
> sendEvent(False, XK_a);
> sendEvent(False, XK_Shift_L);
> 
> I hope I was clear enough.
> 
> 
> On Wed, 02 Nov 2005 15:51:30 +0200, Jim Gettys <jg at freedesktop.org> wrote:
> 
> > How the list of keysyms and modifiers in an event gets mapped to a
> > particular keysym is a matter of convention and input method; I'm not up
> > on the exact details used under what circumstances, a lot of the I18N
> > work having gone on when I was doing things other than X.  This can get
> > quite complex for some languages, involving multiple key presses (e.g.
> > many Asian languages).  So I don't have a simple answer for you.
> >
> > Arguably, there should be some way in X to send (cognizant) clients a
> > unicode character: at the moment, there is not any I'm aware of, beyond
> > sending a keypress event having previously defined a keysym out of the
> > unicode part of the keysym address space.
> >
> > Anyone else on the list have anything to help Cvetelin with?
> >
> > Cvetelin, what *exactly* are you trying to achieve?  Understanding your
> > *goal* would help people help you, and it is very unlikely you are
> > trying to solve a problem that hasn't been solved before.  Just asking
> > how you do something in a preconceived fashion isn't very enlightening
> > to the mailing list.
> > 				- Jim Gettys
> >
> >
> >
> > On Wed, 2005-11-02 at 15:20 +0200, Cvetelin Andreev wrote:
> >> Tell me where to find some help for my problem please.
> >>
> >> On Tue, 01 Nov 2005 14:51:10 +0200, Cvetelin Andreev
> >> <cvetelin.a at komero.net> wrote:
> >>
> >> > Hi all,
> >> >
> >> > After a week researching did not find some answers.
> >> >
> >> > I want to send a KeyEvent with XSendEvent with specified keysym. But  
> >> the
> >> > KeyEvent structure requres keycode (which is system dependant) and
> >> > modifiers. I can obtain the keycode from the keysym with
> >> > KKeysymToKeycode,
> >> > but what about modifiers?
> >> > A solution is to get the keycode, and to test this keycode with all
> >> > modifiers masks for equality with the original keysym with
> >> > XKeycodeToKeysym (or ewith constructing event with each modifiers
> >> > combination and test it with XLookupString). However the combinations  
> >> of
> >> > modifiers are large number.
> >> >
> >> > So is there a way to get the modifiers state from the keysym?
> >> >
> >>
> >>
> >>
> >
> > _______________________________________________
> > xorg mailing list
> > xorg at lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/xorg
> >
> 
> 
> 




More information about the xorg mailing list