-
Nov 7th, 2012, 04:36 PM
#1
Thread Starter
Lively Member
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
-
Nov 7th, 2012, 05:12 PM
#2
Re: Send Message: KeyDown, KeyUp to an Application?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Nov 7th, 2012, 05:37 PM
#3
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)
-
Nov 7th, 2012, 05:53 PM
#4
Thread Starter
Lively Member
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")
Last edited by VBbbq; Nov 7th, 2012 at 06:13 PM.
-
Nov 7th, 2012, 07:35 PM
#5
Re: Send Message: KeyDown, KeyUp to an Application?
SendKeys.Send(Keys.Down) sends the number 40 :X
Yes it does. Forgot the conversion. It's been one of those days!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|