geekman92
Posts: 14
Joined: Wed Sep 12, 2012 10:18 am

Possible to capture mouse input on the CLI?

Thu Dec 29, 2016 12:17 pm

I've just ordered a small PiTFT touchscreen HAT for my headless RPi to do simple tasks such as connecting the RPi to a new network or start a specific program etc. To save resources I want to avoid using X Server and instead create a simple text-based GUI program using something like ncurses or newt. However, I don't want to have to attach a keyboard to control the program when I have a touchscreen available (which I assume just acts as a mouse).

Is it possible for the RPi console to capture the mouse input (and subsequently use that input in a program like ncurses or newt)?

I have also seen you can use PyGame to write directly to the Framebuffer. Is it still possible to capture the mouse events in PyGame if writing directly to the Framebuffer?

I'm not sure if I need an X Server to do anything mouse related....

Anyway, thanks in advance for any input on this. =)

JumpZero
Posts: 1219
Joined: Thu Mar 28, 2013 7:35 pm
Location: Arcachon, France

Re: Possible to capture mouse input on the CLI?

Thu Dec 29, 2016 3:29 pm

geekman92 wrote:Is it still possible to capture the mouse events in PyGame if writing directly to the Framebuffer?
Yes it is. With Pygame. I have a webradio with touchscreen doing this. I followed this adafruit guide: https://learn.adafruit.com/raspberry-pi ... n/overview
You can adapt the code to your needs.
--
Jmp0

User avatar
scruss
Posts: 3854
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Possible to capture mouse input on the CLI?

Thu Dec 29, 2016 3:39 pm

For other ways of reading mouse position, there's the old gpm mouse daemon, and you can also send Mousetraps in a shell script.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

AndyPi
Posts: 83
Joined: Wed Feb 22, 2012 10:24 pm
Location: Gloucestershire,UK

Re: Possible to capture mouse input on the CLI?

Thu Dec 29, 2016 3:48 pm

ncurses supports mouse operations seehttp://www.tldp.org/HOWTO/NCURSES-Progr ... mouse.html. Alternatively you can read the mouse device /dev/input/mice

geekman92
Posts: 14
Joined: Wed Sep 12, 2012 10:18 am

Re: Possible to capture mouse input on the CLI?

Sun Jan 15, 2017 12:25 am

Sorry for not replying to this before, NYE prep and the start of 2017 put this project on hold!

I am very grateful for all your answers. Think I am going to look into ncurses as there is a Python library I can use. =)

User avatar
PeterO
Posts: 6095
Joined: Sun Jul 22, 2012 4:14 pm

Re: Possible to capture mouse input on the CLI?

Sun Jan 15, 2017 8:33 am

There's always another way :-)
Raw mouse events can also be read from appropriate devices in /dev/input. /dev/input/by-id has more "User friendly" symlinks to the devices.
https://www.kernel.org/doc/Documentatio ... /input.txt has details of how to interpret the event stream.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Heater
Posts: 17447
Joined: Tue Jul 17, 2012 3:02 pm

Re: Possible to capture mouse input on the CLI?

Sun Jan 15, 2017 8:55 am

What ever happened to /dev/mouse ?

I recall a time when I could do:

$ cat /dev/mouse

Cat, mouse, get it :) Then a lot of random junk would be printed as I wiggled the mouse.

Basically any program could read that device and interpret the data.

This debian machine has:

/dev/input/mice
/dev/input/mouse0
/dev/input/mouse1

I can't cat any of those, permission denied, even as root.
Memory in C++ is a leaky abstraction .

User avatar
PeterO
Posts: 6095
Joined: Sun Jul 22, 2012 4:14 pm

Re: Possible to capture mouse input on the CLI?

Sun Jan 15, 2017 9:10 am

Heater wrote:What ever happened to /dev/mouse ?
It got replaced by the "input" system which unifies different input devices.

I can't cat any of those, permission denied, even as root.
Works fine as root on my Mint-17.3 box

On Mint the ownership is root:root. On Raspbian it is root:input so "pi" can read them.
I used /dev/input for my rubik's cube demo http://www.peteronion.org.uk/video/RubikTouch.mpeg
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Heater
Posts: 17447
Joined: Tue Jul 17, 2012 3:02 pm

Re: Possible to capture mouse input on the CLI?

Sun Jan 15, 2017 11:01 am

You're right. Just tried it again and cat /dev/input/mouse0 does indeed work on my Debian PC. It just hangs there because I'm logged in remotely, to far away to wiggle the mouse !

I must have got that wrong, half asleep as I was at the time.
Memory in C++ is a leaky abstraction .

Return to “General discussion”