# Use a media keyboard with your pHat Beat, can it be done?

**URL:** <https://forums.pimoroni.com/t/use-a-media-keyboard-with-your-phat-beat-can-it-be-done/5176>\
**Category:** Support\
**Created:** [July 1, 2017, 12:53pm UTC](https://forums.pimoroni.com/t/use-a-media-keyboard-with-your-phat-beat-can-it-be-done/5176 "2017-07-01T12:53:12Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [July 4, 2017, 4:13pm UTC](https://forums.pimoroni.com/t/use-a-media-keyboard-with-your-phat-beat-can-it-be-done/5176/9 "2017-07-04T16:13:38Z")

</div>

The script you’re looking for is probably this: [https://github.com/pimoroni/phat-beat/blob/master/projects/vlc-radio/phatbeatd/usr/bin/phatbeatd](https://github.com/pimoroni/phat-beat/blob/master/projects/vlc-radio/phatbeatd/usr/bin/phatbeatd)

It’s best left as-is, though, since it’s a fairly obtuse script not meant to be an editable example.

The relevant parts to understand are the socket interface for controlling VLC, which you can build into your script. Importing the `phatbeat` library wont work, since that’s just for reading buttons and doesn’t provide control over any of the software on your Pi.

```auto
import socket
import re

VLC_HOST = "127.0.0.1"
VLC_PORT = 9294

class VLC():

    def __init__ (self, host="127.0.0.1", port=9294):
        self.host = host
        self.port = port
        self.current_stream = None
        self.current_state = None
        self.connecting = False
        self.socket = None

    def send(self, command):
        if self.connecting:
            return False

        print("Sending command: {}".format(command))
        command_string = command + "\n"

        if sys.version_info[0] >= 3:
            command_string = command_string.encode("utf-8")

        try:
            self.socket.send(command_string)

        except socket.error:
            print("Failed to send command to VLC") 
            if self.connect():
                self.send(command)

    def recv(self, length):
        value = self.socket.recv(8192)

        if sys.version_info[0] >= 3:
            value = value.decode("utf-8")

        return value

    def communicate(self, command, response_length=8192):
        self.send(command)
        return self.recv(response_length)

    def get_current_stream(self):
        self.send("status")
        status = self.recv(8192)

        result = re.search("input:\ (.*)\ ", status)
        state = re.search("state\ (.*)\ ", status)

        if state is not None:
            self.current_state = state.group(1)

        if result is not None:
            self.current_stream = result.group(1)
        else:
            self.current_stream = None

        return self.current_stream

    def connect(self):
        if self.connecting:
            return

        self.connecting = True

        if self.socket is not None:
            self.socket.close()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        for attempt in range(10):
            try:
                print("Attempting to connect to VLC on {host}:{port}".format(host=self.host, port=self.port))
                self.socket.connect((self.host, self.port))
                print("Connection successful!")
                self.connecting = False
                return True

            except socket.error:
                time.sleep(1)

        print("Connection failed!")
        self.connecting = False
        return False

vlc = VLC(host=VLC_HOST, port=VLC_PORT)

if vlc.connect():
    # Your setup code here

```

And once all of the above has set up a connection to VLC, you can use the following commands to control it:

```auto
vlc.communicate("voldown") # Turn the volume down one step
vlc.communicate("volup") # Turn the volume up one step
vlc.communicate("next") # Next track
vlc.communicate("prev") # Prev track
vlc.communicate("pause") # Pause/Play

```

Note: To paste indented code, surround it with three backticks:

````
```
Like so!
```
````

---

_[View the full topic](https://forums.pimoroni.com/t/use-a-media-keyboard-with-your-phat-beat-can-it-be-done/5176)._
