Speech Recognition RecognizeAsync on keydown - c#

Im trying to have my program listen to me only when i press the shift key so called Push to talk and i tried to use the following code but when the key is pressed for longer then 1-2 seconds i get the following error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Speech.dll
Additional information: Cannot perform this operation while the recognizer is doing recognition.
Here is part of the code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
label1.BackColor = Color.Green;
label1.Text = "Speak";
RecEngine.RecognizeAsync(RecognizeMode.Multiple);
e.SuppressKeyPress = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
label1.BackColor = Color.Yellow;
label1.Text = "Ready";
RecEngine.RecognizeAsyncStop();
e.SuppressKeyPress = true;
}
}

The Form1_KeyDown function is called repetitively while the button is pressed.
You need to check whether the button has already been pressed
private bool recognitionRunning;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey && !recognitionRunning)
{
label1.BackColor = Color.Green;
label1.Text = "Speak";
RecEngine.RecognizeAsync(RecognizeMode.Multiple);
e.SuppressKeyPress = true;
recognitionRunning = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
label1.BackColor = Color.Yellow;
label1.Text = "Ready";
RecEngine.RecognizeAsyncStop();
e.SuppressKeyPress = true;
recognitionRunning = false;
}
}

Related

Perform the action when the key is pressed

I am working on my project and i need to add an action that is only performed while left CTRL is pressed
This is my code:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.Control)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
Now when I run this code and press CTRL all is OK, but when I release CTRL
nothing happened.
I was try this:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
This works fine but I need do this with CTRL
The KeyUp event will fire when you release Ctrl, but the e.Control boolean is not set to True because Ctrl is no longer being held down.
In short: Don't detect the release of Ctrl by inspecting e.Control, inspect the e.KeyCode instead; it will be Keys.ControlKey
if(e.KeyCode == Keys.ControlKey)
{
...
}
On your second code, replace Keys.A with Keys.ControlKey like this:
private void Izrada_kartice_KeyDown(object
sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender,
KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}

Sounds play with delay

here is my code:
MediaPlayer player = new System.Windows.Media.MediaPlayer();
bool playing = false;
bool _bKeyIsDown = false;
protected override void OnKeyDown(KeyEventArgs e)
{
if (_bKeyIsDown) return;
_bKeyIsDown = true;
// play sound;
base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
_bKeyIsDown = false;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
if (label5.Text == "Waiting 01.wav")
{
MessageBox.Show("No beat loaded");
return;
}
pictureBox6.Image = Form1.Properties.Resources.white_square_button;
try
{
playing = true;
player.Open(new Uri(label37.Text));
player.Volume = (double)trackBar4.Value / 100;
player.Play();
}
catch (FileNotFoundException)
{
MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
}
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
pictureBox6.Image = Form1.Properties.Resources.black_square_button;
player1.Stop();
player1.Close();
playing = false;
}
This what makes is to play a sound while key is down, but the problem is that when you release that key and press another one the sound delays and if you press 2 keys at the same time it only play the first one you pressed.
If you remove if (_bKeyIsDown) return; it does the trick but the sound won't play at its full length.
Is there a way to fix this problem? Thanks!
If its just play and stop 1 sound just do KeyDown event and use bool to toggle:
bool playing = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
if (!playing)
{
player.Open(new Uri(yourURI));
player.Play();
playing = true;
}
else
{
player.Stop();
playing = false;
}
}
}
EDIT:
then may i suggest you create a dictionary with all the sounds you need like this:
Dictionary<Keys, Uri> sounds = new Dictionary<Keys, Uri>()
{
{Keys.A,new Uri(#"C:\..\..\youraudiofile.mp3")},
{Keys.B,new Uri(#"C:\..\yourotheraudiofile.wma")}
};
and create a list to keep references to the mediaplayers:
List<MediaPlayer> myplayers = new List<MediaPlayer>();
then only in KeyDown event(remove the onKeyDown,and keyup also):
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
foreach (MediaPlayer m in myplayers)
{
m.Stop();
}
return;
}
if (sounds.ContainsKey(e.KeyCode))
{
MediaPlayer mp = new MediaPlayer();
mp.Open(sounds[e.KeyCode]);
mp.Play();
}
}

Textbox on which Keys Delete and backspace Works

I need to have a text box on which events of Delete and Backspace works.Is it possible to have such a text box in C#,or restrict the behavior of text box in such a way. Other keys do not work.
Use TextBox.KeyPress event:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
// your stuff
}
e.Handled = true;
}
For winforms you can do it like this:
protected void myTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = !IsValidCharacter(e.KeyChar);
}
private bool IsValidCharacter(Keys c)
{
bool isValid = false;
if (c == Keys.Space)
{
isValid = true;
}
return isValid;
}
If you want delete key works ..
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Keys k = e.KeyCode
If Not (k = Keys.Back Or k = Keys.Delete)
{
e.Handled = True
}
}

Does the KeyDown KeySuppress cancel KeyUp event?

Let's say I've got this:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Non-numeric key pressed => prevent this from being input into the Textbox
e.SuppressKeyPress = true;
}
}
and this:
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
try
{
UpdateState(double.Parse(((TextBox)sender).Text));
}
catch (Exception ex)
{
((TextBox)sender).Text = ioElement.StateVal.ToString("0.00");
}
}
}
I know this code doesn't make a lot of sense, it's just test.
The question is: Will the e.SuppressKeyPress = true in KeyDown event have affect on KeyUp event, so the Enter key will not be accepted?
No, e.SuppressKeyPress = true will just ignore the Enter key (it won't go to the next line and Text property of the textbox won't be changed) and e.Keycode will be visible in the KeyUp. Therefore suppressing the key in the KeyDown doesn't affect the KeyUp event and your code should work. The UpdateState will be called when you hit Enter button in the TextBox. You can try this code to check it:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
e.SuppressKeyPress = true;
}
}
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show("Up");
}
}

User can press and hold only one keyboard button at a time

I have a standard WinForms-application. I want to implement such functionality:
user can press and hold only one keyboard button at a time. If he tried to press a button, while another button pressed, then it gets no result.
PS: this behavior spreads only to a form that I want, not to all forms of my application.
C#, 2.0 - 3.5, VS 2008
I got something similar than Khadaji
private Keys CurrentKey = Keys.None;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (CurrentKey == Keys.None)
{
CurrentKey = e.KeyData;
// TODO: put your key trigger here
}
else
{
e.SuppressKeyPress = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == CurrentKey)
{
// TODO: put you key end trigger here
CurrentKey = Keys.None;
}
else
{
e.SuppressKeyPress = true;
}
}
I banged this out pretty quickly, so you might have to tinker with it to make it work, but it should get you started.
Set your form's KeyPreview to true. Put the in a KeyDown event and KeyUp Event.
Keys MyKey;
bool KeyIsDown = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
e.Handled = true;
}
else
{
MyKey = e.KeyData;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
if (e.KeyData == MyKey)
{
KeyIsDown = false;
}
}
}

Categories

Resources