CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 1999
    Posts
    9

    Catching vbReturn results in a BEEP sound, why??

    Hi there,

    this one will hunt me for the rest of my life I fear! Already in my MFC applications I encountered the problem that when I try to catch the "Return" (enter) keycode in the keydown event, the system still
    plays a beeping sound to indicated something is wrong (aka wrong key is pressed).
    Here what I am doing:

    private Sub CtrlEdit_KeyDown(KeyCode as Integer, Shift as Integer)
    Dim ShiftDown as Boolean
    Dim AltDown as Boolean
    Dim CtrlDown as Boolean
    Dim AnyDown as Boolean

    ShiftDown = (Shift And vbShiftMask) > 0
    AltDown = (Shift And vbAltMask) > 0
    CtrlDown = (Shift And vbCtrlMask) > 0
    AnyDown = ShiftDown Or AltDown Or CtrlDown

    If AnyDown then
    Exit Sub
    End If
    Select Case KeyCode
    Case vbKeyReturn', vbKeyExecute
    ' some stuff here
    'KeyCode = 0 ' already tried this to mark this key-event as done
    end select




    Any hints how to get rid of this annoying sound?
    (btw: same happens for tab and escape character)


  2. #2
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: Catching vbReturn results in a BEEP sound, why??

    Hi
    It surprises me that the enter key beeps with youre code. The code with Keycode = 0 not as comment that is... I use pretty the same thing, although the first thing I do when the enter is pressed, is setting the keycode to 0, and then I do some other stuff.
    Maybe that's why it doesn't beep here and it does with you.

    Crazy D :-)



  3. #3
    Join Date
    Apr 1999
    Posts
    45

    Re: Catching vbReturn results in a BEEP sound, why??

    All you do is set the keycode to 0 like you commented out in your code and it gets rid of the annoying beep.

    Gordito


  4. #4
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Catching vbReturn results in a BEEP sound, why??

    Hi,

    I guess you are using single line Text box.
    You might have to 'eat' away the key stroke in KeyPress event also.
    Take a look at the following code, and the trace output that follows: (A form with a text box):


    private Sub Text1_KeyDown(KeyCode as Integer, Shift as Integer)
    Debug.print "KeyDown : Before altering: keycode = " & Str(KeyCode)
    If KeyCode = vbKeyReturn then KeyCode = 0
    Debug.print "KeyDown : After altering: keycode = " & Str(KeyCode)
    End Sub

    private Sub Text1_KeyPress(KeyAscii as Integer)
    Debug.print "KeyPress : Before altering: keyAscii = " & Str(KeyAscii)
    If KeyAscii = vbKeyReturn then KeyAscii = 0
    Debug.print "KeyPress : After altering: keyAscii = " & Str(KeyAscii)
    End Sub



    and the trace output is like this:

    KeyDown : Before altering: keycode = 13
    KeyDown : After altering: keycode = 0
    KeyPress : Before altering: keyAscii = 13
    KeyPress : After altering: keyAscii = 0




    So, it is clear that VB still generates a KeyPress event with KeyAscii = 13 (enter key) even though you set the keycode to 0 in keydown.
    Probably the way Keydown is connected internally to keypress in not all that straight forward!!.

    and single line text box doesnt like enter in KeyPress so beeps.
    Comment out the code in KeyPress and it still beeps.

    Ravi


  5. #5
    Join Date
    Dec 2006
    Posts
    1

    Re: Catching vbReturn results in a BEEP sound, why??

    Private Sub TreeView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TreeView1.KeyDown

    '''''TRY ADDING THESE TWO'''''''''
    e.Handled = True
    e.SuppressKeyPress = True
    '''''TRY ADDING THESE TWO'''''''''


    If Not Me.TreeView1.SelectedNode Is Nothing Then
    If e.Control = True Then
    Select Case e.KeyCode
    Case Keys.R
    Me.cmnuRename_Click(Nothing, Nothing)
    End Select

    End If
    End If
    End Sub

    LET ME KNOW!!!

    From,
    CITEMS

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Catching vbReturn results in a BEEP sound, why??

    Citems, that's VB.NET code

  7. #7
    Join Date
    Oct 2006
    Posts
    327

    Re: Catching vbReturn results in a BEEP sound, why??

    If you use the Keydown event for certain purposes (other keys), you should at the same time use the keypress event for certain keys :

    Try this in a not multiline textbox (so that an ENTER sould provoque a beep)

    Code:
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
      If KeyCode = 13 Then Keycode = 0
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      If KeyAscii = 13 Then KeyAscii = 0
    End Sub
    No beep !

    Note : it would not be necessary to use the keydown event at all, of course

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

Featured