Error with Delegate VB.NET - vb.net

I am trying to make a class which will detect what mouse button was pressed each time, but I'm getting 2 errors in the Hookmouse() sub which regards to delegates. Could someone help me understand what am I doing wrong ? I would really aprreciate if someone could explain me more about delegates except from correcting this piece of code.
The 2 errors I get:
Method 'Private Function MouseProc(nCode As Integer, wParam As IntPtr, ByRef lParam As MouseHook.MouseHookStruct) As Integer' does not have a signature compatible with delegate 'Delegate Function MouseHook.MouseProcDelegate(nCode As Integer, wParam As IntPtr, lParam As MouseHook.MouseHookStruct) As Integer'.
Value of type 'MouseHook.MouseProcDelegate' cannot be converted to 'MouseHook.CallBack'.
Imports System.Runtime.InteropServices
Public Class MouseHook
Public Delegate Function CallBack(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As MouseHookStruct) As Integer
'Declare the mouse hook constant.
'For other hook types, obtain these values from Winuser.h in Microsoft SDK.
Dim WH_MOUSE As Integer = 7
Shared hHook As Integer = 0
'Keep the reference so that the delegate is not garbage collected.
Private hookproc As CallBack
'Import for the SetWindowsHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Overloads Shared Function SetWindowsHookEx _
(ByVal idHook As Integer, ByVal HookProc As CallBack,
ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
'Import for the CallNextHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Overloads Shared Function CallNextHookEx _
(ByVal idHook As Integer, ByVal nCode As Integer,
ByVal wParam As IntPtr, ByVal lParam As MouseHookStruct) As Integer
End Function
'Import for the UnhookWindowsHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Overloads Shared Function UnhookWindowsHookEx _
(ByVal idHook As Integer) As Boolean
End Function
Private Delegate Function MouseProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As MouseHookStruct) As Integer
'Point structure declaration.
<StructLayout(LayoutKind.Sequential)> Public Structure Point
Public x As Integer
Public y As Integer
End Structure
'MouseHookStruct structure declaration.
<StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
Public pt As Point
Public hwnd As Integer
Public wHitTestCode As Integer
Public dwExtraInfo As Integer
End Structure
Private Const HC_ACTION As Integer = 0
Private Const WH_MOUSE_LL As Integer = 14
Private Const WM_MOUSEMOVE As Integer = &H200
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_LBUTTONDBLCLK As Integer = &H203
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const WM_RBUTTONUP As Integer = &H205
Private Const WM_RBUTTONDBLCLK As Integer = &H206
Private Const WM_MBUTTONDOWN As Integer = &H207
Private Const WM_MBUTTONUP As Integer = &H208
Private Const WM_MBUTTONDBLCLK As Integer = &H209
Private Const WM_MOUSEWHEEL As Integer = &H20A
Private MouseHook As Integer
Private MouseHookDelegate As MouseProcDelegate
Public Event Mouse_Move(ByVal ptLocat As Point)
Public Event Mouse_Left_Down(ByVal ptLocat As Point)
Public Event Mouse_Left_Up(ByVal ptLocat As Point)
Public Event Mouse_Left_DoubleClick(ByVal ptLocat As Point)
Public Event Mouse_Right_Down(ByVal ptLocat As Point)
Public Event Mouse_Right_Up(ByVal ptLocat As Point)
Public Event Mouse_Right_DoubleClick(ByVal ptLocat As Point)
Public Event Mouse_Middle_Down(ByVal ptLocat As Point)
Public Event Mouse_Middle_Up(ByVal ptLocat As Point)
Public Event Mouse_Middle_DoubleClick(ByVal ptLocat As Point)
Public Event Mouse_Wheel(ByVal ptLocat As Point, ByVal Direction As Wheel_Direction)
Public Enum Wheel_Direction
WheelUp
WheelDown
End Enum
Private Function MouseProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MouseHookStruct) As Integer
If (nCode = HC_ACTION) Then
Select Case wParam
Case WM_MOUSEMOVE
RaiseEvent Mouse_Move(lParam.pt)
Case WM_LBUTTONDOWN
RaiseEvent Mouse_Left_Down(lParam.pt)
Case WM_LBUTTONUP
RaiseEvent Mouse_Left_Up(lParam.pt)
Case WM_LBUTTONDBLCLK
RaiseEvent Mouse_Left_DoubleClick(lParam.pt)
Case WM_RBUTTONDOWN
RaiseEvent Mouse_Right_Down(lParam.pt)
Case WM_RBUTTONUP
RaiseEvent Mouse_Right_Up(lParam.pt)
Case WM_RBUTTONDBLCLK
RaiseEvent Mouse_Right_DoubleClick(lParam.pt)
Case WM_MBUTTONDOWN
RaiseEvent Mouse_Middle_Down(lParam.pt)
Case WM_MBUTTONUP
RaiseEvent Mouse_Middle_Up(lParam.pt)
Case WM_MBUTTONDBLCLK
RaiseEvent Mouse_Middle_DoubleClick(lParam.pt)
Case WM_MOUSEWHEEL
Dim wDirection As Wheel_Direction
If lParam.hwnd < 0 Then
wDirection = Wheel_Direction.WheelDown
Else
wDirection = Wheel_Direction.WheelUp
End If
RaiseEvent Mouse_Wheel(lParam.pt, wDirection)
End Select
End If
Return CallNextHookEx(MouseHook, nCode, wParam, lParam)
End Function
Protected Overrides Sub Finalize()
UnhookWindowsHookEx(MouseHook)
MyBase.Finalize()
End Sub
Public Sub HookMouse()
MouseHookDelegate = New MouseProcDelegate(AddressOf MouseProc)
MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End Sub
Public Sub UnhookMouse()
UnhookWindowsHookEx(MouseHook)
End Sub
End Class

