Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 28-06-2010, 09:14 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Jun 2010
Posts: 1
Default VB 2008 keyboard hooking


Hi, I have been trying to build an application for which I need to set low-level keyboard hooks. I thing I am trying to accomplish is that when I press the key "a" I want windows to think I have pressed the key "b". But the problem I am facing is that when I press key "a" it produces "ab". I am providing my code below. Any help will be highly appreciated. Also I would like to know how to detect double and triple key presses (e.g. shift + a and ctrl + alt + b).

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
End Class
taurian is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 01-07-2010, 05:40 PM   #2 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: VB 2008 keyboard hooking

hey can u just upload ur project here, i has VB 2010, i will convert ur VB 2008 project to VB 2010 project. i can help!!
abhijangda is offline   Reply With Quote
Old 06-07-2010, 08:20 AM   #3 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: VB 2008 keyboard hooking

Read this:

A Simple C# Global Low Level Keyboard Hook - CodeProject

This helped me a lot

Convert the code to VB using Convert C# to VB.NET - A free code conversion tool - developer Fusion
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Win XP bootable CD aadipa Tutorials 81 07-02-2011 05:28 PM
Due Kernel I/O error, system refuses to boot randomly gary4gar Open Source 33 04-05-2008 10:22 AM
Xbox 360: Games of 2008 gangadhar Gamerz 8 05-04-2008 01:30 PM
Norton AntiVirus 2008 ! ! ! ax3 Software Q&A 31 13-10-2007 07:38 PM
How to: Clean your keyboard. djmykey Tutorials 3 21-10-2005 12:39 PM

 
Latest Threads
- by Charan
- by Charan
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 03:19 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2