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.
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.
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.
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.
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);
......
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With