Problem using the spacebar to kick off an event
Hi.
I need my program to respond to a spacebar press to start a speech synthesiser. The problem i am getting is that pressing the spacebar kicks off the speech but also "clicks" a button if it has focus at the time (i.e. same as clicking on it with the mouse or pressing enter when it has focus).
Does anybody know of a way to stop a button from responding to a spacebar press?
At the minute i am using the "s" key to get around the problem but as the program is intened for use by low vision users it would be good to change this to the spacebar, as it is the biggest key on the keyboard and also easy to locate.
Thanks very much for your help.
Andy.
Re: Problem using the spacebar to kick off an event
Andy:
IF you don't use the Keypress event in any of your controls ...
You can write a single Keypress event handler for ALL the controls on a Form. You can base it on the Keypress event for any control, and then just add each control to the "Handles" reference at the end of the call. If you have a LOT of controls, this will look very messy because of the lenght of the line. However, it will allow you to check for a space character and intercept it (don't pass it on) and start your speech box. If it is not a space character, just let it pass through. I don't remember exactly how to "eat" an even, but I'll look it up.
Here is an example of an event handler that handles all click events for 9 different buttons.
Code:
Private Sub Button_Type_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, _
Button6.Click, Button7.Click, Button8.Click, Button9.Click
Ahh ... "e.Cancel = True" ... I think that's how to kill the event. Just put it in before you exit the event handler:
If space then
do something
e.Cancel = True
End If
End Sub
Re: Problem using the spacebar to kick off an event
why not just set the forms keypreview property to true? then the forms key events will accept the keypress before the button does....
Re: Problem using the spacebar to kick off an event
Thank goodness SOMEBODY around here knows what they are doing!
That just goes to show you ... don't rely on a Newbie Hacker (me) for answers! Thanks Kleinma ... I learned something new!
Re: Problem using the spacebar to kick off an event
Quote:
Originally Posted by Webtest
Thank goodness SOMEBODY around here knows what they are doing!
That just goes to show you ... don't rely on a Newbie Hacker (me) for answers! Thanks Kleinma ... I learned something new!
no worries, you answer was not WRONG... just the more complicated method... I see code all the time that someone wrote in 10 lines what I wrote in 30. Helps you to learn multiple ways to accomplish things..
Re: Problem using the spacebar to kick off an event
Hey guys.
Thanks for the advice. I already have the form's keypreview property set to "true" but i am still getting the problem. It seems like the form handles the spacebar press first and then it carries on and gets handled by the controls as well.
I'm just going to try the code now to "consume" the spacebar press and see if that works.
Thanks both for your help.
Andy.
Re: Problem using the spacebar to kick off an event
OK sorted it!
I placed an e.Handled = True after my code that kicks off the speech synthesiser. This seems to stop the event from carrying on to the buttons on the form.
Thanks both for your help..got there in the end!
Cheers,
Andy.