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 just learning programming, so please be understanding :)
I am writing an application that needs to send text data to another console program. I was successful with SendMessage(), but unfortunately with Wine (on linux) it is not supported for console applications.
So I found a function that is supported. This is WriteConsoleInput().
Here is my code, could someone indicate why it doesn't work?
Public Declare Function AttachConsole Lib "kernel32" (ByVal ProcessID As Long) As Boolean
Private Declare Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Int32) As IntPtr
Declare Function WriteConsoleInputW Lib "kernel32" Alias "WriteConsoleInputW" (ByVal hConsoleInput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByRef lpNumberOfCharsWritten As Integer) As Integer
Declare Function WriteConsoleInputA Lib "kernel32" Alias "WriteConsoleInputA" (ByVal hConsoleInput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByRef lpNumberOfCharsWritten As Integer) As Integer
Declare Function SetConsoleTitle Lib "kernel32.dll" Alias "SetConsoleTitleA" (lpConsoleTitle As String) As Boolean
Private Const STD_OUTPUT_HANDLE As Long = -11&
Private Const STD_INPUT_HANDLE As Long = -10&
Private Const STD_ERROR_HANDLE As Long = -12&
Private Const INVALID_HANDLE_VALUE As Long = -1&
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim titleStr As String = "Test"
Dim textToWrite As Char() = CType("test", Char())
Dim stdIN As Integer
Dim stdOUT As Integer
Dim charwritten As Integer = 0
Dim ret As Integer
If ReturnCMDProcessID() = 0 Then
MessageBox.Show("CMD off")
Exit Sub
End If
AttachConsole(ReturnCMDProcessID())
stdIN = CInt(GetStdHandle(CInt(STD_INPUT_HANDLE)))
stdOUT = CInt(GetStdHandle(CInt(STD_OUTPUT_HANDLE)))
ret = WriteConsoleInputA(stdIN, textToWrite, textToWrite.Length, charwritten)
SetConsoleTitle(titleStr)
End Sub
Did you try the "built-in" way to do this?
System.Console:
https://learn.microsoft.com/en-us/dotnet/api/system.console?view=netframework-4.8
And in particular Console.In
https://learn.microsoft.com/en-us/dotnet/api/system.console.in?view=netframework-4.8
And Console.Out
https://learn.microsoft.com/en-us/dotnet/api/system.console.out?view=netframework-4.8
I also just noticed you are setting the title, that is as simple as Console.Title = "My Title"
I am trying to find out if a process is not frozen by sending a message to its handler and see if it reaches it.
I cannot use Process.Responding because it looks like the monitored application needs to have a window. This has to work for processes that run without a window. I use this method but it always returns FALSE:
Const SMTO_BLOCK = &H1
Const SMTO_ABORTIFHUNG = &H2
Const WM_NULL = &H0
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Function SendMessageTimeout(hWnd As HandleRef, msg As Integer, wParam As IntPtr, lParam As IntPtr, flags As Integer, timeout As Integer, ByRef pdwResult As IntPtr) As IntPtr
End Function
Public Function ProcessIsResponding(ByVal proc As Process) As Boolean
Dim handleRef As HandleRef = New HandleRef(proc, proc.Handle)
Dim timeout As Integer = 10000
Dim lpdwResult As IntPtr
Dim lResult As IntPtr = SendMessageTimeout(handleRef, WM_NULL, IntPtr.Zero, IntPtr.Zero, SMTO_ABORTIFHUNG And SMTO_BLOCK, timeout, lpdwResult)
Return lResult <> IntPtr.Zero
End Function
This works if I replace proc.Handle by proc.MainWindowHandle but I guess in this case a window handle is needed. What happens if there's no window? What other choice do I have?
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
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.