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.
Related
I use the code down below to start an application and move it into a Panel on my Form. In this example I use Notepad, only as an example. Later I will use a different application.
When another application is moved in front of my Form, I can only move my Form to the foreground by clicking the title bar. If I click on the MDI child area (so the Panel where Notepad is moved into), nothing happens.
Is there a way to enable that?
Imports System.Runtime.InteropServices
Public Class Form1
Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim proc As Process
proc = Process.Start("notepad.exe")
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
SendMessage(proc.MainWindowHandle, 274, 61488, 0)
End Sub
End Class
The problem is that a hosted (re-parented) Window, when activated, doesn't cause the hosing Form to also activate, so it's brought to the foreground.
The hosted Window is not exactly a child Window and the hosting Form doesn't receive any message from it.
A possible method to bring to the foreground the Form that hosts a foreign Window, when this Window receives the focus (you click or otherwise activate it)
It uses SetWinEventHook to install a Hook that monitors changes in state of the placement of a Window (EVENT_SYSTEM_FOREGROUND).
You specify the handle of the Window of interests, (your proc.MainWindowHandle here), plus its ProcessId and ThreadId. Note that these are not the same as your app's, a call to GetWindowThreadProcessId() is required to get this information.
When you activate the foreign Window, the Hooks calls the specified callback delegate (here, ForegrundChangedEventDelegate), which in turn executes the method that it points to (ForegroundStateChangedCallback)
When this method is called, it checks whether the object that caused the notification is OBJID_WINDOW and that the event is actually EVENT_SYSTEM_FOREGROUND. If that's the case, it calls SetWindowPos to bring the hosting Form to the foreground, but without activating it, so the hosted Window doesn't lose focus
Notes:
The SetWinEvetHook delegate is created in the Constructor (Sub New()) of the parent Form, along with a GC SafeHandle that is used to prevent the delegate from being garbage-collected at the wrong time.
It's released in the OnHandleDestroyed() override, where also the hook proc is un-hooked
see the imports statement in the Form:
Imports [Your App Namespace].NativeMethods
this means that you have to specify the namespace of your app for the import to work as intended. In VB.Net, usually the name of the app and its main NameSpace match; if your app is named, WinFormsApp1, then its Imports WinFormsApp1.NativeMethods
To activate the Hook, as soon as you have changed the Parent of that Window, use its handle to call the SetForegroundStateChangedHook() method. That's all.
When the Form closes, the hook is released
I suggest using the code in Unhook Window into its original State to set the Parent (and, possibly, set it back to the original value before closing the hosting form). You can send WM_CLOSE to the Window if needed.
Imports [Your App Namespace].NativeMethods
Public Class SomeForm
Private hForegrundChangedEventHook As IntPtr
Private ReadOnly ForegrundChangedEventDelegate As WinEventDelegate
Private Shared GCForegroundStateSafetyHandle As GCHandle
Public Sub New()
InitializeComponent()
ForegrundChangedEventDelegate = New WinEventDelegate(AddressOf ForegroundStateChangedCallback)
GCForegroundStateSafetyHandle = GCHandle.Alloc(ForegrundChangedEventDelegate)
End Sub
Protected Overrides Sub OnHandleDestroyed(e As EventArgs)
GCForegroundStateSafetyHandle.Free()
UnhookWinEvent(hForegrundChangedEventHook)
MyBase.OnHandleDestroyed(e)
End Sub
Private Sub SetForegroundStateChangedHook(hWnd As IntPtr)
Dim processId As UInteger
Dim targetThreadId = GetWindowThread(hWnd, processId)
hForegrundChangedEventHook = WinEventHookOne(SWEH_Events.EVENT_SYSTEM_FOREGROUND, ForegrundChangedEventDelegate, processId, targetThreadId)
End Sub
Friend Sub ForegroundStateChangedCallback(hWinEventHook As IntPtr, eventType As SWEH_Events, hWnd As IntPtr, idObject As SWEH_ObjectId, idChild As Long, dwEventThread As UInteger, dwmsEventTime As UInteger)
If idObject = SWEH_ObjectId.OBJID_WINDOW AndAlso eventType = SWEH_Events.EVENT_SYSTEM_FOREGROUND Then
Dim flags = SWP_Flags.SWP_ASYNCWINDOWPOS Or SWP_Flags.SWP_NOACTIVATE Or SWP_Flags.SWP_NOSIZE Or SWP_Flags.SWP_NOMOVE
SetWindowPos(Handle, IntPtr.Zero, 0, 0, 0, 0, flags)
End If
End Sub
End Class
NativeMethods class
Add this class to the Project and import in your Form as described
Imports System.Runtime.InteropServices
Public Class NativeMethods
<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function GetWindowThreadProcessId(hWnd As IntPtr, ByRef lpdwProcessId As UInteger) As UInteger
End Function
<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, x As Integer, y As Integer, cx As Integer, cy As Integer, uFlags As SWP_Flags) As Boolean
End Function
Friend Delegate Sub WinEventDelegate(
hWinEventHook As IntPtr,
eventType As SWEH_Events,
hwnd As IntPtr, idObject As SWEH_ObjectId,
idChild As Long,
dwEventThread As UInteger,
dwmsEventTime As UInteger)
<DllImport("user32.dll", SetLastError:=False)>
Friend Shared Function SetWinEventHook(
eventMin As SWEH_Events,
eventMax As SWEH_Events,
hmodWinEventProc As IntPtr,
lpfnWinEventProc As WinEventDelegate,
idProcess As UInteger,
idThread As UInteger,
dwFlags As SWEH_dwFlags) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=False)>
Friend Shared Function UnhookWinEvent(hWinEventHook As IntPtr) As Boolean
End Function
Friend Shared WinEventHookInternalFlags As SWEH_dwFlags =
SWEH_dwFlags.WINEVENT_OUTOFCONTEXT Or SWEH_dwFlags.WINEVENT_SKIPOWNPROCESS
Friend Shared Function WinEventHookOne(evt As SWEH_Events, weDelegate As WinEventDelegate, idProcess As UInteger, idThread As UInteger) As IntPtr
Return SetWinEventHook(evt, evt, IntPtr.Zero, weDelegate, idProcess, idThread, WinEventHookInternalFlags)
End Function
Friend Shared Function GetWindowThread(hWnd As IntPtr, ByRef processID As UInteger) As UInteger
processID = 0
Return GetWindowThreadProcessId(hWnd, processID)
End Function
' SetWinEventHook Events
Friend Enum SWEH_Events As UInteger
EVENT_MIN = &H1
EVENT_MAX = &H7FFFFFFF
EVENT_SYSTEM_SOUND = &H1
EVENT_SYSTEM_ALERT = &H2
EVENT_SYSTEM_FOREGROUND = &H3
EVENT_SYSTEM_MENUSTART = &H4
EVENT_SYSTEM_MENUEND = &H5
EVENT_SYSTEM_MENUPOPUPSTART = &H6
EVENT_SYSTEM_MENUPOPUPEND = &H7
EVENT_SYSTEM_CAPTURESTART = &H8
EVENT_SYSTEM_CAPTUREEND = &H9
EVENT_SYSTEM_MOVESIZESTART = &HA
EVENT_SYSTEM_MOVESIZEEND = &HB
EVENT_SYSTEM_CONTEXTHELPSTART = &HC
EVENT_SYSTEM_CONTEXTHELPEND = &HD
EVENT_SYSTEM_DRAGDROPSTART = &HE
EVENT_SYSTEM_DRAGDROPEND = &HF
EVENT_SYSTEM_DIALOGSTART = &H10
EVENT_SYSTEM_DIALOGEND = &H11
EVENT_SYSTEM_SCROLLINGSTART = &H12
EVENT_SYSTEM_SCROLLINGEND = &H13
EVENT_SYSTEM_SWITCHSTART = &H14
EVENT_SYSTEM_SWITCHEND = &H15
EVENT_SYSTEM_MINIMIZESTART = &H16
EVENT_SYSTEM_MINIMIZEEND = &H17
EVENT_SYSTEM_DESKTOPSWITCH = &H20
EVENT_SYSTEM_END = &HFF
EVENT_OEM_DEFINED_START = &H101
EVENT_OEM_DEFINED_END = &H1FF
EVENT_UIA_EVENTID_START = &H4E00
EVENT_UIA_EVENTID_END = &H4EFF
EVENT_UIA_PROPID_START = &H7500
EVENT_UIA_PROPID_END = &H75FF
EVENT_CONSOLE_CARET = &H4001
EVENT_CONSOLE_UPDATE_REGION = &H4002
EVENT_CONSOLE_UPDATE_SIMPLE = &H4003
EVENT_CONSOLE_UPDATE_SCROLL = &H4004
EVENT_CONSOLE_LAYOUT = &H4005
EVENT_CONSOLE_START_APPLICATION = &H4006
EVENT_CONSOLE_END_APPLICATION = &H4007
EVENT_CONSOLE_END = &H40FF
EVENT_OBJECT_CREATE = &H8000
EVENT_OBJECT_DESTROY = &H8001
EVENT_OBJECT_SHOW = &H8002
EVENT_OBJECT_HIDE = &H8003
EVENT_OBJECT_REORDER = &H8004
EVENT_OBJECT_FOCUS = &H8005
EVENT_OBJECT_SELECTION = &H8006
EVENT_OBJECT_SELECTIONADD = &H8007
EVENT_OBJECT_SELECTIONREMOVE = &H8008
EVENT_OBJECT_SELECTIONWITHIN = &H8009
EVENT_OBJECT_STATECHANGE = &H800A
EVENT_OBJECT_LOCATIONCHANGE = &H800B
EVENT_OBJECT_NAMECHANGE = &H800C
EVENT_OBJECT_DESCRIPTIONCHANGE = &H800D
EVENT_OBJECT_VALUECHANGE = &H800E
EVENT_OBJECT_PARENTCHANGE = &H800F
EVENT_OBJECT_HELPCHANGE = &H8010
EVENT_OBJECT_DEFACTIONCHANGE = &H8011
EVENT_OBJECT_ACCELERATORCHANGE = &H8012
EVENT_OBJECT_INVOKED = &H8013
EVENT_OBJECT_TEXTSELECTIONCHANGED = &H8014
EVENT_OBJECT_CONTENTSCROLLED = &H8015
EVENT_SYSTEM_ARRANGMENTPREVIEW = &H8016
EVENT_OBJECT_END = &H80FF
EVENT_AIA_START = &HA000
EVENT_AIA_END = &HAFFF
End Enum
' SetWinEventHook Window Objects
Friend Enum SWEH_ObjectId As Long
OBJID_WINDOW = &H0
OBJID_SYSMENU = &HFFFFFFFFUI
OBJID_TITLEBAR = &HFFFFFFFEUI
OBJID_MENU = &HFFFFFFFDUI
OBJID_CLIENT = &HFFFFFFFCUI
OBJID_VSCROLL = &HFFFFFFFBUI
OBJID_HSCROLL = &HFFFFFFFAUI
OBJID_SIZEGRIP = &HFFFFFFF9UI
OBJID_CARET = &HFFFFFFF8UI
OBJID_CURSOR = &HFFFFFFF7UI
OBJID_ALERT = &HFFFFFFF6UI
OBJID_SOUND = &HFFFFFFF5UI
OBJID_QUERYCLASSNAMEIDX = &HFFFFFFF4UI
OBJID_NATIVEOM = &HFFFFFFF0UI
End Enum
' WinEventDelegate flags
Friend Enum SWEH_dwFlags As UInteger
WINEVENT_OUTOFCONTEXT = &H0 ' Events are ASYNC - No dll needed
WINEVENT_SKIPOWNTHREAD = &H1 ' Don't call back for events on installer's thread
WINEVENT_SKIPOWNPROCESS = &H2 ' Don't call back for events on installer's process
WINEVENT_INCONTEXT = &H4 ' Events are SYNC, this causes your dll to be injected into every process
End Enum
' SetWindowPos flags
<Flags>
Public Enum SWP_Flags As UInteger
SWP_NOSIZE = &H1
SWP_NOMOVE = &H2
SWP_NOZORDER = &H4
SWP_NOREDRAW = &H8
SWP_NOACTIVATE = &H10
SWP_DRAWFRAME = &H20
SWP_FRAMECHANGED = &H20
SWP_SHOWWINDOW = &H40
SWP_HIDEWINDOW = &H80
SWP_NOCOPYBITS = &H100
SWP_NOOWNERZORDER = &H200
SWP_NOREPOSITION = &H200
SWP_NOSENDCHANGING = &H400
SWP_NOCLIENTSIZE = &H800
SWP_NOCLIENTMOVE = &H1000
SWP_DEFERERASE = &H2000
SWP_ASYNCWINDOWPOS = &H4000
End Enum
End Class
Thanks to the great help of Jimi, I was able to get it work:
Add the NativeMethods class to the project, use the code from Jimi's answer above
Use the following code in the form:
Imports System.Runtime.InteropServices
Imports WindowsApp1.NativeMethods
Public Class Form1
Private ReadOnly ForegrundChangedEventDelegate As WinEventDelegate
Private Shared GCForegroundStateSafetyHandle As GCHandle
Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim proc As Process
Dim ProcId As Integer = 0
proc = Process.Start("notepad.exe")
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
SendMessage(proc.MainWindowHandle, 274, 61488, 0)
GetWindowThreadProcessId(proc.MainWindowHandle, ProcId)
SetForegroundStateChangedHook(proc.MainWindowHandle)
End Sub
Public Sub New()
InitializeComponent()
ForegrundChangedEventDelegate = New WinEventDelegate(AddressOf ForegroundStateChangedCallback)
GCForegroundStateSafetyHandle = GCHandle.Alloc(ForegrundChangedEventDelegate)
End Sub
Protected Overrides Sub OnHandleDestroyed(e As EventArgs)
GCForegroundStateSafetyHandle.Free()
UnhookWinEvent(hForegrundChangedEventHook)
MyBase.OnHandleDestroyed(e)
End Sub
Private Sub SetForegroundStateChangedHook(hWnd As IntPtr)
Dim processId As UInteger
Dim targetThreadId = GetWindowThread(hWnd, processId)
hForegrundChangedEventHook = WinEventHookOne(SWEH_Events.EVENT_SYSTEM_FOREGROUND, ForegrundChangedEventDelegate, processId, targetThreadId)
End Sub
Friend Sub ForegroundStateChangedCallback(hWinEventHook As IntPtr, eventType As SWEH_Events, hWnd As IntPtr, idObject As SWEH_ObjectId, idChild As Long, dwEventThread As UInteger, dwmsEventTime As UInteger)
If idObject = SWEH_ObjectId.OBJID_WINDOW AndAlso eventType = SWEH_Events.EVENT_SYSTEM_FOREGROUND Then
Dim flags = SWP_Flags.SWP_ASYNCWINDOWPOS Or SWP_Flags.SWP_NOACTIVATE Or SWP_Flags.SWP_NOSIZE Or SWP_Flags.SWP_NOMOVE
SetWindowPos(Handle, IntPtr.Zero, 0, 0, 0, 0, flags)
End If
End Sub
End Class
I am trying to develop a keystroke macro program using vb.net which will record keystrokes like keydown and keyup events and then play it anywhere while the main program is running in the background. As of now I have successfully captured the keystrokes and stored those strokes. But the problem which I m facing, is at the time of playing those stored keystrokes. I can't fire the KeyDown and KeyUp events from any external program. I have tried SendKeys method as well but it cannot differentiate between KeyDown and KeyUp separately. Help in this scenario will highly be appreciated.
The KeyDown event which is accessible in parent program only.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'MessageBox.Show(e.KeyCode)
'bla bla bla
End Sub
'Use of SendKeys but it cannot distinguish between KeyDown and KeyUp
Private Function AutoSendKey(ByVal keystroke As String, ByVal delay As Integer)
System.Threading.Thread.Sleep(delay)
My.Computer.Keyboard.SendKeys(keystroke, True)
End Function
I need an approach to trigger KeyDown and KeyUp events from external applications. Thanks in advance
I just was looking for the same thing and found few resources and then managed to make it into one application:
Basically this will work unless you use some sort of intercept on a driver level, in which case the keyboard that is intercepted by the intercept application wont be visible to windows and thus this script is useless.
Other than that, here is my code.
This code is optimized for .NET 5.0
First create KeyboardHook.vb class:
Imports System.Runtime.InteropServices
Public Class KeyboardHook
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Private Overloads Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal HookProc As KBDLLHookProc, ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Private Overloads Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Private Overloads Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
End Function
<StructLayout(LayoutKind.Sequential)>
Private Structure KBDLLHOOKSTRUCT
Public vkCode As UInt32
Public scanCode As UInt32
Public flags As KBDLLHOOKSTRUCTFlags
Public time As UInt32
Public dwExtraInfo As UIntPtr
End Structure
<Flags()>
Private Enum KBDLLHOOKSTRUCTFlags As UInt32
LLKHF_EXTENDED = &H1
LLKHF_INJECTED = &H10
LLKHF_ALTDOWN = &H20
LLKHF_UP = &H80
End Enum
Public Shared Event KeyDown(ByVal Key As Key)
Public Shared Event KeyUp(ByVal Key As Key)
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const HC_ACTION As Integer = 0
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_SYSKEYUP = &H105
Private Delegate Function KBDLLHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
Private KBDLLHookProcDelegate As KBDLLHookProc = New KBDLLHookProc(AddressOf KeyboardProc)
Private HHookID As IntPtr = IntPtr.Zero
Private Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
If (nCode = HC_ACTION) Then
Dim struct As KBDLLHOOKSTRUCT
Select Case wParam
Case WM_KEYDOWN, WM_SYSKEYDOWN
Dim aKey = KeyInterop.KeyFromVirtualKey(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode)
RaiseEvent KeyDown(aKey)
Case WM_KEYUP, WM_SYSKEYUP
Dim aKey = KeyInterop.KeyFromVirtualKey(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode)
RaiseEvent KeyUp(aKey)
End Select
End If
Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
End Function
Public Sub New()
HHookID = SetWindowsHookEx(WH_KEYBOARD_LL, KBDLLHookProcDelegate, System.Diagnostics.Process.GetCurrentProcess().MainModule.BaseAddress, 0)
If HHookID = IntPtr.Zero Then
Throw New Exception("Could not set keyboard hook")
End If
End Sub
Protected Overrides Sub Finalize()
If Not HHookID = IntPtr.Zero Then
UnhookWindowsHookEx(HHookID)
End If
MyBase.Finalize()
End Sub
End Class
Now create your own class/usercontrol with KeyboardHook event handlers.
For me this was simple OnScreenKBView.xaml with textblocks that had text property bound to a viewmodel class properties, which shows what keys are pressed down and released up and how many keys are pressed at the same time:
<Grid>
<Border Background="Red" >
<StackPanel Orientation="Vertical">
<TextBlock Text="KeyDown:"/>
<TextBlock Text="{Binding keyDown, Converter={StaticResource KeyToStringVC}}"/>
<TextBlock Text="KeyUp:"/>
<TextBlock Text="{Binding keyUp, Converter={StaticResource KeyToStringVC}}"/>
<TextBlock Text="TotalKeysPressed at the same time:"/>
<TextBlock Text="{Binding keysPressed, Converter={StaticResource ListToCountVC}}"/>
<TextBlock Text="KeysPressed:"/>
<TextBlock Text="{Binding keysPressed, Converter={StaticResource ListToStringVC}}" TextWrapping="Wrap"/>
</StackPanel>
</Border>
</Grid>
Notice I have few value converters KeyToStringVC, ListToStringVC and ListToCountVC.
KeyToStringVC:
Return value.ToString
ListToCountVC:
If Not value Is Nothing Then
Return value.count
Else
Return Nothing
End If
ListToStringVC :
Dim kl As ObservableCollection(Of Key) = value
Dim str As String = Nothing
If Not kl Is Nothing Then
For Each item In kl
str += item.ToString & "; "
Next
End If
Return str
You will have to look up how to use/create value converters if you dont know how.
VB code behind the OnScreenKBView.xaml:
Imports System.Collections.ObjectModel
Public Class OnScreenKBView
Private WithEvents kbHook As New KeyboardHook
Private viewModel As OnScreenKBViewModel
Private Sub kbHook_KeyDown(ByVal Key As Key) Handles kbHook.KeyDown
viewModel.keyDown = Key
'check if list already has the key in it
Dim hasKey As Boolean = False
If Me.viewModel.keysPressed.Contains(Key) Then
hasKey = True
End If
If Not hasKey Then
Me.viewModel.keysPressed.Add(Key)
Dim localCol = Me.viewModel.keysPressed
Dim newCol = New ObservableCollection(Of Key)(From i In localCol Select i)
Me.viewModel.keysPressed = newCol
End If
End Sub
Private Sub kbHook_KeyUp(ByVal Key As Key) Handles kbHook.KeyUp
viewModel.keyUp = Key
Me.viewModel.keysPressed.Remove(Key)
Dim localCol = Me.viewModel.keysPressed
Dim newCol = New ObservableCollection(Of Key)(From i In localCol Select i)
Me.viewModel.keysPressed = newCol
End Sub
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.viewModel = Application.Current.MainWindow.DataContext
End Sub
End Class
This should give you some ideas i hope. If ya need whole project i have it on github but its private, i could zip it for ya.
Lil demo:
GIF here
Imports System.Runtime.InteropServices
Imports System.Windows.Forms 'for the keys. enumeration
Public Module SendWinKey
Const KEYEVENTF_KEYDOWN As Integer = &H0
Const KEYEVENTF_KEYUP As Integer = &H2
Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)
Public Sub Main()
keybd_event(CByte(Keys.LWin), 0, KEYEVENTF_KEYDOWN, 0) 'press the left Win key down
keybd_event(CByte(Keys.R), 0, KEYEVENTF_KEYDOWN, 0) 'press the R key down
keybd_event(CByte(Keys.R), 0, KEYEVENTF_KEYUP, 0) 'release the R key
keybd_event(CByte(Keys.LWin), 0, KEYEVENTF_KEYUP, 0) 'release the left Win key
End Sub
End Module
As you can see it is really simple.
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
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#
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