Results 1 to 18 of 18

Thread: difference between key press and key down event in vb.net

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: difference between key press and key down event in vb.net

    Quote Originally Posted by jmcilhinney View Post
    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: difference between key press and key down event in vb.net

    Quote Originally Posted by ADQUSIT View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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?

  8. #8
    New Member
    Join Date
    Jun 2011
    Posts
    3

    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

  9. #9
    New Member
    Join Date
    Jun 2011
    Posts
    3

    Re: difference between key press and key down event in vb.net

    Quote Originally Posted by ADQUSIT View Post
    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:
    1. private sub textbox1_keydown (keyascii as integer)
    2.  
    3. if keyascii = 13 then
    4. label1.caption = label1.caption + "enter"
    5. end if
    6.  
    7. if keyascii = 27 then
    8. label1.caption = label1.caption + "esc"
    9. end if
    10.  
    11. end sub
    12.  
    13.  
    14.  
    15. private sub textbox1_keyup (keyascii as integer)
    16.  
    17. if keyascii = 13 then
    18. label1.caption = label1.caption - "enter"
    19. end if
    20.  
    21. if keyascii = 27 then
    22. label1.caption = label1.caption - "esc"
    23. end if
    24.  
    25. end sub

    this is keypress:

    keypress Code:
    1. private sub textbox1_keypress (keyascii as integer)
    2. if keyascii = 13 then
    3. label1.caption = label1.caption + "enter"
    4. end if
    5.  
    6. if keyascii = 27 then
    7. label1.caption = label1.caption + "enter"
    8. end if
    9.  
    10. '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
    11.  
    12. 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)

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    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.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: difference between key press and key down event in vb.net

    Quote Originally Posted by jmcilhinney View Post
    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?

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: difference between key press and key down event in vb.net

    Quote Originally Posted by ADQUSIT View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: difference between key press and key down event in vb.net

    Quote Originally Posted by ADQUSIT View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: difference between key press and key down event in vb.net

    Quote Originally Posted by jmcilhinney View Post
    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?

  18. #18
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width