Klogg Posted November 6, 2003 Posted November 6, 2003 I don't know how to get the keypress event to determine if certain keys were pressed. I know about the KeyPreview property, and I know how to check if keys like the letter, number, or backspace keys were pressed, but I don't know how to do the delete key, arrow keys, and that kind of stuff. The ones that I need to use right now are the up and down arrow keys. Quote Take a look at my programs. Go to my web site.
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 You need to use the KeyCode event, and use the Keys enumeration to see which key is being pressed. Look at the MSDN. Quote
Klogg Posted November 6, 2003 Author Posted November 6, 2003 I found out in MSDN that the KeyPress event doesn't see keys that don't have an output on the screen, such as the arrow keys, the Delete key, and the function keys. I need to use KeyDown or KeyUp instead for those. However, I found that when I use KeyDown, it only executes the code for the arrow keys if one of the text boxes on my form has the focus. It works fine in the KeyUp event, but then the focus moves if the arrow key is held down long enough, and I don't want it to do that. Quote Take a look at my programs. Go to my web site.
Darc Posted November 7, 2003 Posted November 7, 2003 I've had some problems with KeyDown and KeyUp before... In some apps they just don't work, and in others they work fine. I haven't pinpointed the problem yet Quote
Voca Posted November 10, 2003 Posted November 10, 2003 This is some Code where i want to know if a function key is pressed and then do something. ... if e.KeyData = Keys.Down or e.KeyData = Keys.F8 then 'do something ... This Code can be found within the KeyUp-Event of my Form and is working. Voca Quote
Voca Posted November 10, 2003 Posted November 10, 2003 This is some Code where i want to know if a function key is pressed and then do something. ... if e.KeyData = Keys.Down or e.KeyData = Keys.F8 then 'do something ... This Code can be found within the KeyUp-Event of my Form and is working. Voca I forgot, the KeyPreview Property of my form is set to true. Voca Quote
Klogg Posted November 11, 2003 Author Posted November 11, 2003 Actually, that doesn't help me with the problem that I have now. I can't get the arrow keys to work in the KeyDown event when a button has the focus. It only works when a text box has the focus. I am using: If e.KeyCode = Keys.Up I also tried e.KeyData and e.KeyValue, but they do the same thing. What is the difference between them anyway? Quote Take a look at my programs. Go to my web site.
Klogg Posted November 11, 2003 Author Posted November 11, 2003 I found out from another post how to do it. Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean If m.WParam.ToInt32 = Keys.Up Then 'whatever the code is End If End Function Quote Take a look at my programs. Go to my web site.
Klogg Posted November 11, 2003 Author Posted November 11, 2003 Oops. When I use the code on my last reply, the keys in my KeyPress event don't work for some reason. Well, actually, the fist time it should execute (unless an arrow key has been pressed), it works, but then it doesn't anymore. Quote Take a look at my programs. Go to my web site.
deBUGger Posted November 13, 2003 Posted November 13, 2003 No, klogg... That happens to work! And thanks to you, now I can move my racket in my pong game! Yeyyyyyyy!!!!!!!!!!!!!!! :) Quote
sizer Posted November 13, 2003 Posted November 13, 2003 this is function that returns value ( bool ) try with this: Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean If m.WParam.ToInt32 = Keys.Up Then 'whatever the code is End If Return True End Function Quote Some people are wise and some are other-wise.
Klogg Posted November 13, 2003 Author Posted November 13, 2003 This doesn't seem to make any difference, even if I put Return False. Quote Take a look at my programs. Go to my web site.
CattleRustler Posted November 13, 2003 Posted November 13, 2003 Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs) If Asc(e.KeyChar) = CInt(System.Windows.Forms.Keys.Escape) Then Me.Close() ' Esc was pressed End If If Asc(e.KeyChar) = 112 Then 'p was pressed (toggles blnPause value) If blnPause = True Then blnPause = False Exit Sub Else blnPause = True End If End If End Sub 'OnKeyPress this works for me Quote mod2software Home of the VB.NET Class Builder Utility - Demo and Full versions available now!
Klogg Posted November 13, 2003 Author Posted November 13, 2003 That one works for the Escape key, but not the arrow keys. Quote Take a look at my programs. Go to my web site.
sizer Posted November 13, 2003 Posted November 13, 2003 (edited) try with this Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean ''Implement your code here '' If keyData = Keys.Escape Then ..... MessageBox.Show(keyData.ToString) Return True End Function Edited November 13, 2003 by sizer Quote Some people are wise and some are other-wise.
Klogg Posted November 14, 2003 Author Posted November 14, 2003 Getting closer... Now it works, but it blocks keyboard shortcuts to menu items. Also, I can't figure out the Keys.Whatever command for the "(," ")," and "^" keys. Quote Take a look at my programs. Go to my web site.
*Experts* DiverDan Posted November 15, 2003 *Experts* Posted November 15, 2003 The easiest way to use the arrow keys is through the keyup event. This way you can also utilize the keydown and keypress events. An example: Private Sub PIValue_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Then If tbarPI.value < 50 Then tbarPI.value += 1 End If ElseIf e.KeyCode = Keys.Left Or e.KeyCode = Keys.Down Then If tbarPI.value >= 1 Then tbarPI.value -= 1 End If End If End Sub Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Klogg Posted November 15, 2003 Author Posted November 15, 2003 I want to use the KeyDown event, but the arrow keys don't work in it unless one of the text boxes has the focus. Maybe I should just just leave this feature out of my program. It's getting to be more trouble than it's worth. Why does something so simple have to be so complicated to do anyway? Quote Take a look at my programs. Go to my web site.
vincentnl Posted December 11, 2003 Posted December 11, 2003 I too don;t think the correlation between windows controls and catching key_down events has been sufficiently explained. Anybody any information? Quote
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.