How to make a sound every time you click something?

Moderators: 666philb, Forum moderators

Post Reply
bob93
Posts: 53
Joined: Fri Nov 25, 2022 8:29 am
Location: Fossapup64 9.5
Has thanked: 21 times
Been thanked: 1 time

How to make a sound every time you click something?

Post 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

Fossapup64 9.5

User avatar
mikewalsh
Moderator
Posts: 6163
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 795 times
Been thanked: 1983 times

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

Post 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. ;)

bob93
Posts: 53
Joined: Fri Nov 25, 2022 8:29 am
Location: Fossapup64 9.5
Has thanked: 21 times
Been thanked: 1 time

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

Post 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

Fossapup64 9.5

bob93
Posts: 53
Joined: Fri Nov 25, 2022 8:29 am
Location: Fossapup64 9.5
Has thanked: 21 times
Been thanked: 1 time

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

Post 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

Fossapup64 9.5

Tippe
Posts: 137
Joined: Wed Feb 15, 2023 11:55 pm
Has thanked: 8 times
Been thanked: 21 times

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

Post 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`

Bionic Puppy 64bit.
Won't use another one.

bob93
Posts: 53
Joined: Fri Nov 25, 2022 8:29 am
Location: Fossapup64 9.5
Has thanked: 21 times
Been thanked: 1 time

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

Post 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?

Fossapup64 9.5

User avatar
rockedge
Site Admin
Posts: 6551
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2757 times
Been thanked: 2628 times
Contact:

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

Post 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
User avatar
MochiMoppel
Posts: 1239
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

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

Post 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.

User avatar
rockedge
Site Admin
Posts: 6551
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2757 times
Been thanked: 2628 times
Contact:

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

Post by rockedge »

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

bob93
Posts: 53
Joined: Fri Nov 25, 2022 8:29 am
Location: Fossapup64 9.5
Has thanked: 21 times
Been thanked: 1 time

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

Post 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

Fossapup64 9.5

User avatar
MochiMoppel
Posts: 1239
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

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

Post 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)

bob93
Posts: 53
Joined: Fri Nov 25, 2022 8:29 am
Location: Fossapup64 9.5
Has thanked: 21 times
Been thanked: 1 time

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

Post 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

Fossapup64 9.5

superhik
Posts: 42
Joined: Mon Jun 19, 2023 7:56 pm
Has thanked: 6 times
Been thanked: 19 times

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

Post 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.

Stealing from the poor to give to the rich!
bslit - Block Splitter Custom Calendar Widget + Diary

superhik
Posts: 42
Joined: Mon Jun 19, 2023 7:56 pm
Has thanked: 6 times
Been thanked: 19 times

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

Post 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.

Stealing from the poor to give to the rich!
bslit - Block Splitter Custom Calendar Widget + Diary

Post Reply

Return to “Fossapup64”