Malcolm Salvador
Malcolm Salvador

Reputation: 1566

Differentiate between enter keys in keydown event

you can detect the enter key if it is pressed in VB.net using the Keydown handler:

Private Sub kd(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
     IF e.key=key.Enter Then
       <do your code>
     Endif
End Sub

However, is there a method to tell which of the two enter keys in a keyboard was pressed? Since you have two enter keys in a keyboard (one next to the apostrophe (middle) and one on the num pad), I want to have the middle enter button have a different response from the numpad enter button.

an example: in photoshop, when using the Text tool, you can press the middle enter to add another line of text, or press the numpad enter to confirm what you have typed.

Upvotes: 2

Views: 814

Answers (1)

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

Taken from http://support.microsoft.com/kb/188550/en-us

Code in VB.net

Option Strict Off
Option Explicit On
Friend Class Form1
    Inherits System.Windows.Forms.Form
    Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (ByRef lpMsg As MSG, ByVal hwnd As Integer, ByVal wMsgFilterMin As Integer, ByVal wMsgFilterMax As Integer, ByVal wRemoveMsg As Integer) As Integer

    Private Structure POINTAPI
        Dim x As Integer
        Dim y As Integer
    End Structure

    Private Structure MSG
        Dim hwnd As Integer
        Dim message As Integer
        Dim wParam As Integer
        Dim lParam As Integer
        Dim time As Integer
        Dim pt As POINTAPI
    End Structure

    Const PM_NOREMOVE As Integer = &H0
    Const WM_KEYDOWN As Integer = &H100
    Const WM_KEYUP As Integer = &H101
    Const VK_RETURN As Integer = &HD

    Private Sub Form1_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim KeyCode As Short = eventArgs.KeyCode
        Dim Shift As Short = eventArgs.KeyData \ &H10000
        Dim MyMsg As MSG
        Dim RetVal As Integer

        ' pass:
        '   MSG structure to receive message information
        '   my window handle
        '   low and high filter of 0, 0 to trap all messages
        '   PM_NOREMOVE to leave the keystroke in the message queue
        '   use PM_REMOVE (1) to remove it
        RetVal = PeekMessage(MyMsg, Me.Handle.ToInt32, 0, 0, PM_NOREMOVE)

        ' now, per Q77550, you should look for a MSG.wParam of VK_RETURN
        ' if this was the keystroke, then test bit 24 of the lparam - if ON,
        ' then keypad was used, otherwise, keyboard was used
        If RetVal <> 0 Then
            If MyMsg.wParam = VK_RETURN Then
                If MyMsg.lParam And &H1000000 Then
                    MsgBox("Enter from Keypad pressed")
                Else
                    MsgBox("Enter from Keyboard pressed")
                End If
            End If
        Else
            MsgBox("No message waiting, or possible problems calling PeekMessage")
        End If
    End Sub
End Class

Upvotes: 3

Related Questions