|
-
Jun 13th, 2011, 04:41 AM
#1
Thread Starter
Fanatic Member
difference between key press and key down event in vb.net
hi, what is the difference between key press event and key down event in vb.net
-
Jun 13th, 2011, 04:58 AM
#2
Re: difference between key press and key down event in vb.net
Follow the Blog link in my signature and check out my post all about Keyboard Events. It will answer your questions and more.
-
Jun 13th, 2011, 05:46 AM
#3
Thread Starter
Fanatic Member
Re: difference between key press and key down event in vb.net
sir, jmcilhinney, it is sad to say that i was reading your signature before posting this question. i read it thoroughly, but i didn't understand in your blog. I was not reading your blog for this question, but including this one i have few more things which i was trying to find there. I didnt understand on PreviewKeyDown and things related to this. It might be my mistake that i didn't understand your blog. Now if you please answer me here so that would be much better.
-
Jun 13th, 2011, 09:11 AM
#4
Re: difference between key press and key down event in vb.net
This is from that blog post:
The purpose of the KeyDown event is to trap specific keys, or key combinations, as they are depressed.
Unlike the other events mentioned here, which are about actual keys on the keyboard, KeyPress is about the text that those keys produce.
What exactly is it that you do not understand about that?
-
Jun 14th, 2011, 01:03 AM
#5
Thread Starter
Fanatic Member
Re: difference between key press and key down event in vb.net
 Originally Posted by jmcilhinney
This is from that blog post:What exactly is it that you do not understand about that?
what you have quoted here sir, honestly i am not picking them up. its still not clear at all to me, that what is actually key down event, and diff b/w key down and key press.
things i did not understand in your blog are
When the user presses a key on the keyboard, active WinForms controls raise multiple events. In the order they are raised, those events are PreviewKeyDown, KeyDown, KeyPress and KeyUp. Following is an explanation of when and how to use each one.
The first event raised by a control is the PreviewKeyDown event. As with all events raised by the Framework, the data for the event is provided by the e parameter; in this case, a PreviewKeyDownEventsArgs object. The purpose of this event is basically the IsInputKey property of that object.
The PreviewKeyDown event was added to the .NET Framework in version 2.0. Prior to that, the only way to specify whether a key is a regular input key or not was to derive your own custom control and override the IsInputKey method. With the addition of the PreviewKeyDown event, we can now handle it for a standard control and specify whether or not the key that was depressed is a regular input key. This way we can more easily provide one-off custom behaviour. For more information on treating a key as a regular input key, consult the MSDN documentation for the Control.IsInputKey method.
-
Jun 14th, 2011, 01:17 AM
#6
Re: difference between key press and key down event in vb.net
 Originally Posted by ADQUSIT
For more information on treating a key as a regular input key, consult the MSDN documentation for the Control.IsInputKey method.[/B]
Did you? Also, did you read the example further down the page that relates to PreviewKeyDown? Regardless of any of that, you didn't even ask about PreviewKeyDown so it's not even relevant.
As For KeyDown and KeyPress, which is what you asked about, I don't know how it can be any clearer than KeyDown relates to keyboard keys and KeyPress relates to text in a control. You understand that there's not a 1:1 correspondence between keys on the keyboard and text in a control, right? For instance, Shift+A is two keys but only one character, so two KeyDown events but only one KeyPress event. You could easily have worked that out for yourself by handling those events and seeing when each was raised and what data was received in each event handler.
-
Jun 14th, 2011, 01:39 AM
#7
Thread Starter
Fanatic Member
Re: difference between key press and key down event in vb.net
I know Sir John, that in post # 5 the bold text was not relevant to my question, but as i told you earlier that i was checking your blog before posting this question, i was studying your blog for general study, for my general information about events. but when i started that so i didnt get the meaning of even first line that what is this for, and the following paragraphs too. so thats why i asked for that too, to explain. So if you please explain the bold text or at least the first 2 paragraphs so it will surely extend my knowledge.
Sir as you said that keydown relates to keyboard and keypress relates to text in a control. Sir let me clear myself. Does it mean that lets suppose i opened a form, containing buttons and textboxes etc right. now i just press Ctrl + B, so i will write this code under key down event? am i right? or please correct me?
-
Jun 18th, 2011, 02:05 AM
#8
New Member
Re: difference between key press and key down event in vb.net
keypress events execute when a key is simply pressed
keydown events execute ONLY when a key is pressed
keyup events execute ONLY when a key is released
i used keydown and keyup, NOT keypress when messing around with ascii (the integers that represent different keys) to make a label display what keys were held down. keydown added text to the label saying i pressed a key. keyup removed that text. keypress would simply add the text and it would not disappear
-
Jun 18th, 2011, 02:20 AM
#9
New Member
Re: difference between key press and key down event in vb.net
 Originally Posted by ADQUSIT
