Search code examples
pythonpython-dragonfly

How to switch context in Dragonfly


I have tried the Python module for handling speech recognition Dragonfly and successfully run notepad example with Windows speech recognition. Now I would like to try something more general, but I cannot find how contexts are switched, i.e. grammars loaded. There are always lines like:

grammar = Grammar("Eclipse", context=DynamicContext(winContext, nixContext))
grammar.add_rule(rules)
grammar.load()

But the context is always tied to an executable or window title. How do I switch between grammars at will, like a word command or at least mouse click, key press?


Solution

  • Create a rule which calls a function which does this:

    grammar.disable()
    other_grammar.enable()
    

    Have a look at grammar_base.py for other relevant functions.