|
-
Feb 4th, 2011, 11:12 AM
#1
Thread Starter
Member
Changing Keys with a Hook
Hi Guys, My end goal is to make it so I can hook only one Keyboard, Which is not really a keyboard it is a handheld scanner. But I figure this would be the first step. When I scan a barcode with my scanner it outputs the UPC in TEXT, I want to intercept that no matter what window may be open, the reason being is that it may be scanned into a application I did not make, for instance notepad so I can use Keydown/Press events for any app cause I did not make it. So I figure a Global Keyboard hook would be the way to start, and Maybe I can restrict it so It applys just to the one USBHID device.
This is what i got so far, but it does not change a key to the value I'm looking for, In this case when I have notepad open after this hook is installed I want to press the printScreen key and have it type the letter "A" in notepad later to be changed to nothing. Any idea's?
Code:
Option Strict On
Option Explicit On
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()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
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
' we check keyup for standard printscreen, and syskeyup incase it is alt+printscreen
' we aren't checking keydown, as that fires before the screenshot is taken.
If nCode >= 0 AndAlso (wParam.ToInt32 = WM_KEYUP OrElse wParam.ToInt32 = WM_SYSKEYUP) Then
Dim vkCode As Integer = Marshal.ReadInt32(lParam)
Marshal.WriteInt32(lParam, Keys.A)
If vkCode = Keys.PrintScreen Then
Dim data As IDataObject = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(Bitmap)) Then
Me.BackgroundImage = DirectCast(data.GetData(GetType(Bitmap)), Bitmap)
End If
End If
End If
Return CallNextHookEx(hookID, nCode, wParam, lParam)
End Function
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|