Related

Force scroll to the top of a RichTextBox

I'm using the following code to force the RichTextBox to scroll to the real bottom
Private Const WM_VSCROLL As Integer = 277
Private Const SB_PAGEBOTTOM As Integer = 7
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Friend Shared Sub ScrollToBottom(ByVal richTextBox As RichTextBox)
SendMessage(richTextBox.Handle, WM_VSCROLL, CType(SB_PAGEBOTTOM, IntPtr), IntPtr.Zero)
richTextBox.SelectionStart = richTextBox.Text.Length
End Sub
' to call it
ScrollToBottom(RichTextBox1)
Is there a way to reverse this code so I can force it to to always scroll up?
I am not looking to append text solutions.
Thanks
This Should do it:
Private Const WM_VSCROLL As Integer = &H115
Private Const SB_TOP As Integer = 6
Private Const SB_BOTTOM As Integer = 7
<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
' scroll RichTextBox
Friend Shared Sub ScrollRichTextBox(ByVal richTextBox As RichTextBox, Direction as integer)
SendMessage(richTextBox.Handle, WM_VSCROLL, Direction, 0)
End Sub
' to call it
ScrollRichTextBox(RichTextBox1, SB_TOP) 'Scrolls to top
ScrollRichTextBox(RichTextBox1, SB_BOTTOM) 'Scrolls to bottom

Show ToolTips on ListView first row in Framework 4.0

