Question Map the Enter key to Tab key

rayoua

New member
Joined
May 6, 2010
Messages
3
Programming Experience
Beginner
Hello every one

I'm trying to find a code that allow me to Map the Enter key to Tab key for every application that runnig with my form.

I found this code but it not disable the Enter keys. I mean when a press the enter key this code does not block enter key but also press the tab key. In other words this code press enter and tab. The good thing with this code is working for any application that running.

Thanks
__________________________________________________________
Option Strict On
Imports System.Runtime.InteropServices

Public Class Form1

Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
Private Const KEYEVENTF_KEYUP As Long = &H2
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYUP As Integer = &H101
Private Shared _proc As LowLevelKeyboardProc = AddressOf HookCallback
Private Shared _hookID As IntPtr = IntPtr.Zero

Public Declare Auto Function SetWindowsHookEx Lib "user32.dll" ( _
ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, _
ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr

Public Declare Auto Function UnhookWindowsHookEx _
Lib "user32.dll" (ByVal hhk As IntPtr) As IntPtr

Public Declare Auto Function CallNextHookEx _
Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Public Declare Auto Function GetModuleHandle Lib "kernel32.dll" ( _
ByVal lpModuleName As String) As IntPtr


Private Shared Function SetHook( _
ByVal proc As LowLevelKeyboardProc) As IntPtr

Dim curProcess As Process = Process.GetCurrentProcess()
Dim curModule As ProcessModule = curProcess.MainModule

Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, _
GetModuleHandle(curModule.ModuleName), 0)

End Function

Public Delegate Function LowLevelKeyboardProc( _
ByVal nCode As Integer, ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr

Public Shared Function HookCallback( _
ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

If nCode >= 0 And wParam = CType(WM_KEYUP, IntPtr) Then
Dim vkCode As Keys = CType(Marshal.ReadInt32(lParam), Keys)
If vkCode = Keys.Return Or vkCode = Keys.Enter Then
keybd_event(CByte(Keys.Tab), 0, KEYEVENTF_EXTENDEDKEY, 0)
End If
End If

Return CallNextHookEx(_hookID, nCode, wParam, lParam)
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_hookID = SetHook(_proc)
End Sub

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
UnhookWindowsHookEx(_hookID)
End Sub

End Class
 
Please remember to use code tags. It makes reading code so much easier and often prevents people even considering to read you post.

Thankfully for you, your code has nothing to do with this.

You're thinking about this too openly. Narrow down what you actually want to happen.

When you press enter, you want the focus to change to the next control, yes?

VB.NET:
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) [color=red]Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown[/color]
        If e.KeyCode = Keys.Enter Then
            Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
        End If
    End Sub

Notice the code in red.

Make sure to set the KeyPreview property of the form to True.
 
Oh. I just read your post more.

Why do you want to disable enter? You can't disable the enter key if you want to use it to tab.
 
Here the code in code tag

VB.NET:
Option Strict On 
Imports System.Runtime.InteropServices 
 
Public Class Form1 
 
    Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1 
    Private Const KEYEVENTF_KEYUP As Long = &H2 
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _ 
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) 
 
    Private Const WH_KEYBOARD_LL As Integer = 13 
    Private Const WM_KEYUP As Integer = &H101 
    Private Shared _proc As LowLevelKeyboardProc = AddressOf HookCallback 
    Private Shared _hookID As IntPtr = IntPtr.Zero 
 
    Public Declare Auto Function SetWindowsHookEx Lib "user32.dll" ( _ 
        ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, _ 
        ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr 
 
    Public Declare Auto Function UnhookWindowsHookEx _ 
    Lib "user32.dll" (ByVal hhk As IntPtr) As IntPtr 
 
    Public Declare Auto Function CallNextHookEx _ 
    Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, _ 
                      ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr 
 
    Public Declare Auto Function GetModuleHandle Lib "kernel32.dll" ( _ 
    ByVal lpModuleName As String) As IntPtr 
 
 
    Private Shared Function SetHook( _ 
        ByVal proc As LowLevelKeyboardProc) As IntPtr 
 
        Dim curProcess As Process = Process.GetCurrentProcess() 
        Dim curModule As ProcessModule = curProcess.MainModule 
 
        Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, _ 
                GetModuleHandle(curModule.ModuleName), 0) 
 
    End Function 
 
    Public Delegate Function LowLevelKeyboardProc( _ 
        ByVal nCode As Integer, ByVal wParam As IntPtr, _ 
        ByVal lParam As IntPtr) As IntPtr 
 
    Public Shared Function HookCallback( _ 
        ByVal nCode As Integer, _ 
        ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr 
 
        If nCode >= 0 And wParam = CType(WM_KEYUP, IntPtr) Then 
            Dim vkCode As Keys = CType(Marshal.ReadInt32(lParam), Keys) 
            If vkCode = Keys.LWin Or vkCode = Keys.RWin Then 
                keybd_event(CByte(Keys.Tab), 0, KEYEVENTF_EXTENDEDKEY, 0) 
            End If 
        End If 
 
        Return CallNextHookEx(_hookID, nCode, wParam, lParam) 
    End Function 
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        _hookID = SetHook(_proc) 
    End Sub 
 
    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed 
        UnhookWindowsHookEx(_hookID) 
    End Sub 
 
End Class
 
Back
Top