Re: using keyboard arrows
On Mar 12, 11:10 pm, Brice Rebsamen <brice.rebsa...@gmail.com> wrote:[color=blue]
> 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.[/color]
[color=blue]
> Any idea of what would be a good way of doing this?[/color]
Using X11 might be more suitable. Try this little program:
#include <X11/Xlib.h>
#include <stdio.h>
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.)([url]http://mi.eng.cam.ac.uk/~er258[/url])
/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
Re: using keyboard arrows
On Mar 14, 3:53 am, Edward Rosten <Edward.Ros...@gmail.com> wrote:[color=blue]
> On Mar 12, 11:10 pm, Brice Rebsamen <brice.rebsa...@gmail.com> wrote:
>[color=green]
> > Hi[/color]
>[color=green]
> > 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?[/color]
>
> Using X11 might be more suitable. Try this little program:
>
> #include <X11/Xlib.h>
> #include <stdio.h>
>
> 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.)([url]http://mi.eng.cam.ac.uk/~er258[/url])
>
> /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[/color]
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 <X11/Xlib.h>
#include <X11/keysym.h>
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?
Re: using keyboard arrows
Brice Rebsamen <brice.rebsamen@gmail.com> writes:
[color=blue]
> switch ( e.xkey.keycode ) {
> case XK_Up: case XK_W: case XK_w: case XK_KP_Up: case XK_KP_8:[/color]
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).
Re: using keyboard arrows
On Mar 14, 3:45 pm, Kalle Olavi Niemitalo <k...@iki.fi> wrote:[color=blue]
> Brice Rebsamen <brice.rebsa...@gmail.com> writes:[color=green]
> > switch ( e.xkey.keycode ) {
> > case XK_Up: case XK_W: case XK_w: case XK_KP_Up: case XK_KP_8:[/color]
>
> 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).[/color]
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.