# Event Handler in Powershell

**URL:** <https://forums.powershell.org/t/event-handler-in-powershell/45>\
**Category:** PowerShell Help\
**Created:** [May 12, 2013, 1:48am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45 "2013-05-12T01:48:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin-kramer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/martin-kramer/32/2638_2.png) [@martin-kramer](https://forums.powershell.org/u/martin-kramer)\
**Post date:** [May 12, 2013, 1:48am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/1 "2013-05-12T01:48:46Z")

</div>

Hi,  
I am trying to do some voice recognition via Powershell. - Just to play a bit. I have already found some information about this with help of google. But the most source code is in C#.  
My Problem is the following. In C# the event handler is attached like the following:

1  
2

`speechRecognitionEngine.SpeechRecognized += `  
`Â Â ``new` `EventHandler(engine_SpeechRecognized);`

How can I do this in Powershell? I’ve tried to do the following:

1  
2

`$handler` `= ``[System.EventHandler[System.Speech.Recognition.SpeechDetectedEventArgs]]``(engine_SpeechRecognized)`  
`$speechRecognitionEngine``.SpeechRecognized += ``$handler`

But this doesn’t work. It tells me “Property ‘SpeechRecognized’ cannot be found on this object; Make sure it exists and is settable.”

For additional Information:

- $SpeechRecognitionEngine is a object of type System.Speech.Recognition.SpeechRecognitionEngine
- engine\_SpeechRecognized is a function doing something ( in easiest way to write-host “SpeechRecognzed” ).

I know about Add-Type command and that I can use C# Code within powershell with it. But that is not what I want.

Thank you for your help  
mk.maddin

EDIT: Sorry didn’t know about the new forum. This is why I posted on both.

---

<div class="post-metadata">

**Author:** ![toni-martinez](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@toni-martinez](https://forums.powershell.org/u/toni-martinez)\
**Post date:** [May 12, 2013, 3:27am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/2 "2013-05-12T03:27:28Z")

</div>

Have you tried **Register-ObjectEvent**?

Something like:

$speechRecognition = New-Object System.Speech.Recognition.SpeechRecognitionEngine

Register-ObjectEvent -InputObject $speechRecognition -EventName SpeechRecognized -Action { Write-Host “SpeechRecognized” }

---

<div class="post-metadata">

**Author:** ![martin-kramer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/martin-kramer/32/2638_2.png) [@martin-kramer](https://forums.powershell.org/u/martin-kramer)\
**Post date:** [May 12, 2013, 3:34am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/3 "2013-05-12T03:34:06Z")

</div>

Perfect.  
This was exactly what I was looking for 🙂  
I didn’t know the cmdlet Register-ObjectEvent.

But how can I now decide what was recognized? In c# there is something like “object sender”.  
I already tried with $sender or $this … but they are empty ☹

Thank you again.

---

<div class="post-metadata">

**Author:** ![toni-martinez](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@toni-martinez](https://forums.powershell.org/u/toni-martinez)\
**Post date:** [May 12, 2013, 4:24am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/4 "2013-05-12T04:24:31Z")

</div>

I think in the -Action scriptblock you should have access to $sender.

Take a look on these links about event handling:

[http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/16/use-asynchronous-event-handling-in-powershell.aspx](http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/16/use-asynchronous-event-handling-in-powershell.aspx)

[http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/17/manage-event-subscriptions-with-powershell.aspx](http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/17/manage-event-subscriptions-with-powershell.aspx)

&nbsp;

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 12, 2013, 4:29am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/5 "2013-05-12T04:29:50Z")

</div>

Hi Martin,

To access an event’s arguments, use the $EventArgs.SourceArgs built-in variable. $Sender contains the object that generated the event, not it’s arguments. For more info on the built-in event variables, check out `Get-HelpÂ about_Automatic_Variables`.

As an example, say I wanted to access theÂ SignalTime argument passed to the Elapsed event in a Timer object. I would so the following:

```
$Timer = New-Object Timers.Timer
$Timer.Interval = 1000

$ElapsedAction = { Write-Host ($Event.SourceArgs.SignalTime) }

Register-ObjectEvent -InputObject $Timer `
                     -EventName Elapsed `
                     -SourceIdentifier TimerAction `
                     -Action $ElapsedAction

$Timer.Enabled = $True
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![martin-kramer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/martin-kramer/32/2638_2.png) [@martin-kramer](https://forums.powershell.org/u/martin-kramer)\
**Post date:** [May 12, 2013, 4:52am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/6 "2013-05-12T04:52:31Z")

</div>

```
[Reflection.Assembly]::LoadWithPartialName("System.Speech") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("System.Globalization") | Out-Null

#Show aviable languages
#[System.Speech.Recognition.SpeechRecognitionEngine]::InstalledRecognizers()

$Aktion ={
         Write-Host "Erkannt" 
         Write-host "---------"
         Write-Host "Sender: " $Sender
         Write-Host "Event: " $Event
         Write-host "EventArg:" $EventArg
         Write-host "---------"
         }

$CultureInfo = New-Object System.Globalization.CultureInfo("en-US")
$SpeechRecognitionEngine = New-Object System.Speech.Recognition.SpeechRecognitionEngine($CultureInfo)

$Texts = New-Object System.Speech.Recognition.Choices
$Texts.Add("Test")
$Texts.Add("Bla")
$Texts.Add("Hello")

$GrammarBuilder = New-Object System.Speech.Recognition.GrammarBuilder($Texts)
$WordsList = New-Object System.Speech.Recognition.Grammar($GrammarBuilder)
$SpeechRecognitionEngine.SetInputToDefaultAudioDevice()
$SpeechRecognitionEngine.LoadGrammarAsync($WordsList)

# attache an event handler
Unregister-Event "SpeechModuleCommandRecognized" -ErrorAction SilentlyContinue
#-->Olny for known commands in $WordList
#Register-ObjectEvent $WordsList SpeechRecognized -SourceIdentifier "SpeechModuleCommandRecognized" -Action { engine_SpeechRecognized $this } | Out-Null

#-->for ALL commands 
Register-ObjectEvent $SpeechRecognitionEngine SpeechRecognized -SourceIdentifier "SpeechModuleCommandRecognized" -Action {. $Aktion} | Out-Null

# start recognition
$RecognizeMode = New-Object System.Speech.Recognition.RecognizeMode
$RecognizeMode.value__ = 1 # 0=Signle; 1=Multiple

$SpeechRecognitionEngine.EmulateRecognize("bla") | Out-Null
#$SpeechRecognitionEngine.RecognizeAsync($RecognizeMode)
```

&nbsp;

&nbsp;

Thank you for your support.  
All these variables are empty at mine.

Here is the whole source code:

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 12, 2013, 5:03am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/7 "2013-05-12T05:03:09Z")

</div>

I’m guessing that you’re seeing the quotes in Write-Host print but not the objects themselves, right? If that’s what you’re running into, then you’re dealing with one of the quirks of PowerShell eventing.Â If you insist on calling Write-Host on an object, you have to call its ToString() method. To validate that $Event is actually being returned, save it to a global variable ($Global:Foo = $Event) and then access it.

---

<div class="post-metadata">

**Author:** ![martin-kramer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/martin-kramer/32/2638_2.png) [@martin-kramer](https://forums.powershell.org/u/martin-kramer)\
**Post date:** [May 12, 2013, 5:28am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/8 "2013-05-12T05:28:50Z")

</div>

```
Write-Host "Erkannt" 
         Write-host "---------"
         Write-Host "Sender: " 
         $global:a = $Sender
         Write-Host "Event: " 
         $global:b = $Event
         Write-host "EventArg:" 
         $global:c = $EventArg
         Write-host "---------"
```

But the variables $global:a , b and c are all empty ☹

I did the following:

&nbsp;

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 12, 2013, 6:10am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/9 "2013-05-12T06:10:18Z")

</div>

I modified your example slightly to show a working example:

```
[Reflection.Assembly]::LoadWithPartialName("System.Speech") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("System.Globalization") | Out-Null

$Aktion ={ Write-Host "Did you say $($Event.SourceArgs.Result.Text)?" }

$CultureInfo = New-Object System.Globalization.CultureInfo("en-US")
$SpeechRecognitionEngine = New-Object System.Speech.Recognition.SpeechRecognitionEngine($CultureInfo)

$Texts = New-Object System.Speech.Recognition.Choices
$Texts.Add("Test")
$Texts.Add("Bla")
$Texts.Add("Hello")

$GrammarBuilder = New-Object System.Speech.Recognition.GrammarBuilder($Texts)
$WordsList = New-Object System.Speech.Recognition.Grammar($GrammarBuilder)
$SpeechRecognitionEngine.SetInputToDefaultAudioDevice()
$SpeechRecognitionEngine.LoadGrammarAsync($WordsList)

Unregister-Event "SpeechModuleCommandRecognized" -ErrorAction SilentlyContinue

Register-ObjectEvent $SpeechRecognitionEngine SpeechRecognized -SourceIdentifier "SpeechModuleCommandRecognized" -Action $Aktion | Out-Null

# start recognition
$RecognizeMode = New-Object System.Speech.Recognition.RecognizeMode
$RecognizeMode.value__ = 1 # 0=Signle; 1=Multiple

$SpeechRecognitionEngine.EmulateRecognize('Hello') | Out-Null
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![martin-kramer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/martin-kramer/32/2638_2.png) [@martin-kramer](https://forums.powershell.org/u/martin-kramer)\
**Post date:** [May 12, 2013, 6:59am UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/10 "2013-05-12T06:59:28Z")

</div>

&nbsp;

&nbsp;

Okay… I found my problem.  
I usedÂ -Action {. $Aktion} instead of -Action $Aktion in line:

```
Register-ObjectEvent$SpeechRecognitionEngineSpeechRecognized-SourceIdentifier"SpeechModuleCommandRecognized"-Action$Aktion|Out-Null
```

I don’t understand what is the difference but it looks like there is one.

But not everything is recognized now…just the predefined Texts.  
Is there no way to get what the computer understood? - The idea behind all this was to can say anything and get it as text… - similar to dictation.

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![martin-kramer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/martin-kramer/32/2638_2.png) [@martin-kramer](https://forums.powershell.org/u/martin-kramer)\
**Post date:** [May 13, 2013, 5:41pm UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/11 "2013-05-13T17:41:32Z")

</div>

How can I mark this discussion as “solved”?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 13, 2013, 7:54pm UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/12 "2013-05-13T19:54:58Z")

</div>

I don’t believe it’s possible. 😕

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:57pm UTC](https://forums.powershell.org/t/event-handler-in-powershell/45/13 "2024-05-16T20:57:54Z")

</div>

Closed on May 16, 2024, 8:57pm UTC