I have a WinForm application developed in Framework 2.0 with VB.Net which was using the event MouseMove on all the ListView objects to display ToolTip text on the first row of the ListViews - as it's not possible to have ToolTips on ColumnHeader, as far as I know, without third part tools.
The problem is that since I converted the application to Framework 4.0 this "trick" is not working and the ToolTips are not displayed anymore.
Does anyone know a solution or, even better, a way to display ToolTips on ListView ColumnHeaders?
Here's my code snippet:
Private Sub ShowTooltip(ByVal sender As Object, ByVal e As MouseEventArgs)
Handles myListView.MouseMove
Dim iColumn As System.Int32 = FindListViewColumnHeader(e.X, e.Y)
If Me.myListView.Columns.Count > 0 AndAlso iColumn >= 0 AndAlso
iColumn <= Me.myListView.Columns.Count - 1 Then
Me.myToolTip.Active = True
Me.myToolTip.UseAnimation = True
Me.myToolTip.UseFading = True
Me.myToolTip.AutomaticDelay = 10000
Me.myToolTip.AutoPopDelay = 10000
Me.myToolTip.InitialDelay = 0
Me.myToolTip.ReshowDelay = 2000
Dim sTooltipText As System.String = SomeText(...)
If sTooltipText <> DirectCast(Me.myToolTip.Tag, System.String) Then
Me.myToolTip.Tag = sTooltipText
Me.myToolTip.SetToolTip(Me.myListView, sTooltipText)
End If
Else
Me.myToolTip.Active = False
End If
End Sub
Protected Overridable Function FindListViewColumnHeader(ByVal X As System.Int32,
ByVal Y As System.Int32) As System.Int32
If Y > 20 And Y < 40 Then
Dim iCount As System.Int32
Dim iLeft As System.Int32
For iCount = 0 To myListView.Columns.Count - 1
iLeft = iLeft + myListView.Columns(iCount).Width
If X <= iLeft Then
Return iCount
Exit For
End If
Next
Return iCount
Else
Return -1
End If
End Function
Note: myToolTip is
Friend WithEvents myToolTip As System.Windows.Forms.ToolTip
and myListView is
Protected WithEvents myListView As System.Windows.Forms.ListView
Please notice that, as suggested in the question:
How to set tooltip for a ListviewItem, ShowItemToolTips is already set to True.
You can get the handle of the header column and subclass it:
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal newProc As Win32WndProc) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function CallWindowProc(lpPrevWndFunc As IntPtr, hWnd As IntPtr, Msg As UInteger, wParam As Integer, lParam As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function
Private Delegate Function Win32WndProc(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const GWL_WNDPROC As Integer = -4
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_MOUSEMOVE As Integer = &H200
Private oldWndProc As IntPtr = IntPtr.Zero
Private newWndProc As Win32WndProc = Nothing
Private Sub SubclassHWnd(ByVal hWnd As IntPtr)
'hWnd is the window you want to subclass...,
'create a new delegate for the new wndproc
newWndProc = New Win32WndProc(AddressOf MyWndProc)
'subclass
oldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, newWndProc)
End Sub
Private Function MyWndProc(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Select Case Msg
Case WM_LBUTTONDOWN
'The lower 2 bytes of lParam are the x coordinate
'and the higher 2 bytes the y.
ToolTip1.Show("My tooltip", ListView1, lParam And &HFFFF, (lParam >> 16) And &HFF, 2000)
Exit Select
Case Else
Exit Select
End Select
Return CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam)
End Function
To subclass the header use:
'LVM_GETHEADER = &H101F
Dim hwndHeader As IntPtr = SendMessage(ListView1.Handle, &H101F, 0, 0)
SubclassHWnd(hwndHeader)
I used the WM_LBUTTONDOWN event for convenience. You can use the WM_MOUSEMOVE event and check which column the mouse is etc... and show the tooltip
The code for subclassing: Subclass an Unmanged Window in C#

VB.NET Sending Strings via Postmessage

OK, this problem has been bugging me for a long time.
I have the code for the service which communicates a string to the client via PostMessage:
Public Sub SendToClient(msgs As String, types As Integer, hwnd As Long)
postMessage(hwnd, 0, Nothing, msgs)
End Sub
Then I have the client that receives the string:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If (m.Msg = 0) Then
Dim a as string
a = Marshal.PtrToStringAuto(m.LParam)
end if
MyBase.WndProc(m)
End Sub
However, the client sends an error, or some jumble of binary data or even just a blank string sometimes. By the way, the m.LParam is a number.
Can someone tell me what is the right way to send/receive strings via postmessage.
I do it like this:
Public Sub SendMessageToApp(ByVal NombreVentana As String, ByVal Mensaje As String, ByVal sender As Form)
Dim hWnd As IntPtr
Dim mCopyData As COPYDATASTRUCT
hWnd = CType(FindWindow(Nothing, NombreVentana), IntPtr)
Dim message As New System.Text.StringBuilder
If (CInt(hWnd) <> 0) Then
message.Append(Mensaje)
Dim pCopyData As IntPtr = Marshal.AllocHGlobal(message.Length() + 40)
mCopyData.lpData = Marshal.StringToHGlobalAnsi(message.ToString)
mCopyData.cbData = message.Length
mCopyData.dwData = CType(_messageID, IntPtr)
Marshal.StructureToPtr(mCopyData, pCopyData, False)
SendMessage(hWnd, WM_COPYDATA, CInt(sender.Handle), pCopyData)
Marshal.FreeHGlobal(mCopyData.lpData)
Marshal.FreeHGlobal(pCopyData)
End If
End Sub
Receiver window:
Declarations and definitions:
Const WM_COPYDATA As Integer = 74
Const SIG_LENGTH As Integer = 36
Const MAX_COPY_LENGTH As Integer = 128
Const SigConnect As String = "F7B82657-BD18-4ee6-B182-78721293821C"
Dim CDCount As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As IntPtr) As Integer
Dim hWndSender As Integer
Private Const _messageID As Integer = 10
'Estructura obligatoria para poder utilizar el API CopyData
<StructLayout(LayoutKind.Sequential)> _
Private Structure COPYDATASTRUCT
Public dwData As IntPtr
Public cbData As Integer
Public lpData As IntPtr
End Structure
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_COPYDATA Then
Dim Estructura As COPYDATASTRUCT
Dim tipo As Type = Estructura.GetType
Dim message As String
Estructura = CType(m.GetLParam(GetType(COPYDATASTRUCT)), COPYDATASTRUCT)
'Here you get the message
message = Marshal.PtrToStringAnsi(Estructura.lpData, Estructura.cbData)
End If

