Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing Input in Linux

Tags:

c++

linux

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.

like image 972
f4nt Avatar asked Sep 24 '08 07:09

f4nt


People also ask

What is echo $? In Linux?

Echo is a Unix/Linux command tool used for displaying lines of text or string which are passed as arguments on the command line. This is one of the basic command in linux and most commonly used in shell scripts.

What does input do Linux?

In Linux, when you enter a command as an input, you receive an output. It's the basic workflow of Linux. The standard input or stdin device to give commands is the keyboard and the standard output or stdout device is your terminal screen. With redirection, you can change the standard input/output.

What is $1 $2 in shell script?

Shell scripts have access to some "magic" variables from the environment: $0 - The name of the script. $1 - The first argument sent to the script. $2 - The second argument sent to the script.


2 Answers

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);
......
like image 148
KPexEA Avatar answered Sep 30 '22 04:09

KPexEA


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.

like image 31
skymt Avatar answered Sep 30 '22 05:09

skymt