Home All Groups Group Topic Archive Search About

Form_Keydown being overridden

Author
11 Jul 2006 5:06 PM
Jeffy210
I'm having a weird problem with the Keydown event.

- I have a form with the KeyPreview property set to true
- I have a Form_KeyDown sub that does a bit of code for me
- I have a few control buttons on the form

Now comes the weird part. When I first launch the form, it intercepts the
key presses  no problem (i'm capturing the arrow keys). However, when I click
on a button, the form no longer fires off the keydown event, pressing the
left arrow, for example, will start cycling through the other buttons.

Is there a way to return the focus or control to the form so it can capture
the arrow's being pressed and run the form_keydown code?

Author
12 Jul 2006 3:59 AM
Shane Story
There is a property on the Form that must be set.. I think it is KeyPreview
(I don't have VS up and running now).  That allows the form to see it before
a control.

What happens is the control is handling it and thus eating the event before
your form ever sees it.

HTH,

Shane
Show quoteHide quote
"Jeffy210" <Jeffy***@discussions.microsoft.com> wrote in message
news:49DE92B0-6EC6-44B4-AACF-CBB74E943D62@microsoft.com...
> I'm having a weird problem with the Keydown event.
>
> - I have a form with the KeyPreview property set to true
> - I have a Form_KeyDown sub that does a bit of code for me
> - I have a few control buttons on the form
>
> Now comes the weird part. When I first launch the form, it intercepts the
> key presses  no problem (i'm capturing the arrow keys). However, when I
> click
> on a button, the form no longer fires off the keydown event, pressing the
> left arrow, for example, will start cycling through the other buttons.
>
> Is there a way to return the focus or control to the form so it can
> capture
> the arrow's being pressed and run the form_keydown code?
Author
12 Jul 2006 7:06 AM
R. MacDonald
Hello, Jeffy210,

When the focus is on a Button, the arrow keys are being "pre-processed"
as navigation keys so they never reach the form's KeyDown event.

Two possibilities come to mind.

1. You could replace the Button control with your own button, inherited
from the standard button, but include an override like:

     Protected Overrides Function _
         IsInputKey(ByVal keyData As Keys) As Boolean
         Select Case keyData
             Case Keys.Down, Keys.Left, Keys.Right, Keys.Up
                 Return True
             Case Else
                 Return False
         End Select
     End Function

2. Maybe you could "catch" the arrow keys before they are pre-processed
by overriding WndProc in your form.  I have never tried this, though.
But maybe others more learned may be able to suggest how to do this, or
offer other ways around the problem.

Cheers,
Randy


Shane Story wrote:
Show quoteHide quote
> There is a property on the Form that must be set.. I think it is KeyPreview
> (I don't have VS up and running now).  That allows the form to see it before
> a control.
>
> What happens is the control is handling it and thus eating the event before
> your form ever sees it.
>
> HTH,
>
> Shane
> "Jeffy210" <Jeffy***@discussions.microsoft.com> wrote in message
> news:49DE92B0-6EC6-44B4-AACF-CBB74E943D62@microsoft.com...
>
>>I'm having a weird problem with the Keydown event.
>>
>>- I have a form with the KeyPreview property set to true
>>- I have a Form_KeyDown sub that does a bit of code for me
>>- I have a few control buttons on the form
>>
>>Now comes the weird part. When I first launch the form, it intercepts the
>>key presses  no problem (i'm capturing the arrow keys). However, when I
>>click
>>on a button, the form no longer fires off the keydown event, pressing the
>>left arrow, for example, will start cycling through the other buttons.
>>
>>Is there a way to return the focus or control to the form so it can
>>capture
>>the arrow's being pressed and run the form_keydown code?
>
>
>
Author
12 Jul 2006 2:16 PM
Jeffy210
Okay, I understand what you mean, and I even found the "UserControl" item,
but for the life of me I can't figure out how to make it inherit the
properties of a standart button. Addionally that means I'm having trouble
figuring out where to put that code.

But all things being said, it looks like that will be the exact solution for
what I'm looking for. Thanks!

Show quoteHide quote
"R. MacDonald" wrote:

> Hello, Jeffy210,
>
> When the focus is on a Button, the arrow keys are being "pre-processed"
> as navigation keys so they never reach the form's KeyDown event.
>
> Two possibilities come to mind.
>
> 1. You could replace the Button control with your own button, inherited
> from the standard button, but include an override like:
>
>      Protected Overrides Function _
>          IsInputKey(ByVal keyData As Keys) As Boolean
>          Select Case keyData
>              Case Keys.Down, Keys.Left, Keys.Right, Keys.Up
>                  Return True
>              Case Else
>                  Return False
>          End Select
>      End Function
>
> 2. Maybe you could "catch" the arrow keys before they are pre-processed
> by overriding WndProc in your form.  I have never tried this, though.
> But maybe others more learned may be able to suggest how to do this, or
> offer other ways around the problem.
>
> Cheers,
> Randy
>
>
> Shane Story wrote:
> > There is a property on the Form that must be set.. I think it is KeyPreview
> > (I don't have VS up and running now).  That allows the form to see it before
> > a control.
> >
> > What happens is the control is handling it and thus eating the event before
> > your form ever sees it.
> >
> > HTH,
> >
> > Shane
> > "Jeffy210" <Jeffy***@discussions.microsoft.com> wrote in message
> > news:49DE92B0-6EC6-44B4-AACF-CBB74E943D62@microsoft.com...
> >
> >>I'm having a weird problem with the Keydown event.
> >>
> >>- I have a form with the KeyPreview property set to true
> >>- I have a Form_KeyDown sub that does a bit of code for me
> >>- I have a few control buttons on the form
> >>
> >>Now comes the weird part. When I first launch the form, it intercepts the
> >>key presses  no problem (i'm capturing the arrow keys). However, when I
> >>click
> >>on a button, the form no longer fires off the keydown event, pressing the
> >>left arrow, for example, will start cycling through the other buttons.
> >>
> >>Is there a way to return the focus or control to the form so it can
> >>capture
> >>the arrow's being pressed and run the form_keydown code?
> >
> >
> >
>
Author
12 Jul 2006 2:30 PM
Jeffy210
Okay, actually bothered to sit down and read on User Controls and I got it
working. That rocks! Thank you very much. I can see some good uses for this
in the future.

Thank you very much!
Jeffy210

Show quoteHide quote
"R. MacDonald" wrote:

> Hello, Jeffy210,
>
> When the focus is on a Button, the arrow keys are being "pre-processed"
> as navigation keys so they never reach the form's KeyDown event.
>
> Two possibilities come to mind.
>
> 1. You could replace the Button control with your own button, inherited
> from the standard button, but include an override like:
>
>      Protected Overrides Function _
>          IsInputKey(ByVal keyData As Keys) As Boolean
>          Select Case keyData
>              Case Keys.Down, Keys.Left, Keys.Right, Keys.Up
>                  Return True
>              Case Else
>                  Return False
>          End Select
>      End Function
>
> 2. Maybe you could "catch" the arrow keys before they are pre-processed
> by overriding WndProc in your form.  I have never tried this, though.
> But maybe others more learned may be able to suggest how to do this, or
> offer other ways around the problem.
>
> Cheers,
> Randy
>
>
> Shane Story wrote:
> > There is a property on the Form that must be set.. I think it is KeyPreview
> > (I don't have VS up and running now).  That allows the form to see it before
> > a control.
> >
> > What happens is the control is handling it and thus eating the event before
> > your form ever sees it.
> >
> > HTH,
> >
> > Shane
> > "Jeffy210" <Jeffy***@discussions.microsoft.com> wrote in message
> > news:49DE92B0-6EC6-44B4-AACF-CBB74E943D62@microsoft.com...
> >
> >>I'm having a weird problem with the Keydown event.
> >>
> >>- I have a form with the KeyPreview property set to true
> >>- I have a Form_KeyDown sub that does a bit of code for me
> >>- I have a few control buttons on the form
> >>
> >>Now comes the weird part. When I first launch the form, it intercepts the
> >>key presses  no problem (i'm capturing the arrow keys). However, when I
> >>click
> >>on a button, the form no longer fires off the keydown event, pressing the
> >>left arrow, for example, will start cycling through the other buttons.
> >>
> >>Is there a way to return the focus or control to the form so it can
> >>capture
> >>the arrow's being pressed and run the form_keydown code?
> >
> >
> >
>
Author
12 Jul 2006 5:05 AM
Sugan
This thread may be a useful one for you

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=438551&SiteID=1

-Sugan.

Jeffy210 wrote:
Show quoteHide quote
> I'm having a weird problem with the Keydown event.
>
> - I have a form with the KeyPreview property set to true
> - I have a Form_KeyDown sub that does a bit of code for me
> - I have a few control buttons on the form
>
> Now comes the weird part. When I first launch the form, it intercepts the
> key presses  no problem (i'm capturing the arrow keys). However, when I click
> on a button, the form no longer fires off the keydown event, pressing the
> left arrow, for example, will start cycling through the other buttons.
>
> Is there a way to return the focus or control to the form so it can capture
> the arrow's being pressed and run the form_keydown code?