tags:

views:

624

answers:

4

First, yes I know about this question, but I'm looking for a bit more information that that. I have actually, a fairly similar problem, in that I need to be able to capture input for mouse/keyboard/joystick, and I'd also like to avoid SDL if at all possible. I was more or less wondering if anyone knows where I can get some decent primers on handling input from devices in Linux, perhaps even some tutorials. SDL works great for cross-platform input handling, but I'm not going to be using anything else at all from SDL, so I'd like to cut it out altogether. Suggestion, comments, and help are all appreciated. Thanks!

Edit for clarity:

The point is to capture mouse motion, keyboard press/release, mouse clicks, and potentially joystick handling for a game.

+1  A: 

You could start by reading This Tutorial form The Linux Documentation Project

dsm
+1  A: 

You didn't state the purpose of your application but reading raw data from the input devices is seldom what you want unless you just do it for fun (or school project or whatever)

If you're writing a GUI application (one that runs in an X11 graphical environment) you can rely on the graphical toolkits (or raw X) input drivers.

If you're writing a text-mode client, then maybe readline or even ncurses could be good alternatives.

Isak Savo
A: 

If you know your project will only be run under Linux (not Windows or even one of the BSDs), you should look into the Linux kernel's input system. Download the kernel source and read Documentation/input/input.txt, particularly the description of the evdev system.

For a significantly higher-level (and more portable) solution, read up on Xlib. Obviously it requires a running X server, but it has the advantage of inheriting the user's keyboard settings. Joystick events are unfortunately not included, you'd probably need to use the kernel joystick API for those.

skymt
+1  A: 

Using the link below look at the function void kGUISystemX::Loop(void)

This is my main loop for getting input via keyboard and mouse using X Windows on Linux.

http://code.google.com/p/kgui/source/browse/trunk/kguilinux.cpp

Here is a snippet:

 if(XPending(m_display))
 {
  XNextEvent(m_display, &m_e);
  switch(m_e.type)
  {
  case MotionNotify:
   m_mousex=m_e.xmotion.x;
   m_mousey=m_e.xmotion.y;
  break;
  case ButtonPress:
   switch(m_e.xbutton.button)
   {
   case Button1:
    m_mouseleft=true;
   break;
   case Button3:
    m_mouseright=true;
   break;
   case Button4:/* middle mouse wheel moved */
    m_mousewheel=1;
   break;
   case Button5:/* middle mouse wheel moved */
    m_mousewheel=-1;
   break;
   }
  break;
  case ButtonRelease:
   switch(m_e.xbutton.button)
   {
   case Button1:
    m_mouseleft=false;
   break;
   case Button3:
    m_mouseright=false;
   break;
   }
  break;
  case KeyPress:
  {
   XKeyEvent *ke;
   int ks;
   int key;

   ke=&m_e.xkey;
   kGUI::SetKeyShift((ke->state&ShiftMask)!=0);
   kGUI::SetKeyControl((ke->state&ControlMask)!=0);
   ks=XLookupKeysym(ke,(ke->state&ShiftMask)?1:0);
......
KPexEA