High Level:
I want to use my custom Cortana command "Notepad" in TEXT mode. For instance, by pressing WIN+S and typing "appname Notepad Example sentence!".
(This will open Notepad and input "Example sentence!".)
The Notepad command already works in VOICE mode: when I press WIN+C and say "appname Notepad Example sentence!", my notepad script is run with the payload "Example sentence!".
The Problem:
When I press WIN+S and input "appname Notepad Example sentence!", the text property of SpeechRecognitionResult is "Notepad ..." (as opposed to voice where it is "Notepad Example sentence!", as expected).
Code:
VCD.xml excerpt
<Command Name="notepad">
<Example> Notepad Example Sentence! </Example>
<ListenFor> Notepad {wildcardArgs} </ListenFor>
<Feedback> Notepadding {wildcardArgs} </Feedback>
<Navigate/>
</Command>
<PhraseTopic Label="wildcardArgs" Scenario="Dictation">
<!--<Subject>Wildcard</Subject>-->
</PhraseTopic>
CommandHandler.cs
public static CortanaCommand ProcessCommand(SpeechRecognitionResult speechRecognitionResult, CommandDiagnostics diagnostics)
{
// Get the name of the voice command and the raw text
string voiceCommandName = speechRecognitionResult.RulePath[0];
string text = speechRecognitionResult.Text;
string mode = speechRecognitionResult.SemanticInterpretation.Properties[interpretationKey].FirstOrDefault();
// When mode is voice, text is "Notepad Example sentence!"
// When mode is text, text is "Notepad ..."
// How can one retrieve "Example sentence!" from "..." !?
// Is there some property other than speechRecognitionResult.Text that holds the raw text typed?
string argument = null;
CortanaCommand processedCommand = null;
switch (voiceCommandName)
{
// ...
case CortanaCommand.Notepad:
const string notepad = "Notepad";
argument = CortanaCommand.StripOffCommandName(notepad, text);
processedCommand = new NotepadCortanaCommand(argument, diagnostics);
break;
default:
Debug.WriteLine("Command Name Not Found: " + voiceCommandName);
break;
}
return processedCommand;
}
Question Restated
How can the above code be changed to extract command arguments (i.e. everything other than the app name and command name) in text mode?
case CortanaCommand.Notepad:
argument = speechRecognitionResult.SemanticInterpretation.Properties["wildcardArgs"].FirstOrDefault();
// the magic line ^
processedCommand = new NotepadCortanaCommand(argument, diagnostics);
break;
来源:https://stackoverflow.com/questions/37106171/how-to-extract-the-argument-from-a-cortana-command-with-a-phrase-topic-activate