what you have quoted here sir, honestly i am not picking them up. its still not clear at all to me, that what is actually key down event, and diff b/w key down and key press.
things i did not understand in your blog are
When the user presses a key on the keyboard, active WinForms controls raise multiple events. In the order they are raised, those events are PreviewKeyDown, KeyDown, KeyPress and KeyUp. Following is an explanation of when and how to use each one.
The first event raised by a control is the PreviewKeyDown event. As with all events raised by the Framework, the data for the event is provided by the e parameter; in this case, a PreviewKeyDownEventsArgs object. The purpose of this event is basically the IsInputKey property of that object.
The PreviewKeyDown event was added to the .NET Framework in version 2.0. Prior to that, the only way to specify whether a key is a regular input key or not was to derive your own custom control and override the IsInputKey method. With the addition of the PreviewKeyDown event, we can now handle it for a standard control and specify whether or not the key that was depressed is a regular input key. This way we can more easily provide one-off custom behaviour. For more information on treating a key as a regular input key, consult the MSDN documentation for the Control.IsInputKey method.
here's a better example. includes code to write your own program to demonstrate the difference:
first, open new project. add a label called label1 and a textbox called textbox1.
this is visual basic 6 code:
keydown Code:
private sub textbox1_keydown (keyascii as integer)
if keyascii = 13 then
label1.caption = label1.caption + "enter"
end if
if keyascii = 27 then
label1.caption = label1.caption + "esc"
end if
end sub
private sub textbox1_keyup (keyascii as integer)
if keyascii = 13 then
label1.caption = label1.caption - "enter"
end if
if keyascii = 27 then
label1.caption = label1.caption - "esc"
end if
end sub
this is keypress:
keypress Code:
private sub textbox1_keypress (keyascii as integer)
if keyascii = 13 then
label1.caption = label1.caption + "enter"
end if
if keyascii = 27 then
label1.caption = label1.caption + "enter"
end if
'there's nowhere to place the code that runs when a key is released, therefore the label still says we are holding in enter and esc, even when we release them
end sub
i would give you vb 2008 code, but i don't understand any of the key events (keyascii is much harder to implement in vb 2008)
if you run vb 2008, click tools, upgrade vb6 code and paste the above code into it. click upgrade. private message me if you have trouble.
also, i run vb 2008, but somethings i have trouble with. i'm still almost pro, just a few things i have trouble with (like keyascii)
-
Jun 22nd, 2011, 04:43 AM
#10
Thread Starter
Fanatic Member
Re: difference between key press and key down event in vb.net
let me ask you all something. Is key down only used for Ctrl, Alt, shift and enter key? is it right?
I am also providing a link please check it and answer me about this too.
http://www.bloggingdeveloper.com/pos...ey-Events.aspx
-
Jun 22nd, 2011, 05:33 AM
#11
Re: difference between key press and key down event in vb.net
Let me repeat what I have already said several times: KeyDown detects keyboard keys while KeyPress detects characters. That is all there is to it. Nothing more, nothing less.
-
Jun 22nd, 2011, 09:19 AM
#12
Re: difference between key press and key down event in vb.net
In case these have not been posted:
How Keyboard Input Works
Using Keyboard Events
@adquist - is there something specific you are trying to do?
-
Jun 22nd, 2011, 10:35 AM
#13
Thread Starter
Fanatic Member
Re: difference between key press and key down event in vb.net
No dbasnett, i am not going for something specific. I am honestly not understanding the difference.
-
Jun 22nd, 2011, 10:38 AM
#14
Thread Starter
Fanatic Member
Re: difference between key press and key down event in vb.net
 Originally Posted by jmcilhinney
Let me repeat what I have already said several times: KeyDown detects keyboard keys while KeyPress detects characters. That is all there is to it. Nothing more, nothing less.
sir what does this mean that one detects keyboard and other detects characters? aren't these same things? aren't these characters are on keyboard? Please tell me the difference between keyboard and characters?
-
Jun 22nd, 2011, 12:26 PM
#15
Re: difference between key press and key down event in vb.net
 Originally Posted by ADQUSIT
