Click here to Skip to main content
16,018,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to make an application (in VB 2008) that requires me to remap the keyboard. For example, when I press "A" it should produce the character "Æ" and when I press "shift + A" it should produce the character "Ā". Also it should be system wide i.e. to any windows that has focus and whether or not this application has focus. I hope I have made myself clear. It will be so kind of you if you will take the time to solve this problem. I will be eagerly waiting for your response. Thanks in advance.
Regards
email : [removed]

[Update: do not add an "answer" when responding to questions. Put the information here]
@ Kristian Sixhoej: I am using the following code to remap "a" to "b" but it produces "ab". Also I could not figure out what to do if I want to remap "shift+a". Please look into it and tell me what I am not doing right.

Also @ AnnieMacD : I always find it difficult to follow microsoft articles. They sound too technical.

VB
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.A Or vkCode = Keys.A Then
            keybd_event(CByte(Keys.B), 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
Posted
Updated 27-Jun-10 15:03pm
v4
Comments
Kristian Sixhøj 27-Jun-10 11:13am    
Tip: Do not use the word 'urgent' - it may be urgent to you, but often it will just delay helpful answers.
Also, it's a bad idea to put your email address in your posts, as it could be picked up by spambots.
Anyway, what have you tried so far? Where are you stuck?

1 solution

 
Share this answer
 

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