Author Topic: Enable/Disable WSR example  (Read 3311 times)

0 Members and 2 Guests are viewing this topic.

GMib

  • Jr. Member
  • **
  • Posts: 26
  • Karma: 0
    • View Profile
Enable/Disable WSR example
« on: September 27, 2013, 01:09:01 PM »
How to enable or disable Windows Speech Recognition using SendMessage, I used to activate Dictation mode.

WSRcontrol.py
Code: [Select]
import ctypes

def sendWsrMessage(iMsg):
   SendMessage = ctypes.windll.user32.SendMessageW
   FindWindow = ctypes.windll.user32.FindWindowA
   handle = FindWindow(b"MS:SpeechTopLevel",None)
   if handle:
      SendMessage(handle, 273, iMsg, 0)
      return 1
   else:
      return 0

def wsrOn():
   return sendWsrMessage(101)
def wsrOff():
   return sendWsrMessage(102)

Example of use in xml

VC must be run as administrator
« Last Edit: September 28, 2013, 12:59:09 AM by GMib »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Enable/Disable WSR example
« Reply #1 on: September 27, 2013, 04:51:02 PM »
In order to call python with a parameter you need to define a function (or method, whatever it is called in python).  You do this in a .py file which you load using the PY.ExecFile action.  This only needs to be done once so you could put it in a command that is triggered by the VC.Loaded event.

Once that has been done you can call the function using PY.ExecString action.

so for example, your python file (which you can load at startup using PY.ExecFile) might look like this:
Code: [Select]
import ctypes

def sendWsrMessage(iMsg):
   SendMessage = ctypes.windll.user32.SendMessageW
   FindWindow = ctypes.windll.user32.FindWindowA
   handle = FindWindow(b"MS:SpeechTopLevel",None)
   if handle:
      SendMessage(handle, 273, iMsg, 0)
      result = 1
   else:
     result = 0

def wsrOn():
   sendWsrMessage(101)
def wsrOff():
   sendWsrMessage(102)

and then you could use these commands to turn WSR on and off.
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<commandGroup open="True" name="WRS Start Stop" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="188" name="windows speech recognition start" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>wsrOn()</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="199" name="windows speech recognition stop" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>wsrOff()</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
</commandGroup>

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Enable/Disable WSR example
« Reply #2 on: September 27, 2013, 04:55:03 PM »
It looks like having a Window.SendMessage action in VC would be handy... ;)

GMib

  • Jr. Member
  • **
  • Posts: 26
  • Karma: 0
    • View Profile
Re: Enable/Disable WSR example
« Reply #3 on: September 27, 2013, 05:19:18 PM »
I do not succeed to get "result" in function with ExecString. ( i put result in wsron and off command but i think it's work same)

Window.SendMessage action would be better ;)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Enable/Disable WSR example
« Reply #4 on: September 27, 2013, 05:52:13 PM »
I do not succeed to get "result" in function with ExecString.

Python File:
Code: [Select]
import ctypes

def sendWsrMessage(iMsg):
   SendMessage = ctypes.windll.user32.SendMessageW
   FindWindow = ctypes.windll.user32.FindWindowA
   handle = FindWindow(b"MS:SpeechTopLevel",None)
   if handle:
      SendMessage(handle, 273, iMsg, 0)
      return 1
   else:
      return 0

def wsrOn():
   return sendWsrMessage(101)
def wsrOff():
   return sendWsrMessage(102)

VC WSR On command:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="188" name="windows speech recognition start" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result =sendWsrMessage(102)</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <cmdString>{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

This will get you a result.   If you set a variable in a python method, it is a local variable only and not in the same "scope" as our "result" variable, which is at the "base level" (for lack of a better term - or I should say, I don't know the correct term).

By the way, this method of sending a message to WSR seems to work fine on one of my computers, but does nothing on another computer.  ???
« Last Edit: September 27, 2013, 05:55:19 PM by jitterjames »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Enable/Disable WSR example
« Reply #5 on: September 27, 2013, 06:13:33 PM »
By the way, this method of sending a message to WSR seems to work fine on one of my computers, but does nothing on another computer.  ???

Ah...  I figured this out.  I should have known.

UAC blocks sendmessage unless you run VC as Administrator.  Then it works.  :bonk

GMib

  • Jr. Member
  • **
  • Posts: 26
  • Karma: 0
    • View Profile
Re: Enable/Disable WSR example
« Reply #6 on: September 28, 2013, 01:02:51 AM »
thank you, I did not think to put the result in the ExecString ;)

i've edited first post.