Unix/Linux Go Back    

UNIX and Linux Applications Discuss UNIX and Linux software applications. This includes SQL, Databases, Middleware, MOM, SOA, EDA, CEP, BI, BPM and similar topics.

read position mouse cursor

UNIX and Linux Applications

Thread Tools Search this Thread Display Modes
Old Unix and Linux 09-16-2008
chris75 chris75 is offline
Registered User
Join Date: Sep 2008
Last Activity: 16 September 2008, 1:09 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
read position mouse cursor

Hi to all!
I'm a teacher of maths and physics in an italian high school in Milan, Italy.
I need a simple program that read the position of mouse cursor in function of time and write the coordinates in a text file. The time resolution have to be something like 1/10 sec or better (I have to know this parameter in advance). I can do that with a macro in labview but I can't buy that program. This work will be a part of physics lab in my classroom: we want to plot oscillation (in function of time) of a pendulum followed by an optical mouse.
thank you in advance for help!

Christian Bonfanti
Sponsored Links
Old Unix and Linux 09-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
It's probably better to use a modern framework such as Qt or GTK+ but Googling brought up a few half-promising links about raw X11 mouse coordinates.

X11 Programming - Ubuntu Forums
How to get mouse's position? - Qt Centre Forum

I don't know anything about X11 really but hopefully at least this is a start.
Sponsored Links
Old Unix and Linux 11-04-2008
daggilli daggilli is offline
Registered User
Join Date: Nov 2008
Last Activity: 5 November 2008, 10:52 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

This is the complete code for a Qt application I threw together in about ten minutes (called crosshair) which displays the current mouse coordinates in a window. You might be able to pull enough out of it to be useful. This is Qt 3.1, but Qt 4 is not a great deal different. You will need the Qt development libraries, not just the runtimes. The code comprises two files, crosshair.h and crosshair.cpp.


#include <qwidget.h>
#include <qstring.h>
#include <qlabel.h>
#include <qevent.h>

class Crosshair : public QLabel

    Crosshair(QWidget *parent=0);

    void mousePressEvent(QMouseEvent *);

    QTimer         *timer;

private slots:
    void timerfire();



#include <qapplication.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qcursor.h>
#include <iostream>
#include "crosshair.h"

using namespace std;

int main(int argc,char **argv)
    QApplication a(argc,argv);

    Crosshair mousepos;

    return a.exec();

Crosshair::Crosshair(QWidget *parent) : QLabel(parent)
    timer=new QTimer(this);

void Crosshair::mousePressEvent(QMouseEvent *)

void Crosshair::timerfire()
    QPoint p=QCursor::pos();

To build this, put both files in a directory called crosshair. cd to that directory and type

qmake -project

This does nothing more complex than inherit from a QLabel, set a timer to run 20x a second, grab the current cursor coordinates and write them into the label's text. Clicking in the window closes it. I use it for fixing up alignment bugs in JavaScript when I'm laying out objects.

You could open a file in the Crosshair class's constructor to store your data, and use gettimeofday(2) to get a timestamp. Nothing says Qt has to run in GUI mode (you can tell it explicitly not to in the QApplication constructor).

Qt from Trolltech: http://doc.trolltech.com
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting the cursor position tinman47 Shell Programming and Scripting 3 09-15-2011 08:26 AM
ksh - moving cursor position cesarNZ Shell Programming and Scripting 1 10-01-2009 03:23 AM
Cursor position gefa UNIX for Dummies Questions & Answers 4 05-05-2009 05:21 PM
Cursor Global Position boogy Programming 3 02-12-2007 10:18 AM
Get the cursor position bestbuyernc Shell Programming and Scripting 0 07-27-2005 04:59 PM

All times are GMT -4. The time now is 06:20 AM.