richietheg
Posts: 4
Joined: Fri Jan 11, 2013 8:32 pm

'Binding' keys in script (was 'Detecting mouse clicks')

Fri May 03, 2013 8:55 am

Hi,

Slightly new to the RPi and not-so-new to Linux. I am developing a system whereby a Pi can be used to switch between two online radio stations. The only output is the audio, no visual, and am using MPD/MPC and a shell script to change the stations (unfortunately, one of them has a dynamic stream URL so a playlist has to be loaded for it each time).

To make it as easy as possible, I want to develop a simple two button switcher which will allow the users to switch between the two stations. I have in the past hacked a mouse to work on a footpedal switch (I use it to cycle through a PowerPoint (or equiv.) presentation while playing guitar), and am currently thinking that this would be the easiest way to go about it.

The two questions that come to mind is that although I have found tutorials online that allow this thing using the GPIO interface, there is mentions of resistors and stuff like that which, to my mind, overcomplicates things - is it not possible to just attach a button to the GPIO? (I realise that should be in the hardware section here, but given that the answer I'm expecting is 'no', there didn't see much point!)

The second question, and possibly the more relevant one, is is there any way that through the terminal (and not in any GUI) I can detect mouse clicks, and assign a script to either one? I did come across 'xbindkeys' while looking around last night, but that seems to only work within a GUI environment and not in just the plain ole terminal.

This is probably a more general Linux question than one specific to the RPi, but I currently am testing this on the basic Raspbian OS (though will probably, once I get it working, sort out a very minimal one with only what I need on it).

Is what I want possible, or am I barking up the wrong tree?
Last edited by richietheg on Mon May 06, 2013 11:02 am, edited 1 time in total.

ghans
Posts: 7878
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Detecting mouse clicks

Fri May 03, 2013 9:45 am

Perhaps you can use a USB Number Block (for Laptops).

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

richardp
Posts: 117
Joined: Thu Jan 12, 2012 11:46 am

Re: Detecting mouse clicks

Fri May 03, 2013 10:21 am

take a look at the /dev/input/events files.. depending on what you are doing there will be triggers on what keys/mouse/etc operations are occurring.

Richard
RaspberryPi's galore
Solid run CuBox
ODroid U2

richietheg
Posts: 4
Joined: Fri Jan 11, 2013 8:32 pm

Re: Detecting mouse clicks

Sun May 05, 2013 3:01 pm

Ooh, thanks for that - I hadn't thought about using a number pad for this - works much better as instead of having only two stations to switch between I can have nine, and have commands for altering the volume or switching the audio off.

So after a bit of searching around, I have now used the bind command to set the keys to their tasks - currently 1, 2, and 3 load specific radio stations, 0 stops MPC, and the + and - buttons alter the volume. It's also set to start playing automatically when the Pi is booted (it did that anyway, I didn't have any problems with that bit - as I said before, I have some slightly higher-than-basic knowledge of Linux terminal).

The problem I now have is that I can't seem to get the bind command to run automatically. At the moment, I've got a shell script that initialises the 'radio', so it sets the default volume of MPC and starts the default station. But it doesn't seem to want to run the bind command. I just get

Code: Select all

init.sh: 1: init.sh: bind: not found
when it tries to run (obviously as part of the bootup sequence, this doesn't appear, but it does when I try to run it, either locally or remotely - I know running bind doesn't work on the local machine when you run it remotely, but am just testing!)

A search of the Internet has come up with a blank, partially - I think - because there's another bind command that does something with DNS servers. So does anybody know how I can get this to run on boot? The idea is that this can just get switched on and then it will run, with no screen attached or anything and with just the USB number pad attached. So any ideas?

Thanks :)

ghans
Posts: 7878
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Detecting mouse clicks

Mon May 06, 2013 6:28 am

I believe you will have to put the full path into the
startup script.

Code: Select all

whereis bind
should show it.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

richietheg
Posts: 4
Joined: Fri Jan 11, 2013 8:32 pm

Re: Detecting mouse clicks

Mon May 06, 2013 10:11 am

Nope :/

The command gave me /usr/share/man/man2/bind.2.gz - putting in /usr/share/man/man2/bind.2.gz/bind gave me the Not Found error; putting in just /usr/share/man/man2/bind.2.gz gave me permission denied and running it as sudo came up with command not found.

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: Detecting mouse clicks

Mon May 06, 2013 10:34 am

There is no "bind" command.

edit: You are trying to use readline to run mpc on certain keystrokes. That can only work in an interactive bash shell. The way to make it persistent is to add the commands to ~/.bashrc. But you have to be logged in, and sitting at the shell prompt with no foreground process running at the time the keys are pressed. So it is not really very useful.

richietheg
Posts: 4
Joined: Fri Jan 11, 2013 8:32 pm

Re: 'Binding' keys in script (was 'Detecting mouse clicks')

Mon May 06, 2013 7:12 pm

As running 'bind' does something when I run it, what is it then if it's not a command? I'm genuinely curious to know btw, that's not me being harsh!

I have set the system up thus far to sit at the shell prompt, and as the only thing this will be doing is playing out the audio and changing stations when buttons on the keypad are pressed, there won't be any other processes running in the foreground (at least, that's the hope!).

If using 'bind' (whether it's a command or not!) is not the best way to do it, how would you recommend doing it instead? Again, I'm genuinely interested in learning this - when I Google searched it the other day, the only thing that seemed to work, particularly without having a GUI desktop environment (what's the need for that if there's going to be no screen attached‽) was using 'bind', hence why that was what I used to a fair amount of success (apart from remapping the keys at startup!).

Thanks :)

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: 'Binding' keys in script (was 'Detecting mouse clicks')

Mon May 06, 2013 8:36 pm

"bind" is a bash-specific shell built-in. Technically, as far as bash is concerned, that is a type of command. But it is not available in other shells, standard shell scripts, or even in non-interactive bash sessions. It has no path and you cannot call it from other languages like C or Python.

("help" in bash shows the built-ins. Some of these are both built-ins and external commands. But most are built-in because they have to change the behaviour or status of the shell itself, which could not be done in an external command. "cd" is the classic example. If it were an external command it would change its own working directory and then exit, leaving the shell's working directory unaffected. Most of the built-ins are not bash-specific but also work in standard shell scripts.)

If you are happy to leave a bash prompt in the foreground, then putting the bind commands in .bashrc should be okay.

I cannot find any console key-binding daemon in the Raspbian repositories, presumably because on desktops people usually use xbindkeys or the equivalent facility of their X window manager.

actkbd appears to do it, but it needs to be compiled from source and looks a little complex to configure: http://users.softlab.ece.ntua.gr/~thkal ... ts/actkbd/

It should also be possible to read evdev input events in a Python script running in the background. But I have never tried that either. http://www.raspberrypi.org/phpBB3/viewt ... p?p=339731

Return to “Raspbian”