Page 1 of 1

How to make a sound every time you click something?

Posted: Sat Mar 16, 2024 8:41 pm
by bob93

Is it possible to have a custom sound play every time you press a mouse button? sort of like how games work. You click certain things on the interface and you hear a certain sound. You right click and you hear another.

If possible I'd like to hear a sound when I left click, and another when I right click. This kind of auditory feedback is very handy for screencasting


Re: How to make a sound every time you click something?

Posted: Sat Mar 16, 2024 10:35 pm
by mikewalsh

@bob93 :-

Aye, it's certainly possible. I have a coupla scripts set-up, making use of MPlayer: one plays an "intro" sound at boot-up.....and the other plays the Win XP 'closing' jingle at shut-down.

It's simple enough to get the sound playing. The only thing I'm not at all certain about is how you get Puppy to 'detect' whenever a mouse button is clicked.....and thus trigger the audio script. Somebody here will know, but I wouldn't like to say who..... :?

Be patient. We'll sort summat out for ya..!

Certainly, this can be done for a 'global' rule for mouse behaviour. But from the sounds of it, you want this behaviour to ONLY occur when you're using specific programs?

============================

....later:-

(xev might be the way to go, then use xbindkeys to associate a particular key-press event with a given audio sound script? Did some reading on StackExchange, and this sounds like the easiest method for ID-ing a given button "event"...)

https://unix.stackexchange.com/question ... or-command

Info about xev/xbindkeys further down the page...

Just throwing this out there. "Me no expert", etc..! :D

Mike. ;)


Re: How to make a sound every time you click something?

Posted: Sun Mar 17, 2024 12:17 am
by bob93
mikewalsh wrote: Sat Mar 16, 2024 10:35 pm

Certainly, this can be done for a 'global' rule for mouse behaviour. But from the sounds of it, you want this behaviour to ONLY occur when you're using specific programs?

Not so much to have it only work while using certain programs, but to be able to turn it on and off whenever I want, if possible. Think of command

Code: Select all

xcalib -i -a

If you want to invert the screen colors you enter the command. When you're done you enter it again. Simple. It doesn't have to be done via a command but you get the idea

I'll check out that link. Thanks


Re: How to make a sound every time you click something?

Posted: Sat Apr 13, 2024 9:40 pm
by bob93

Alright, I have given this a try using xbindkeys. First I had to create file /root/xbindkeysrc. Piece of cake
Then in that file I had to specify the command to play the file and the button associated with it, like this (b:1 is left mouse button, and I chose deadbeef because mplayer and mpv take a second to start playing. Deadbeef takes no time as long as it's already running):

Code: Select all

"deadbeef /root/Desktop/BUTTON.WAV"
 b:1

Then I killed and restarted xbindkeys. It works instantaneously just like I needed, but there's a problem: the mouse click only triggers the sound but the rest of system doesn't "see" the click, e.g. I click on any button on the screen and nothing happens except for the sound being played. This looks like an easy to solve common side-effect, so next I'm gonna look for how to allow the click to "go-through" to the system instead of being taken only by xbindkeys, then I think I'll be set.

I still leave this note here in case anyone knows the trick


Re: How to make a sound every time you click something?

Posted: Sat Apr 13, 2024 10:07 pm
by Tippe

Perhaps one can create something using xev.
Perhaps combined with xdotool.

Code: Select all

ButtonPress event, serial 36, synthetic NO, window 0x4400001,
    root 0x2b6, subw 0x0, time 2289790, (82,68), root:(217,230),
    state 0x10, button 1, same_screen YES

ButtonRelease event, serial 36, synthetic NO, window 0x4400001,
    root 0x2b6, subw 0x0, time 2289910, (82,68), root:(217,230),
    state 0x110, button 1, same_screen YES

Code: Select all

 echo `xev > /root/xev-ouput`

Re: How to make a sound every time you click something?

Posted: Tue Apr 30, 2024 2:55 pm
by bob93

Alright, I asked around and a xbindkeys expert gave me this tip:

You have to prevent xbindkeys to grab the key before replaying the mouse click.

Something like this should do the trick :

"pkill xbindkeys; sleep 0.1; xdotool click 1; play sound.mp3 & xbindkeys"
b:1

This is a little bit inefficient but should work.

Now, this looks like a shorthand description of a command, but how would it actually look like in a terminal/text file?


Re: How to make a sound every time you click something?

Posted: Tue Apr 30, 2024 6:02 pm
by rockedge

Looking at xbindkeys it could look like this in script form

Code: Select all

#!/bin/sh

pkill xbindkeys
sleep 0.1
xdotool click 1
play sound.mp3 & 
xbindkeys
b:1

Re: How to make a sound every time you click something?

Posted: Wed May 01, 2024 12:21 am
by MochiMoppel
bob93 wrote: Tue Apr 30, 2024 2:55 pm

Alright, I asked around and a xbindkeys expert gave me this tip:

You have to prevent xbindkeys to grab the key before replaying the mouse click.
Something like this should do the trick :
"pkill xbindkeys; sleep 0.1; xdotool click 1; play sound.mp3 & xbindkeys"
b:1

This is a little bit inefficient but should work.

Now, this looks like a shorthand description of a command, but how would it actually look like in a terminal/text file?

It works. The red lines are the lines you have to use in your .xbindkeysrc file. The play sound.mp3 is just an example of the audio command and your expert used the play command instead of your deadbeef , which may be a bit less inefficient ;)

