Is there a way to wake mycroft with a press of the keyboard instead of mycroft listening for 'hey mycroft'?

Is there a way to make it so when i press a certain key, it wakes mycroft INSTEAD of mycroft listening for ‘hey mycroft’? if not, how would i make this?

Not just by configurering Mycroft. But you can make a skill that does that.
The trick will be to send to the messagebus that it has to start listning.

self.bus.emit(Message("mycroft.mic.listen"))

Look at the picroft-google-aiy-skill for an example. It listen for press on button connected to the GPIO and when that happen it emit the message to the messagebus

https://github.com/andlo/picroft-google-aiy-voicekit-skill

Se more about messagebus here

https://mycroft-ai.gitbook.io/docs/mycroft-technologies/mycroft-core/message-bus

1 Like

awesome tips thx andlo !

1 Like

This is an awesome idea. Thank you andlo, I didn’t know it was that simple to just send it via the message bus, that’s awesome!

I rewrote one of the cl tools (mycroft-say-to) and made mycroft-listen that just activates it. (I will send it up to see if I can’t merge it, or at least if it can be used somewhere else.).

On KDE you can then make a custom shortcut to run this new script. (from the “custom shortcut”)

I am struggling to figure out git issues and keyboard issues tonight, so I’ll just post the contents of script here so you can check it out.

bin/mycroft-listen

#!/usr/bin/env bash

# Copyright 2018 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

SOURCE="${BASH_SOURCE[0]}"
cd -P "$( dirname "$SOURCE" )"
DIR="$( pwd )"

# Enter the Mycroft venv
source "$DIR/../venv-activate.sh" -q

# Send a message to be spoken
output=$(python -m mycroft.messagebus.send "mycroft.mic.listen")
1 Like

Nice work, I’m very interested to see your progress on this topic. It would be very handy to be able to ‘double tap’ the command key and that wakes mycroft. I’m gonna look into how to achieve this. any ideas? @FruityWelsh

1 Like

If you find anything on a double tap shortcut please let me know. That’s not anything I’ve heard of before, but I could see the use for sure! (I guess I know of things that use it, like windows sticky keys for example). The KDE and Gnome reddits maybe a good place to ask though!

Pull request has been made.

1 Like

awesome thanks @FruityWelsh !

1 Like

I am new to Mycroft and have a very poor recognition rate of the wake word.
This script is perfect - thanks.

If anyone is as new to Bash as me:-
Put the script in /home/pi/graham/mycroft_listen.sh and run with
sudo /home/pi/graham/mycroft_listen.sh

This change made it run

#source “$DIR/…/venv-activate.sh” -q
source “/home/pi/mycroft-core/venv-activate.sh” -q

I intend to run the script by waving a hand close to an ultrasonic range module or use a clap detector.

2 Likes
from mycroft import MycroftSkill
from mycroft.messagebus.message import Message
from pynput import keyboard
#from pynput.keyboard import KeyCode
import time


class HotKey(MycroftSkill):
    def __init__(self):
        MycroftSkill.__init__(self)
        
    def initialize(self):
        self.handle_button()
        
    def handle_button(self):
        # Single key press example
        '''
        def on_press(key):
            if key == KeyCode.from_char('r'):
                self.speak_dialog('Listening')
                time.sleep(2)
                self.bus.emit(Message("mycroft.mic.listen"))
            
        listener = keyboard.Listener(
                on_press=on_press)
        '''
        # Using global hotkeys
        def on_activate():
            self.speak_dialog('Listening')
            time.sleep(2)
            self.bus.emit(Message("mycroft.mic.listen"))
            
        def for_canonical(f):
            return lambda k: f(listener.canonical(k))
        
        hotkey = keyboard.HotKey(
                keyboard.HotKey.parse('<ctrl>+<alt>+r'),
                on_activate)
        
        listener = keyboard.Listener(on_press = for_canonical(hotkey.press),
                                     on_release = for_canonical(hotkey.release))
        
        listener.start()

def create_skill():
    return HotKey()
1 Like

Hey Lara, welcome to the Forums and thanks for posting your code.

Looks like a great solution and I love seeing more examples of people using Skills to do interesting things!

@Lara I was having a little play with your code and realized the listener thread would get replicated if the Skill reloads. You might want to add a listener.stop() in the Skills self.shutdown() method. This gets called anytime the Skill is shutting down or reloading.

I’ve done some other tweaks for my own purposes here if you’re interested:

  • Stop the listener on shutdown or if the settings change and a new listener is being created
  • Can set the key combo via skill settings
  • creates a single or multi-key listener dependent on whether there is a ‘+’ character in the string
  • Removed the speak_dialog call and delay (just personal preference as I already have the listener beep on)

If you have this in a repo anywhere, I’d be very happy to fork off your repo so the attribution is clear.

Love your work :slight_smile:

meant for desktop users and anyone with multiple wake word support patches

LGPL free

1 Like

@gez-mycroft Good catch with the listener.stop() in the skills self.shutdown() method! I like your modifications. I don’t have a repo anywhere, but appreciate the credit.