hog Posted June 7, 2003 Posted June 7, 2003 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: Quote My website
*Experts* jfackler Posted June 7, 2003 *Experts* Posted June 7, 2003 From what I can see, that code will respond if you hit the down key only. Jon Quote
*Experts* jfackler Posted June 7, 2003 *Experts* Posted June 7, 2003 Hog, Here's a link for some answers. http://www.syncfusion.com/FAQ/WinForms/FAQ_c46c.asp#q993q Also, do a search on the forum this topic has been discussed on multiple previous occasions. Is the Form.KeyPreview property set to true? Jon Quote
hog Posted June 7, 2003 Author Posted June 7, 2003 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?? Quote My website
*Experts* mutant Posted June 7, 2003 *Experts* Posted June 7, 2003 That works fine for me. What are you doing the the keydown event? Quote
hog Posted June 7, 2003 Author Posted June 7, 2003 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?? Quote My website
hog Posted June 7, 2003 Author Posted June 7, 2003 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? Quote My website
Administrators PlausiblyDamp Posted June 8, 2003 Administrators Posted June 8, 2003 If yoou want to trap a key event in a form, you need to set the form's keypreview property to true. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hog Posted June 8, 2003 Author Posted June 8, 2003 (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 June 8, 2003 by hog Quote My website
*Experts* mutant Posted June 8, 2003 *Experts* Posted June 8, 2003 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. Quote
*Experts* jfackler Posted June 8, 2003 *Experts* Posted June 8, 2003 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 Quote
*Experts* mutant Posted June 8, 2003 *Experts* Posted June 8, 2003 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. Quote
hog Posted June 8, 2003 Author Posted June 8, 2003 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????? Quote My website
ThePentiumGuy Posted June 8, 2003 Posted June 8, 2003 (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 June 9, 2003 by ThePentiumGuy Quote 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
hog Posted June 8, 2003 Author Posted June 8, 2003 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 Quote My website
hog Posted June 8, 2003 Author Posted June 8, 2003 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 :-)))) Quote My website
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.