Disable Enter as button click


Nov 25, 2009
Programming Experience
Is there a way to prevent the Enter key from being interpreted as a button click.

I am trapping key strokes at the form level but if a button has the focus and the ENTER key is pressed I cannot trap the enter because the button consumes it as a button click event. I am also unable to trap the enter key in any of the button's key events.

I am interfacing with magtripe swipe and barcode readers that send their information as keystrokes with a CRLF at the end. I have a work around by chaning the focus to a textbox anytime keystrokes are detected but I am hoping for better control.

Is there a way to disable the buttons ability to do this?

Thank you


VB.NET Forum Moderator
Staff member
Aug 17, 2004
Sydney, Australia
Programming Experience
You'll need to create your own custom control that filters out the Enter key. It's very simple:
Public Class ButtonEx
    Inherits Button

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Return keyData = Keys.Enter OrElse MyBase.ProcessCmdKey(msg, keyData)
    End Function

End Class
Just add that class to your project and build and you can then add your new ButtonEx control to your forms from the Toolbox. If you already have Button controls on a form you can simply open the designer code file and change System.Windows.Forms.Button to ButtonEx in the code. To see the designer code files in the Solution Explorer you must click the Show All Files button.
Top Bottom