Check if active window handle is password box

i want to check if the active window handle is password box.
this function returns me the active control handle of the active window:
Imports System.Runtime.InteropServices
Public Class FormMain
Inherits Form
Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal ProcessId As IntPtr) As IntPtr
Private Declare Function AttachThreadInput Lib "user32.dll" (ByVal idAttach As IntPtr, ByVal idAttachTo As IntPtr, ByVal fAttach As Boolean) As IntPtr
Private Declare Function GetFocus Lib "user32.dll" () As IntPtr
Public Sub New()
MyBase.New
InitializeComponent
End Sub
Private Sub timerUpdate_Tick(ByVal sender As Object, ByVal e As EventArgs)
labelHandle.Text = ("hWnd: " + FocusedControlInActiveWindow.ToString)
End Sub
Private Function FocusedControlInActiveWindow() As IntPtr
Dim activeWindowHandle As IntPtr = GetForegroundWindow
Dim activeWindowThread As IntPtr = GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero)
Dim thisWindowThread As IntPtr = GetWindowThreadProcessId(Me.Handle, IntPtr.Zero)
AttachThreadInput(activeWindowThread, thisWindowThread, true)
Dim focusedControlHandle As IntPtr = GetFocus
AttachThreadInput(activeWindowThread, thisWindowThread, false)
Return focusedControlHandle
End Function
End Class
now i want to do something like:
if FocusedControlInActiveWindow() <> intptr.zero then
dim IsPass as boolean = isPassword(FocusedControlInActiveWindow())
if IsPass then
msgbox("yes")
else
msgbox ("no")
end if
end if
how can i check if the foucsed control in the active window text is passwordcahr?
If you want to check if a Windows Edit Control has the ES_PASSWORD style, this is how to do it:
Public Shared Function HasPasswordStyle(ByVal hWnd As IntPtr) As Boolean
Return ((GetWindowLong(hWnd, GWL_STYLE) And ES_PASSWORD) <> 0)
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
Private Const ES_PASSWORD As Integer = 32
Private Const GWL_STYLE As Integer = -16

How to trap keypress event in MSword using VSTO?

