Reading keydown regardless of active application in vb.net - vb.net

I asked a question earlier about keyhooks in vb.net.
My current issue is such:
I have created a program which should perform a certain action whenever a certain group of keys is pressed at the same time. The program must be able to run in the background, or in the system tray or something. Essentially, this should work like the KeyDown event on a form, except the form in this case is everything.
I'm not certain if there is a way to do this directly from within the .net API, but if there is I certainly have not found it.

That doesn't require a keyboard hook, you'll want to register a hot key. Much easier to implement and much less demanding of system resources. Here's an example, it restores the form to the foreground if it was minimized. Note that you can register more than one key:
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Public Class Form1
Private Const cHotKeyId As Integer = 0
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
'--- Register Ctrl + Shift + U as a hot key
If Not RegisterHotKey(Me.Handle, cHotKeyId, MOD_CONTROL + MOD_SHIFT, Keys.U) Then
Throw New Win32Exception()
End If
MyBase.OnLoad(e)
End Sub
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
UnregisterHotKey(Me.Handle, cHotKeyId)
MyBase.OnFormClosing(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Console.WriteLine(m.ToString())
If (m.Msg = WM_HOTKEY AndAlso m.WParam = CType(cHotKeyId, IntPtr)) Then
Me.Visible = True
If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
SetForegroundWindow(Me.Handle)
End If
MyBase.WndProc(m)
End Sub
'--- P/Invoke declarations
Private Const WM_HOTKEY As Integer = &H312
Private Const MOD_ALT As Integer = &H1
Private Const MOD_CONTROL As Integer = &H2
Private Const MOD_SHIFT As Integer = &H4
Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Boolean
Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
End Class

Related

How to move a Form to the foreground when clicking a Window of another app parented to a Panel

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

How to trigger keydown and keyup events from external applications in VB.NET? avoid sendkeys method

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.

Switch focus to a vb.net application on key press

I have a vb.net application which will be minimized to task bar or notification area. Is there any way for me to maximize/focus on that application when user presses a key (even if the user is using any other application).
Tried windows hot key but doesn't focus the app when it is already open.Please help
You require global hotkeys. First, add these functions to your application.
Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Integer
End Function
Next, add a Timer and in its Timer.Tick Event, use this function like this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If GetKeyPress(Keys.LControlKey) And GetKeyPress(Keys.A) Then
Dim Handle As IntPtr = Process.GetProcessById(2916).MainWindowHandle
ShowWindow(Handle, 9)
SetForegroundWindow(Handle)
End If
End Sub
Set the Timer Interval to 150 to avoid repeated keypresses and make sure to enable the timer in the Form Load event.

keyboard shortcut to show hidden form

I'm creating a program to manage a local network but Im having a little problem.
When the form starts, it starts hidden.
I used this code to hide it:
Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
If Not Me.IsHandleCreated Then
Me.CreateHandle()
value = False
End If
MyBase.SetVisibleCore(value)
End Sub
What I want exactly is to show the form when I click ALT+X for example.
Use a Global Hotkey. That way you don't have to worry about your Application having focus. A good example for VB.Net is here:
http://www.kirsbo.com/How_to_add_global_hotkeys_to_applications_in_VB.NET
To summarise. Define a HotKey class:
Public Class Hotkey
#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
''' <summary>
''' Declaration of winAPI function wrappers. The winAPI functions are used to register / unregister a hotkey
''' </summary>
Public Declare Function RegisterHotKey Lib "user32" _
(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Enum KeyModifier
None = 0
Alt = &H1
Control = &H2
Shift = &H4
Winkey = &H8
End Enum 'This enum is just to make it easier to call the registerHotKey function: The modifier integer codes are replaced by a friendly "Alt","Shift" etc.
#End Region
#Region "Hotkey registration, unregistration and handling"
Public Shared Sub registerHotkey(ByRef sourceForm As Form, ByVal triggerKey As String, ByVal modifier As KeyModifier)
RegisterHotKey(sourceForm.Handle, 1, modifier, Asc(triggerKey.ToUpper))
End Sub
Public Shared Sub unregisterHotkeys(ByRef sourceForm As Form)
UnregisterHotKey(sourceForm.Handle, 1) 'Remember to call unregisterHotkeys() when closing your application.
End Sub
Public Shared Sub handleHotKeyEvent(ByVal hotkeyID As IntPtr)
MsgBox("The hotkey was pressed")
End Sub
#End Region
End Class
Then add the following Sub to your main form:
'System wide hotkey event handling
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = Hotkey.WM_HOTKEY Then
Hotkey.handleHotKeyEvent(m.WParam)
End If
MyBase.WndProc(m)
End Sub
Then register a HotKey in the startup code for your form:
Hotkey.RegisterHotKey(Me, "X", Hotkey.KeyModifier.Alt)
The first parameter is the form, the second is the key to be handled and the third is the modifier key. Once this Hotkey is registered it will trigger code in HotKey.handleHotKeyEvent.
You can then use some sort of Callback to trigger a method in the calling form itself if you so desire.

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.