Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically toggle dictation on MacOS

Due to injury I use dictation on MacOS:

enter image description here

As can be seen from the screenshot, I can toggle it using a keyboard shortcut.

I wish to toggle it from code (preferably ObjC).

I can manually inject the events:

// Assumes CTRL OPT CMD Space toggles dictation
void toggle_dictation()
{
    // NOTE: To return created event in tap-callback:
    //      cgEvent = [my_nsEvent CGEvent];
    //      CFRetain(cgEvent);

    //unsigned short keyCode_SPACE = 49;

    NSEvent* down_event = [NSEvent keyEventWithType: NSEventTypeKeyDown
                                           location: NSZeroPoint
                                      modifierFlags: NSEventModifierFlagControl | NSEventModifierFlagOption | NSEventModifierFlagCommand
                                          timestamp: 0.0
                                       windowNumber: 0
                                            context: nil
                                         characters: @" "
                        charactersIgnoringModifiers: @" "
                                          isARepeat: false
                                            keyCode: 0 /* keyCode_SPACE */ ];

    NSEvent* up_event = [NSEvent keyEventWithType: NSEventTypeKeyUp
                                         location: NSZeroPoint
                                    modifierFlags: 0
                                        timestamp: 0.0
                                     windowNumber: 0
                                          context: nil
                                       characters: @" "
                      charactersIgnoringModifiers: @" "
                                        isARepeat: false
                                          keyCode: 0 /* keyCode_SPACE */ ];

    CGEventPost(kCGHIDEventTap, [down_event CGEvent]);
    CGEventPost(kCGHIDEventTap, [up_event CGEvent]);
}

... but this is clumsy as it depends on my chosen shortcut.

Is there any way to do it with an API call?

like image 683
P i Avatar asked Jul 05 '19 03:07

P i


People also ask

What is the shortcut to change the dictation language on a Mac?

Click the Language pop-up menu, choose Customize, then deselect the language you don't want to use. Click the Shortcut pop-up menu, then choose a shortcut to start Dictation. To create your own shortcut, click the Shortcut pop-up menu, choose Customize, then press the keys you want to use.

What is the shortcut for dictate?

Then, use the keyboard shortcut “Windows Key + H” and Dictate will begin listening for your voice.

How do I dictate voice to text on a Mac?

To use voice dictation in an application on your Mac, first select a text field in an application. Next, press the Fn (Function) key twice or click the Edit menu and select Start Dictation. Speak to your Mac and the words you speak will start appearing in the text field.

Does Nuance Dragon work with Mac?

Dragon Dictate for Mac 3 goes beyond simple speech-to-text, and gives you control in more applications so that you can simply speak to do more than ever before. Use Dragon Dictate in Mac OS X Lion or Mountain Lion with virtually any Mac application.


1 Answers

Yes, there is:

NSSpeechRecognizer *recognizer = [[NSSpeechRecognizer alloc] init];
// start
[recognizer startListening];
// stop
[recognizer stopListening];

The full API is here:

https://developer.apple.com/documentation/appkit/nsspeechrecognizer?language=objc

like image 147
jvarela Avatar answered Oct 01 '22 04:10

jvarela