Running the commands from a bash script as @rockedge proposed didn't work for me.


Re: How to make a sound every time you click something?

Posted: Wed May 01, 2024 1:00 am
by rockedge

@MochiMoppel the bash script doesn't work for me either. Added to the .xbindkeysrc the command string works.


Re: How to make a sound every time you click something?

Posted: Fri May 10, 2024 5:42 pm
by bob93
MochiMoppel wrote: Wed May 01, 2024 12:21 am

"pkill xbindkeys; sleep 0.1; xdotool click 1; play sound.mp3 & xbindkeys"
b:1

It works. The red lines are the lines you have to use in your .xbindkeysrc file. The play sound.mp3 is just an example of the audio command and your expert used the play command instead of your deadbeef , which may be a bit less inefficient ;)

Using deadbeef I got the same result as before, .e.g sound plays on every click but the system doesn't "see" the clicks, .e.g clicking on buttons and menus does nothing apart from playing the sound. When you say "it works" do you mean the system sees the clicks in your case? What media player did you use with the command?

This is my line:

Code: Select all

"pkill xbindkeys; sleep 0.1; xdotool click 1; deadbeef /root/Desktop/BUTTON.WAV & xbindkeys"
b:1

Re: How to make a sound every time you click something?

Posted: Sat May 11, 2024 1:33 am
by MochiMoppel
bob93 wrote: Fri May 10, 2024 5:42 pm

When you say "it works" do you mean the system sees the clicks in your case?

Yes

What media player did you use with the command?

play (aka sox)


Re: How to make a sound every time you click something?

Posted: Sun May 12, 2024 1:28 am
by bob93

Alright, the problem was that I didn't have the package xdotool. I was under the impression that this package was part of the system already.

Installed the package and the command is working. I also tried lower sleep intervals to reduce the delay between clicking and hearing the sound as much as possible but the lowest that seems to work is 0.06 seconds, so the lines look like this now:

Code: Select all

"pkill xbindkeys; sleep 0.06; xdotool click 1; play /root/Desktop/BUTTON.WAV & xbindkeys"
b:1

Next step is to make to make drag-selection work but I'll deal with that another day

Thank you

EDIT: The line below is better to avoid inconsistent behavior that I have noticed. It still doesn't make drag selection work, though

Code: Select all

"pkill xbindkeys; sleep 0.01; xdotool click 1; play /root/Desktop/BUTTON.WAV & xbindkeys"
b:1 + Release

Also, to affect right click instead of left click, replace click 1 with click 3 and b:1 with b:3


Re: How to make a sound every time you click something?

Posted: Thu Jun 13, 2024 10:52 am
by superhik

You can't grab X button events from the root window and use it.
It's a security feature.
Usually you would detect mouse press and release with xinput.

Code: Select all

#!/bin/bash

MOUSE_ID=$(xinput --list | grep -i -m 1 'mouse' | grep -o 'id=[0-9]\+' | grep -o '[0-9]\+')

STATE1=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
while true; do
    sleep 0.2
    STATE2=$(xinput --query-state $MOUSE_ID | grep 'button\[' | sort)
    comm -13 <(echo "$STATE1") <(echo "$STATE2")
    STATE1=$STATE2
done

It does not print out very reliably since you have to use sleep to detect or it will be hard on cpu load.


Re: How to make a sound every time you click something?

Posted: Thu Jun 13, 2024 10:53 pm
by superhik

I made two apps, one runs as root and monitors /dev/input/mice the other as unprivileged user plays a wav file and the two communicate using a shared memory.
But it turns out it's quite easy. :D
https://askubuntu.com/questions/153316/ ... er-i-click
when you make that expect script use

Code: Select all

spawn xmacrorec2 -k 73

But you use your own key to stop xmacrorec2.