Controlling The Space Bar, But With My Brain

Seher Khan
8 min readJan 24, 2024

Through my research in Brain Computer Interfaces it became time to do something tangible; controlling the space key with my mind. I would first like to express immense gratitude to Jaswant Dineshkumar, Yuejia Wang and Prajwal Prashanth for their extensive help throughout this process. Lastly, This whole project would not have been possible without a step by step tutorial made by Carol Rong. Be sure to check out her article!

Now let’s get into the juicy stuff.

Woah, I made a mind control machine my bedroom? Well it’s not as hard as you think. Let’s take a deep dive on how I did this.

First off, you need a key tool to control your space key; a Muse 1 headband .

Muse 1 Headband

The Muse headband was created to measure and track brain activity, specifically to help users enhance their meditation and mindfulness practices by providing real-time feedback on their mental states.

But we are going to take the brain reading feature of the Muse and use it control the space key, or any key of your choice.

There are a few applications you need to download to do this, disclaimer my article is specific to Mac, if you have a different device I’d suggest taking a look at Carol Rong’s step by step which goes through both Mac and Windows.

  1. Microsoft Visual Studios: Most of your code will be done through this terminal
  2. Petal Metrics: The medium you will use to stream the Muse through
  3. Terminal: A coding application that is apart of Mac, you likely won’t need to download it

There are also a few other resources you’ll need throughout this process

  1. Python — version 3.12.1
  2. Alexandre Barachant’s Github repository: This allows us to view Muse data

Now let’s go through the steps:

Step 1

To begin, make sure you have the most up-to-date version of Python installed. Occasionally, even after downloading the latest version, you might encounter an issue where it appears to be an older version. In such cases, simply try downloading it again.

Python download page

Step 2

Open up the terminal application on your Mac, you can do this by either pressing ⌘ + space or simply going to launchpad and searching up terminal.

What terminal looks like

In terminal, you want to verify that python has been installed. To do so, type in the code below and press the enter key.

Python --version 

On my Mac, the first option unfortunately lead to installation of an older version. If you are on the same device, I’d suggest using the option below.

Python3 --version 

Step 3

From here, we want to check if `pip` is installed. In simple terms, `pip` is a tool in Python that helps you easily install, uninstall, and manage external packages, which are sets of code and resources created by other developers. It simplifies the process of adding or removing functionality to Python projects.

To check installation, type in the following code into terminal

pip3 --version 

We have now finished installing Python and it’s dependancies, time to get your Muse out and get ready for the fun stuff

Step 4

At this point, you want to open the Petal Metrics application, this tool will help you connect your Muse to your Mac through Bluetooth.

When you open Petal Metrics you should be able to see a few options when streaming your Muse

Petal Metrics when opening

Be sure that the setting is “LSL” as shown above. LSL stands for lab streaming layer which is the best setting when wanting real time data.

At this point you are ready to stream your Muse, click the stream button if it has connected it should say streaming Muse ####. While going through this process, I noticed that Petal would show “connecting…” for extended periods of time but never connect. To solve this, just close Petal Metrics, open it again, and give it another try. Keep in mind that nothing will be shown at this step, there are still a few more things you need to do to be able to see your Muse data.

Step 5

The next step is to download a program that will help you actually view the Muse data. Alexandre Barachant has fortunately created a Github file that allows us to to do this called Muselsl.

To use this, click the link attached to Muselsl or view the link in the tools section in the beginning of the article.

The Github Repository

From here, you want to click the code button and press the copy option so we are able to use it in our code editor, Visual Studios.

Step 6

Now, it’s time to open up Visual Studios and paste the repository into a new terminal.

A photo from Carol Rong’s amazing article

When you press “clone repository” there should be search bar that pops up on the top, paste the github repository you copied earlier and paste it into the search bar.

Step 7

Once you paste your repository, Visual Studios should now look a little something like this.

For now, ignore all the functions at the left side, you’ll be using them more as we progress. You now want to open up a new terminal as shown.

Once you open up the terminal, it should look like this.

Step 8

With the terminal window open, you can utilize pip to install muselsl. Just a quick reminder, pip is the command you used initially to install various Python dependencies.