No dbasnett, i am not going for something specific. I am honestly not understanding the difference.
It might help your understanding if you wrote a simple program, and captured each of the events. Create a new project, add one TextBox and one RichTextBox to the form, and try this code as a start. Run the code and press keys with the cursor in the TextBox .
Code:
Public Class Form1
Dim naviKeys() As Keys = New Keys() {Keys.Up, Keys.Down, Keys.Left, Keys.Right, _
Keys.Tab, Keys.Escape, Keys.Enter}
Private Sub TextBox1_PreviewKeyDown(sender As Object, _
e As System.Windows.Forms.PreviewKeyDownEventArgs) _
Handles TextBox1.PreviewKeyDown
'If naviKeys.Contains(e.KeyCode) Then
' e.IsInputKey = True
'End If
RichTextBox1.AppendText(String.Format("{0} {1} {2}", "Preview", e.KeyCode, e.Modifiers))
scrollRTB()
End Sub
Private Sub TextBox1_KeyDown(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyDown
RichTextBox1.AppendText(String.Format("{0} {1} {2}", "KeyDown", e.KeyCode, e.Modifiers))
scrollRTB()
End Sub
Private Sub TextBox1_KeyPress(sender As Object, _
e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
'note - no KeyCode
RichTextBox1.AppendText(String.Format("{0} {1}", "KeyPress", e.KeyChar))
scrollRTB()
End Sub
Private Sub TextBox1_KeyUp(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyUp
RichTextBox1.AppendText(String.Format("{0} {1} {2}", "KeyUp", e.KeyCode, e.Modifiers))
scrollRTB()
End Sub
Private Sub scrollRTB()
If RichTextBox1.TextLength > 32768 Then
RichTextBox1.Text = RichTextBox1.Text.Remove(0, 16384)
End If
RichTextBox1.AppendText(Environment.NewLine)
RichTextBox1.ScrollToCaret()
End Sub
End Class
-
Jun 22nd, 2011, 08:10 PM
#16
Re: difference between key press and key down event in vb.net
 Originally Posted by ADQUSIT
sir what does this mean that one detects keyboard and other detects characters? aren't these same things? aren't these characters are on keyboard? Please tell me the difference between keyboard and characters?
I already did. Did you not read post #6?
For instance, Shift+A is two keys but only one character
How can I make it any clearer than that? To get one upper-case character in a TextBox you have to press two keys on the keyboard.
Last edited by jmcilhinney; Jun 22nd, 2011 at 08:16 PM.
-
Jun 24th, 2011, 06:01 AM
#17
Thread Starter
Fanatic Member
Re: difference between key press and key down event in vb.net
 Originally Posted by jmcilhinney
I already did. Did you not read post #6?How can I make it any clearer than that? To get one upper-case character in a TextBox you have to press two keys on the keyboard.
Hi sir john, i am not little bit understanding. But if you give me some exercise to do that i could practically work upon it. i tried to do some work with key down event and keypress event, but i had no idea that what to do?
One more thing is please tell me that whenever you quote example of keydown event, so you use shift, Ctrl or Alt key. is it necessary that this key down is using with this?
-
Jun 24th, 2011, 07:06 AM
#18
Re: difference between key press and key down event in vb.net
@ADQUSIT - Are you replying to these messages by pressing keys that cause characters to be generated into groupings of characters called words and sentences, or are you using a different method?
Key down / up deal with the physical keys, and key press deals the character that is generated based on which keys are down. e.g. In key down you will see Keys.D2 with e.Modifiers = Keys.Shift and in key press see the character @ . (Keys.D2 = the 2 key)
In VB .Net you are given several chances to see the different states of a key. Try this code that captures Shift-A and does something different with it.
Code:
'add TextBox1 and RichTextBox1 to your form
Private Sub TextBox1_KeyDown(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyDown
RichTextBox1.AppendText(String.Format("{0} {1} {2}", "KeyDown", e.KeyCode, e.Modifiers))
scrollRTB()
'press Shift and the A key
If e.KeyCode = Keys.A AndAlso e.Modifiers = Keys.Shift Then
TextBox1.SelectedText = "ADQUSIT"
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
Private Sub TextBox1_KeyPress(sender As Object, _
e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
'note - no KeyCode
RichTextBox1.AppendText(String.Format("{0} {1}", "KeyPress", e.KeyChar))
scrollRTB()
End Sub
Private Sub scrollRTB()
If RichTextBox1.TextLength > 32768 Then
RichTextBox1.Text = RichTextBox1.Text.Remove(0, 16384)
End If
RichTextBox1.AppendText(Environment.NewLine)
RichTextBox1.ScrollToCaret()
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|