kelan has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use the SendEvent method from the X11::Protocol module, and I can't seem to get it to work. My thoughts on why are either that:
a) the app I'm sending events to is ignoring them, which doesn't seem likely since xse works fine, or
b) I'm not building the %event hash correctly that gets sent, so the event isn't being executed.

If anyone's ever used the SendEvent method successfully and has any tips, I'd be very grateful. Or if you happen to know where some good documentation for the module is, that would be wonderful as well. (In particular, a better explanation of what exactly needs to be put into an %event hash for correct usage.)

Thanks in advance.

Replies are listed 'Best First'.
Re: SendEvent in X11::Protocol
by PodMaster (Abbot) on Aug 17, 2002 at 09:16 UTC
    Hmm, I've never used the module, but I took a look at it's documentation and it seems to contain more than adequate information.

    You can find a copy at http://search.cpan.org/author/SMCCAM/X11-Protocol-0.04/Protocol.pm as well as various other places.

    If you didn't know how to find the link I give above, I suggest reading this friendly guide to various perl documentation and resources.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: SendEvent in X11::Protocol
by RMGir (Prior) on Aug 17, 2002 at 12:18 UTC
    I think if you post the code you're trying to make work, we'll have a better chance of being able to help.

    Otherwise, all we can do is point you at the docs, and I get the feeling you've read them already.
    --
    Mike

      Well all I'm trying to do right now is send a letter to another window to a get feel for it. This little snippet is trying to send a letter 'a' to window with ID of 0x1800002. Here's what I have right now:
      #!/usr/bin/perl use strict; use X11::Protocol; my $x = X11::Protocol->new; my %event = ( name => 'KeyPress', event => 97, state => 0, code => 2, event_x => 0, event_y => 0, detail => 0, same_screen => 1, root => $x->{'root'}, child => 'None', time => 1029571004, root_x => 0, root_y => 0, sequence_number => 0, ); my $event_string = $x->pack_event(%event); $x->SendEvent(0x1800002, 1, 0xfff, $event_string);
        Looks sane to me...

        Check the return values of new, pack_event, and SendEvent. Maybe one of them is failing?
        --
        Mike

        Well I figured it out, mostly. I thought that the event field of the hash was used for the event to send, but now I know it isn't. I'm not sure what it is for, perhaps an event ID.

        The detail field is where you can specify what to send for a KeyPress event. But it's not the ASCII value, or even the X keysym (which, for letters, are mapped to ASCII anyway). The detail field actually takes a keycode, which is simply a number representing a certain keyboard key.

        All the keys on a keyboard are coded according to your modmap, and it doesn't really have any relation to what the key being pressed is supposed to do, it just says "Key #25 was pressed". Internally, X translates the keycode (specific to your keyboard setup) to the keysym (portable across X), depending on what is in the state field, so that the statement becomes "The letter 'w' was given".

        The state field tells what modifier keys should be taken into consideration. 0 means none, 1 means SHIFT (so you'd get 'W' instead of 'w'), etc. It's probably taken as a bit vector to specify combinations of SHIFT, ALT, CTRL, META, and SUPER, but I didn't test fully to find out which bit is which. I'm not exactly sure what the code field does, but it seems to be another indicator of the event type. 0 and 1 are invalid, 2 seems to work for keypresses, and anything higher doesn't seem to do anything (in my very limited testing) except for 15 and 18, which specify events for window notifications (focus in/out, and map/unmap I think).

        The other fields are rather self-explanatory, except for sequence_number about which I have no idea. In this module, the time field can also be given 'CurrentTime', which does the obvious, although I'm not sure X cares about the time.

        I'm still having a problem getting the event to be delivered to the correct window, but I haven't done extensive testing. Updates as they come.

        PS. You can get a look at your modmap to find out your specific keycodes by using the command: xmodmap -pk