Copy and paste the provided code into the terminal, then hit the “Enter” key.

pip3 install muselsl

Step 9

At this step it’s time to connect and view your muse data. Make sure Petal Metrics is up and your streaming your Muse. If your having issues with this refer back to step 4.

Into the same terminal paste the following code and hit the “enter” key.

muselsl view

Once the code fully loads you should be able view your muse data in the form of the different brain waves! (Alpha, Beta, Theta, Delta, and Gamma)

From here, there are a few more steps in order to make your Visual studios recognize blinks however, I won’t be covering those steps in this article. Luckily, Carol Rong’s article shows you exactly how to do this.

Next, let’s go over how to connect your Muse device to your Visual Studio Code and use your blinks to control keyboard functions.

Step 1

For MacOS there are a few dependencies we need to download in order to connect to our keyboard.

Into Visual studios type in the following code and hit enter for installation.

pip install pyobjc-core pyobjc

These packages are part of the PyObjC framework, which is a Python module that allows you to interact with and use macOS Cocoa APIs and libraries from Python.

Step 2

To enable keyboard control for Visual Studio and our code, you first need to configure your laptop’s system settings to allow keyboard-based accessibility control.

To do this:

System Preferences > Accessibility tab > Enable Full keyboard Access

Make sure “Full keyboard access” is enabled

Step 3

Next, you must grant Visual Studio permission to use the keyboard so that your code can control it.

To do this:

System settings > Privacy and security > Accessibility > Enable Visual Studios to allow computer control

“Visual Studio Code 2” should be enabled

Now that keyboard and Visual Studios have access to your keyboard and computer, it’s time to head back into Visual Studios application.

Step 4

At this step, you’ll need to modify the code for Muse and Visual Studio so that when you blink, it triggers an action, like pressing the “up” key or “space” in your application.

To do this we need to paste a code under the “smooth_band_powers” code shown below

smooth_band_powers = np.mean(band_buffer, axis=0)
if band_powers[Band.Delta] >= 1.3:
blink = time()
if blink - last_blink >= 0.365:
print("Blink")
pyautogui.press("up")
last_blink = blink

Paste the code above right under the “smooth_band_powers” code.

Ignoring the commented code, your screen should also look like this

In a nutshell, what this does is decipher whether band_powers[Band.Delta] is greater than or equal to 1.3.

  1. When this condition is met, the code records the current time as a “blink” event.
  2. It then checks if enough time (0.365 seconds or more) has passed since the last recorded blink event.
  3. If the time difference is sufficient, it takes action by printing “Blink” to the console to indicate a blink has occurred.
  4. Additionally, it simulates pressing the “up” key on the keyboard using the pyautogui library. This keypress action serves as a response to the detected blink. You can also change the key that’s being controlled, for example to control the space key replace the “up” in brackets to “space.”
  5. Finally, it updates the timestamp for the last recorded blink, so it can be used as a reference for future blink detections.

In essence, this code monitors brainwave activity and responds to blinks by registering them in time and triggering actions like printing “Blink” and simulating a keypress.

Step 5

The last step until you are able to control a key on the keyboard is to paste the code “last_blink = time” into our code.

Paste the code in this area

Paste the following code into the same place shown in the image above

last_blink = time()

What this does is sets the last_blink variable to the current time when the code runs. It's like noting the time on a stopwatch before starting a task so that you can later measure how much time has passed while doing that task.

Step 6

Woohoo! You have set up everything on your computer for key control. It’s now time to connect your Muse through Petal Metrics and run your code on Visual Studios. To run your code, simply press the play icon on the top right corner of your Visual Studios.

Once the code is running there are so many things you can play around with! For example, I played the classic no internet game by blinking. To do the same, paste this link into your browser chrome://dino/ and give it a try!

My highest score using blinks to play!

Conclusion

There you have it! DIY mind control!

Thank you so much for checking out my article. If you’ve had a chance to try this tutorial out, I’d love to hear about your experience!

Feel free to reach out to me via:

Linkedin: www.linkedin.com/in/seherkhan1

Email: sehernaaz8@gmail.com

--

--