Due to injury I use dictation on MacOS:
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?
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.
Then, use the keyboard shortcut “Windows Key + H” and Dictate will begin listening for your voice.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With