Re: using keyboard arrows - Xwindows

This is a discussion on Re: using keyboard arrows - Xwindows ; On Mar 12, 11:10 pm, Brice Rebsamen wrote: > Hi > > I am trying to write a small application to control a mobile robot > from the keyboard. What I really need is to handle multiple keys > pressed ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Re: using keyboard arrows

  1. Re: using keyboard arrows

    On Mar 12, 11:10 pm, Brice Rebsamen wrote:
    > Hi
    >
    > I am trying to write a small application to control a mobile robot
    > from the keyboard. What I really need is to handle multiple keys
    > pressed at one time. If it's too difficult with arrow keys I don't
    > mind using another combination like WSAD.


    > Any idea of what would be a good way of doing this?



    Using X11 might be more suitable. Try this little program:

    #include
    #include

    int main()
    {
    Display* d = XOpenDisplay(0);
    Window w, root;
    root = RootWindow(d, DefaultScreen(d));
    w = XCreateSimpleWindow(d, root, 100, 100, 640, 480, 0, 0, 0);
    XSelectInput(d, w, KeyPressMask | KeyReleaseMask);
    XMapWindow(d, w);

    XEvent e;
    for(;
    {
    XNextEvent(d, &e);

    if(e.type == KeyPress || e.type == KeyRelease)
    {
    KeySym k;
    k = XLookupKeysym(&(e.xkey), e.xkey.state);
    printf("keycode=%i key=%s %s\n", e.xkey.keycode,
    XKeysymToString(k), e.type == KeyPress ?"pressed":"released");
    }
    }
    }

    It probably does a goot bit of what you want with regards to key
    presses. Note that XNextEvent is a blocking call, which you probably
    don't want. However, the non-blocking call XPending(d) tells you if
    XNextEvent will block.


    -Ed
    --
    (You can't go wrong with psycho-rats.)(http://mi.eng.cam.ac.uk/~er258)

    /d{def}def/f{/Times s selectfont}d/s{11}d/r{roll}d f 2/m{moveto}d -1
    r 230 350 m 0 1 179{ 1 index show 88 rotate 4 mul 0 rmoveto}for/s 12
    d f pop 235 420 translate 0 0 moveto 1 2 scale show showpage



  2. Re: using keyboard arrows

    On Mar 14, 3:53 am, Edward Rosten wrote:
    > On Mar 12, 11:10 pm, Brice Rebsamen wrote:
    >
    > > Hi

    >
    > > I am trying to write a small application to control a mobile robot
    > > from the keyboard. What I really need is to handle multiple keys
    > > pressed at one time. If it's too difficult with arrow keys I don't
    > > mind using another combination like WSAD.
    > > Any idea of what would be a good way of doing this?

    >
    > Using X11 might be more suitable. Try this little program:
    >
    > #include
    > #include
    >
    > int main()
    > {
    > Display* d = XOpenDisplay(0);
    > Window w, root;
    > root = RootWindow(d, DefaultScreen(d));
    > w = XCreateSimpleWindow(d, root, 100, 100, 640, 480, 0, 0, 0);
    > XSelectInput(d, w, KeyPressMask | KeyReleaseMask);
    > XMapWindow(d, w);
    >
    > XEvent e;
    > for(;
    > {
    > XNextEvent(d, &e);
    >
    > if(e.type == KeyPress || e.type == KeyRelease)
    > {
    > KeySym k;
    > k = XLookupKeysym(&(e.xkey), e.xkey.state);
    > printf("keycode=%i key=%s %s\n", e.xkey.keycode,
    > XKeysymToString(k), e.type == KeyPress ?"pressed":"released");
    > }
    > }
    >
    > }
    >
    > It probably does a goot bit of what you want with regards to key
    > presses. Note that XNextEvent is a blocking call, which you probably
    > don't want. However, the non-blocking call XPending(d) tells you if
    > XNextEvent will block.
    >
    > -Ed
    > --
    > (You can't go wrong with psycho-rats.)(http://mi.eng.cam.ac.uk/~er258)
    >
    > /d{def}def/f{/Times s selectfont}d/s{11}d/r{roll}d f 2/m{moveto}d -1
    > r 230 350 m 0 1 179{ 1 index show 88 rotate 4 mul 0 rmoveto}for/s 12
    > d f pop 235 420 translate 0 0 moveto 1 2 scale show showpage




    this code is great and is a very good solution to my problem. Can you
    just tell me how to map a key to an action. I am using switch case
    block like this:

    #include
    #include

    switch ( e.xkey.keycode ) {
    case XK_Up: case XK_W: case XK_w: case XK_KP_Up: case XK_KP_8:
    linvel = e.type==KeyPress ? 1 : 0;
    break;
    case XK_Down: case XK_S: case XK_s: case XK_KP_Down: case
    XK_KP_2:
    linvel = e.type==KeyPress ? -1 : 0;
    break;
    case XK_Left: case XK_A: case XK_a: case XK_KP_Left: case
    XK_KP_4:
    rotvel = e.type==KeyPress ? -1 : 0;
    break;
    case XK_Right: case XK_D: case XK_d: case XK_KP_Right: case
    XK_KP_6:
    rotvel = e.type==KeyPress ? 1 : 0;
    break;
    }

    But it does not work. How can I know which key is pressed?

  3. Re: using keyboard arrows

    Brice Rebsamen writes:

    > switch ( e.xkey.keycode ) {
    > case XK_Up: case XK_W: case XK_w: case XK_KP_Up: case XK_KP_8:


    These XK_ constants are keysyms, not keycodes. So you should
    compare them to the values returned by XLookupKeysym, not to
    e.xkey.keycode.

    I suppose it's also possible to examine the keymap beforehand,
    make a list of the interesting keycodes, and then use that. It
    would be more difficult however, especially with the more complex
    keymaps made possible by the X Keyboard Extension (XKB).

  4. Re: using keyboard arrows

    On Mar 14, 3:45 pm, Kalle Olavi Niemitalo wrote:
    > Brice Rebsamen writes:
    > > switch ( e.xkey.keycode ) {
    > > case XK_Up: case XK_W: case XK_w: case XK_KP_Up: case XK_KP_8:

    >
    > These XK_ constants are keysyms, not keycodes. So you should
    > compare them to the values returned by XLookupKeysym, not to
    > e.xkey.keycode.
    >
    > I suppose it's also possible to examine the keymap beforehand,
    > make a list of the interesting keycodes, and then use that. It
    > would be more difficult however, especially with the more complex
    > keymaps made possible by the X Keyboard Extension (XKB).


    Ok, it works well. And it's definitely better than the ioctl
    KDSKBMODE: there is no risk that I mess my keyboard anymore. And the
    code is so much simpler.
    Thanks guys.

+ Reply to Thread