The officially official Devuan Forum!

You are not logged in.

#1 2017-10-10 13:06:06

GNUser
Member
Registered: 2017-03-16
Posts: 561  

How to send a signal whenever mouse button is clicked? [SOLVED]

I'd like to rig my mouse so that any time I left-click, not only do I get a normal left click but also send a signal to a script.

I tried both of these in my xbindkeys, but with them I lose the ability to use the mouse click altogether (i.e., I can move the mouse but nothing happens when I click on things):

"xdotool click 1; pkill -USR1 somescript"
  b:1 + release
"xvkbd -text "\m1"; pkill -USR1 somescript"
  b:1 + release

I tried adding a short delay to the command. Also tried just b:1 without "+ release". No luck with either.

Any ideas? I've been going around in circles with this. A CLI solution would be preferable.

Last edited by GNUser (2017-10-10 17:13:37)

Offline

#2 2017-10-10 15:20:52

fsmithred
Administrator
Registered: 2016-11-25
Posts: 2,409  

Re: How to send a signal whenever mouse button is clicked? [SOLVED]

This is not exactly what you're looking for, but maybe it can be adapted or maybe it will give you an idea. I set up xscreensaver to run a script that would stop if I clicked the mouse or keyboard. You could either have it run/stop somescript or have it run a script that did something with somescript. Maybe looking in the xscreensaver source code will give you ideas, too.
http://forums.debian.net/viewtopic.php? … 55#p591933

Offline

#3 2017-10-10 17:11:56

GNUser
Member
Registered: 2017-03-16
Posts: 561  

Re: How to send a signal whenever mouse button is clicked? [SOLVED]

Thanks, fsmithred. I took a look at xscreensaver's code. I'm too lazy to parse through all that smile

I found a solution. It requires that python3 and the pyuserinput module (which contains pymouse) be installed.

#!/usr/bin/python3
from pymouse import PyMouseEvent
import os
class ClickDetector(PyMouseEvent):
    def __init__(self):
        PyMouseEvent.__init__(self)
    def click(self, x, y, button, press):
        '''Send signal when left click is pressed.'''
        if button == 1:
            if press:
                os.system('pkill -USR1 somescript')
ClickDetector().run()

If the above is running in the background, my mouse works normally and any time I left-click on anything a SIGUSR1 is sent to somescript.

Perhaps you can adapt the above and use it instead of xscreensaver for your purposes, although as-is it does not respond to keyboard input.

Last edited by GNUser (2017-10-10 17:22:53)

Offline

#4 2017-10-10 23:59:46

GNUser
Member
Registered: 2017-03-16
Posts: 561  

Re: How to send a signal whenever mouse button is clicked? [SOLVED]

@fsmithred - If you just need your system to do something for you (e.g., stop a script) when coming out of idle (i.e., when there is any keyboard or mouse activity), you may like this: https://unix.stackexchange.com/a/122816

I tried it and it worked like a charm. I just needed to install libxss-dev (provides scrnsaver.h) before compiling.

Last edited by GNUser (2017-10-11 00:00:27)

Offline

#5 2017-10-11 11:05:20

fsmithred
Administrator
Registered: 2016-11-25
Posts: 2,409  

Re: How to send a signal whenever mouse button is clicked? [SOLVED]

Thanks. I don't actually use that, but I'm glad to know about it. It might come in handy some time.

Offline

#6 2017-10-11 12:38:51

ralph.ronnquist
Administrator
From: Clifton Hill, Victoria, AUS
Registered: 2016-11-30
Posts: 1,106  

Re: How to send a signal whenever mouse button is clicked? [SOLVED]

A smaller footprint  solution would simply read byte triplets from /dev/input/mice, where <9,0,0> means left button down, and <8,0,0> means left button up; all other triplets mean other things. I would use newlisp, of course, to remain in small foot print scripting, but a small C program could also do.

Offline

#7 2017-10-12 18:40:23

GNUser
Member
Registered: 2017-03-16
Posts: 561  

Re: How to send a signal whenever mouse button is clicked? [SOLVED]

Thanks, ralph.ronnquist. I'll explore that option.

In the meantime, I found yet another gem. Here is a python script that sends a signal to somescript whenever there is any mouse or keyboard activity:

from pynput import keyboard
from pynput import mouse
import os

def send_signal()
        os.system('pkill -USR1 somescript')

def on_press(key): 
	send_signal()

def on_move(x, y):
	send_signal()

def on_click(x, y, button, pressed):
	send_signal()

def on_scroll(x, y, dx, dy):
	send_signal()

with mouse.Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
	with keyboard.Listener(on_press=on_press) as listener:
		listener.join()

It would be easy to adapt the above to only send the signal in a subset of cases (e.g., only on left clicks).

Last edited by GNUser (2017-10-12 19:02:04)

Offline

Board footer