Click here to Skip to main content
16,019,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have use word, notepad any application keyboard remapping this application using vb.net programming pls help me......

Moved code from comment to here...
VB
Imports System.Runtime.InteropServices
Public Class Form1
    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYUP As Integer = &H101
    Private Const WM_SYSKEYUP As Integer = &H105
    Private proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
    Private hookID As IntPtr
    Private Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As IntPtr

    <DllImport("user32")> _
    Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, _
        ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <marshalas(unmanagedtype.bool)> Boolean
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As IntPtr
    End Function


    <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
    Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
    End Function

    Sub New()
        InitializeComponent()
        Text = "KeyboardPlus 1.01"
        hookID = SetHook(proc)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
        UnhookWindowsHookEx(hookID)
    End Sub

    Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr
        Using curProcess As Process = Process.GetCurrentProcess()
            Using curModule As ProcessModule = curProcess.MainModule

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

            End Using
        End Using
    End Function

    Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        ' "The WM_KEYUP message is posted to the window with the keyboard focus
        ' when a nonsystem key is released. A nonsystem key is a key that is pressed when the ALT key is not pressed,
        ' or a keyboard key that is pressed when a window has the keyboard focus." 
        

        If nCode >= 0 AndAlso (wParam.ToInt32 = WM_KEYUP OrElse wParam.ToInt32 = WM_SYSKEYUP) Then
            Dim vkCode As Integer = Marshal.ReadInt32(lParam)
            'vkCode = Keys.B
            If vkCode = Keys.A Then
                'SendKeys.Send(vbBack)
                If Console.CapsLock = True Then
                    SendKeys.Send("B")
                    'vkCode = Keys.B
                Else
                    'vkCode = Keys.B
                    SendKeys.Send("b")
                End If

            End If
        End If
        Return CallNextHookEx(hookID, nCode, wParam, lParam)
    End Function

End Class>
Posted
Updated 4-Jan-16 16:09pm
v2
Comments
ZurdoDev 4-Jan-16 21:16pm    
Why?
And where are you stuck?

1 solution

You absolutely can NOT use SendKey inside a Global Keyboard hook. The system is giving you the entire keyobard message, including the key that was hit, lParam. All you have to do is modify the lParam value with the keycode for the key you want to replace it with before you send it up the hook chain. That's it!

If you want to eat the key and not let the system see it. All you have to do is NOT call CallNextHookEx.
 
Share this answer
 
Comments
mediamcce 4-Jan-16 22:44pm    
do not solve this problem
Dave Kreskowiak 4-Jan-16 23:41pm    
OK, fine, I won't.
mediamcce 5-Jan-16 5:29am    
any developer solve this problem
Dave Kreskowiak 5-Jan-16 8:56am    
I told you what to do. By your latest statement, it now appears you want someone to write it for you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900