Can get Form KeyDown to work

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
Im trying the following:

Code:
Private Sub frmGame_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        If e.KeyCode =  Keys.Down Then

            yadda yadda 

        End If

End Sub

However no matter what I do, this does not get called. I put a break point in the code, run the program and tap away at the keys and nothing.

Am I missing something:confused:
 
Thanks...

Yes, Im trying to catch the down key

Does not make any difference if key preview is set

Already did a search before posting but could not find any??
 
mutant, Im working through the .NET DirectX book stuff and cannot get this to work. I have checked my other VB books which all have similar examples in them?

I have a single form with the code above placed in its code section, but cannot get it to work. The event just does not fire??
 
OK, so I am now comparing my form to the sample and can see no difference. However I can trap the key down event until I place a button on the form, then it no longer works?
 
Does not make any difference if key preview is set

already tried this and it does not work once I place a button on the form

OK...to confirm what I am doing..

I create a new form and set the KeyPreview to True.

I place the code listed above in the Forms code section and it works.

I place a picturebox on the form and the KeyDown Event still works.

I place a Button on the form and the KeyDown Event does not work anymore??:confused:
 
Last edited by a moderator:
From my observations it looks like the button keeps getting focus to itself, to see what im saying put the button on the form and set Enabled to false, now you will be able to catch form KeyDown.
 
In VS, a search for KeyDown event gave me this:

Visual Basic Reference

KeyDown, KeyUp Events (ActiveX Controls)

Occur when the user presses (KeyDown) or releases (KeyUp) a key while an object has the focus. (To interpret ANSI characters, use the KeyPress event.)

For both events, the object with the focus receives all keystrokes. A form can have the focus only if it has no visible and enabled controls. Although the KeyDown and KeyUp events can apply to most keys, theyre most often used for:

Extended character keys such as function keys.
Navigation keys.
Combinations of keys with standard keyboard modifiers.
Distinguishing between the numeric keypad and regular number keys.
Use KeyDown and KeyUp event procedures if you need to respond to both the pressing and releasing of a key.


Try the KeyPress event.

Jon
 
As you are saying that you try examples from DX book I assume your making game, which will make me assume you want to use directional buttons :) KeyPress does not trap keys like directional keys.
 
Mutant, you are correct I am writing a game.

Starnge thing is the example from the CD works with the forms KeyPreview set to true and a single button on the from?

I have opened two sessions of VS and compared the form that works from the book with the form Im creating. They match perfectly, yet on my form if the button is present the key down is not fired?????
 
i had the same problem!!

ok
listen

you have to make sure any objects that are on the form are not default

for instance,

create a new project with a BLANK form and use this code
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        If e.KeyCode =  Keys.Down Then

            MessageBox.Show("down key pressed")

        End If

End Sub
[/VB]
it will work

now draw a command button onto the form..
you will notice that it doesnt work. Because now, the command button is the default button on the form. the Form doesnt have focus, the command button does

to fix that, write the code for the object that has focus, in this case, the command button:

change your code to this:
[VB]
Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown

        If e.KeyCode =  Keys.Down Then

            MessageBox.Show("down key pressed")

        End If

End Sub
[/VB]
and it should work

so
on ur game,  see which object has focus. If a textbox has focus, you can type in it, if a command button has focus you will see 
a shadow on the button. one thing thats safe is a label, it cannot have focus, 

i hope this works :-\  not too sure but try it out

btw, when you guys copy and paste code, how do you get it in a little grey box in the post?
 
Last edited by a moderator:
Thanks...Ill give it a go.

to get the code box use the tags

square bracket vb square bracket

then

squre barcket / vb square bracket
 
OK.....sorted it :-)

jfackler you were right. The demo game disables the button on the form once it has been clicked and the game starts...

Thanks chaps we can call this problem closed :-))))
 
Back
Top