Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying the following:

 



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:

My website
Posted

Thanks...

 

Yes, I'm 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??

My website
Posted

mutant, I'm 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??

My website
Posted
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?
My website
Posted (edited)

 

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:

Edited by hog
My website
  • *Experts*
Posted
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.
  • *Experts*
Posted

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, they're 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

  • *Experts*
Posted
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.
Posted

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 I'm creating. They match perfectly, yet on my form if the button is present the key down is not fired?????

My website
Posted (edited)

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

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

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:

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

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 that's 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?

Edited by ThePentiumGuy

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

Posted

Thanks...I'll give it a go.

 

to get the code box use the tags

 

square bracket vb square bracket

 

then

 

squre barcket / vb square bracket

My website
Posted

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 :-))))

My website

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...