Question Using KeyDown event

Lance Turbo

New member
Joined
Apr 19, 2013
Messages
4
Programming Experience
Beginner
Hi,

First up, this is my first post and so a big "Gday" to everyone.

My question is,

I'm using the key down event to capture arrow presses to move shapes around a form. However I have push buttons on the form as well. The arrow keys simply move focus from push button to push button before reaching the picture boxes when I can capture them to move my shape. Is there anyway I can stop the arrow keys moving focus to the push buttons?

Thanks in Advance
 
Hi,

I would normally always defer to jcmilhinney's expertise, including his blog examples, but in this case I would suggest that his blog does not accommodate the issue you are having.

I am still a relative novice to .NET and if I were to answer your question directly I would have typically suggested that you set the Forms KeyPreview propetry to True and then test for the Up or Down Arrow Key in the KeyDown event of the Form and then code what you need to do from there.

However, after testing this before I posted earlier today, I found that as long as a Button control has the Focus then the PreviewKeyDown, KeyDown and KeyPress evants are all IGNORED?

After a quick search on the NET I found that the solution was to override the ProcessCmdKey function. Here is a quick example based on what you are wanting to do:-

VB.NET:
Protected Overrides Function ProcessCmdKey(ByRef Message As Message, ByVal keyData As Keys) As Boolean
  If keyData = Keys.Down OrElse keyData = Keys.Up Then
    Select Case keyData
      Case Keys.Down
        PictureBox1.Top += 10
      Case Keys.Up
        PictureBox1.Top -= 10
    End Select
    Return True
  Else
    Return False
  End If
End Function

Knowing that now, it would be great if one of the experts on this forum could jump in here and let you and I know if I have missed something obvious or if there is a reason why the key stoke events described above are NOT fired when a button control has the focus when the Forms KeyPreview property is set to True.

Hope that helps.

Cheers,

Ian
 
However, after testing this before I posted earlier today, I found that as long as a Button control has the Focus then the PreviewKeyDown, KeyDown and KeyPress evants are all IGNORED?
That is something that I wasn't aware of.
 
However, after testing this before I posted earlier today, I found that as long as a Button control has the Focus then the PreviewKeyDown, KeyDown and KeyPress evants are all IGNORED?
When a button control has focus you would have to handle the events for that button, unless you intervene at an earlier stage in preprocessing as I explained.
 
Hi JohnH,

Many thanks for the additional notes and Yes, ProcessDialogKey worked just fine after reading those notes on Keyboarding events etc.

Hope that gets you where you need to be Lance Turbo.

Cheers,

Ian
 
Hi Fellas, thanks a million for your help.

I can get both ProcessCmdKey and ProcessDialogKey to work.

While not an issue for me, I noticed that when using the ProcessDialogKey, if the form loads and focus is on a text box, the ProcessDialogKey method won't capture my arrow key movements.

Cheers,
Ben
 
Lance Turbo said:
While not an issue for me, I noticed that when using the ProcessDialogKey, if the form loads and focus is on a text box, the ProcessDialogKey method won't capture my arrow key movements.
TextBox control considers arrow keys as input keys (IsInputKey returns True), so ProcessDialogKey is not called in that case.
 
Back
Top