Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Trap "Enter" key press

Trap "Enter" key press

Scheduled Pinned Locked Moved Visual Basic
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moonshaddow
    wrote on last edited by
    #1

    hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>

    J.Hardy

    L L D S B 5 Replies Last reply
    0
    • M moonshaddow

      hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>

      J.Hardy

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, you should use the KeyDown event, it offers KeyCode which directly compares to Keys.Enter without needing the ASC function; then set SuppressKeyPress when you want to ignore the key. BTW: you managed to use PRE tags the wrong way! :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      • M moonshaddow

        hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>

        J.Hardy

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You need a global keyhook to do that, otherwise that code will only be triggered when your form as focus.

        1 Reply Last reply
        0
        • M moonshaddow

          hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>

          J.Hardy

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You can also set the form's KeyPreview property to True, then handle the form's KeyDown or KeyPress events to do the same thing.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          1 Reply Last reply
          0
          • M moonshaddow

            hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>

            J.Hardy

            S Offline
            S Offline
            StuBaum
            wrote on last edited by
            #5

            I set KeyPreview = true in the form's properties dialog & then trap keydown Private Sub frmInvoice_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyData = Keys.Enter Then 'bla, bla, bla e.Handled = True End If End Sub

            1 Reply Last reply
            0
            • M moonshaddow

              hey, i tried a new way to trap the enter key press event, so it dose not matter what control on the form has focus the enter key will be trapped, can someone correct me please <pre>Private Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If Asc(e.KeyChar) = Keys.Enter Then MsgBox("YES!") e.Handled = True End If End Sub</pre>

              J.Hardy

              B Offline
              B Offline
              Bharat Jain
              wrote on last edited by
              #6

              Set the KeyPreview property of the form to True , that would solve your problem

              -Regards Bharat Jain bharat.jain.nagpur@gmail.com

              M 1 Reply Last reply
              0
              • B Bharat Jain

                Set the KeyPreview property of the form to True , that would solve your problem

                -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                M Offline
                M Offline
                moonshaddow
                wrote on last edited by
                #7

                hey guy thanks for all your help :)

                J.Hardy

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups