Send Message: KeyDown, KeyUp to an Application?
Been searching for awhile on how to do this...
Question:
How do you make Visual Basic (2010) Send Message: KeyDown, KeyUp to an Application?
Note: SendKeys.Send("{Down}") etc. won't work.
Example Script: (This Works, But Only Does Key Press Events)
Code:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Try
AppActivate(1612) ' App ID
SendKeys.Send("{" & C1.Text & "}") ' Keys To Send
Catch ' Error Handling
MsgBox(ErrorToString) ' Error Message If Any
End Try
End Sub
Any Help Would Be Greatly Appreciated, Thanks :)
Re: Send Message: KeyDown, KeyUp to an Application?
Re: Send Message: KeyDown, KeyUp to an Application?
You can use the window handle of the control (not the one of the application, nor the process handle). Some controls also have an ID that does not change between restarts so it is possible to use that instead whenever possible.
The constants of the different keys are in winuser.h
vb.net Code:
Private Declare Auto Function SendMessageKeyUpDown Lib "user32" Alias "SendMessage" _
(ByVal hwnd As IntPtr, ByVal wMsg As Integer, _
ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101
'this will send a down arrow
Const VK_DOWN As Integer = &H28
'for first time experiments try to get the handle manually, using C++ or some clone thereof
Dim ControlWindowHandle As IntPtr = CType(TheHandleAsInteger, IntPtr)
'The actual messages
SendMessageKeyUpDown(ControlWindowHandle, WM_KEYDOWN, VK_DOWN, IntPtr.Zero)
SendMessageKeyUpDown(ControlWindowHandle, WM_KEYUP, VK_DOWN, IntPtr.Zero)
Re: Send Message: KeyDown, KeyUp to an Application?
Thanks Half, Looks Like What I'm Looking For. :) +rep
Dunfiddin: SendKeys.Send(Keys.Down) sends the number 40 :X
Windows Handle Isnt To Hard In VB:
Code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function
Dim topWindow As IntPtr = FindWindow("classname", "windowcaption")
Dim childWindow As IntPtr = FindWindowEx(topWindow, New IntPtr(0), "classname", "windowcaption")
Re: Send Message: KeyDown, KeyUp to an Application?
Quote:
SendKeys.Send(Keys.Down) sends the number 40 :X
Yes it does. Forgot the conversion. It's been one of those days!