I am new to VSTO VB.Net programming. and I am developing a word application level Addin and want to trap keypress event. I have tried various codes of hooking but none is working. I want to use application level hook using WH_KEYBOARD instead of WH_KEYBOARD_LL. The following code which I have tried traps just one key stroke after that it stops. Moreover I could not understand where to put trap the keystrokes. How would I use the following for handling key events.
Public Event KeyDown As KeyEventHandler
Public Event KeyPress As KeyPressEventHandler
Public Event KeyUp As KeyEventHandler
The code that I am using is
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class KeyBoardHook
Inherits Component
Dim PredictString As String
#Region " keyboardHook"
Private Declare Auto Function LoadLibrary Lib "kernel32" (ByVal lpFileName As String) As IntPtr
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As IntPtr) As Boolean
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
ByVal lpfn As KeyboardProc, ByVal hmod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
Private Delegate Function KeyboardProc(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As IntPtr, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
Private Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
'Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYDOWN As Integer = &H104
Private Const WM_SYSKEYUP As Integer = &H105
Private Const WH_KEYBOARD As Integer = 2
Public hKeyboardHook As IntPtr
Public Event KeyDown As KeyEventHandler
Public Event KeyPress As KeyPressEventHandler
Public Event KeyUp As KeyEventHandler
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As Integer
Private Const VK_ALT As Integer = &H12
Private Const VK_CONTROL As Integer = &H11
Private Const VK_SHIFT As Integer = 16
<MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardProc
Public Sub HookKeyboard()
callback = New KeyboardProc(AddressOf KeyboardCallback)
Dim hInstance As IntPtr = LoadLibrary("User32")
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, callback, hInstance, 0)
CheckHooked()
End Sub
Private Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
Dim xy As System.Drawing.Point = Cursor.Position()
Try
If (Code = HC_ACTION Or Code = 3) Then
Dim CapsLock As Boolean = GetKeyState(Keys.CapsLock) = 1
Dim shifting As Boolean = False
Dim modifiers As Keys
If GetAsyncKeyState(VK_CONTROL) <> 0 Then
modifiers = modifiers Or Keys.Control
End If
If GetAsyncKeyState(VK_SHIFT) <> 0 Then
modifiers = modifiers Or Keys.Shift
shifting = True
End If
If GetAsyncKeyState(VK_ALT) <> 0 Then
modifiers = modifiers Or Keys.Alt
End If
Static lastKeys As Keys
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
RaiseEvent KeyDown(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
If lastKeys <> (DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers) Then
lastKeys = (DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers)
If CapsLock AndAlso shifting Then
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToLower(Chr(lParam.vkCode))))
ElseIf Not CapsLock AndAlso shifting Then
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToUpper(Chr(lParam.vkCode))))
ElseIf Not shifting Then
If CapsLock Then
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToUpper(Chr(lParam.vkCode))))
Else
RaiseEvent KeyPress(Me, New KeyPressEventArgs(Char.ToLower(Chr(lParam.vkCode))))
End If
End If
End If
Case WM_KEYUP, WM_SYSKEYUP
If CapsLock AndAlso shifting Then
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
ElseIf Not CapsLock AndAlso shifting Then
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
ElseIf Not shifting Then
If CapsLock Then
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
Else
RaiseEvent KeyUp(Me, New KeyEventArgs(DirectCast(Asc(Chr(lParam.vkCode)), Keys) Or modifiers))
End If
End If
lastKeys = Nothing
End Select
End If
MsgBox("Keypressed is -> " & lParam.vkCode)
Return CallNextHookEx(hKeyboardHook, Code, wParam, lParam)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Private Function keyboardHooked() As Boolean
Return hKeyboardHook <> IntPtr.Zero
End Function
Public Sub UnhookKeyboard()
If keyboardHooked() Then
UnhookWindowsHookEx(hKeyboardHook)
End If
End Sub
#End Region
Private Sub CheckHooked()
If keyboardHooked() Then
MsgBox("Keyboard hooked")
Else
MsgBox("Keyboard hook failed: " & Err.LastDllError)
End If
End Sub
End Class
Your question is a possible duplicate of:
How to get the "KeyPress" event from a Word 2010 Addin (developed in C#)?
How to raise an event on MS word Keypress
Capturing keydown event of MS Word using C#
... however, the answer remains the same: you simply can't :)
In my answer to the last of the questions listed above I explain the reason behind this in a bit more detail, and also covers a possible alternative solution involving the WindowSelectionChange event.