Click to See Complete Forum and Search --> : Sending keyboard commands to another application
Merlyn27
Oct 7th, 2000, 01:32 AM
How can I send keyboard commands to another application?
I'm able to find and close a window using FindWindows and SendMessage but I haven't been able to send keys to the app.
An example would be that I was to send the keys 2 + 2 = when I press a command button and then calc show the answer...
Thanks!
James
Vlatko
Oct 7th, 2000, 02:56 PM
First you have to activate Calc using the ShellExecute API then you can make sure that the window is on top(has focus) use the setwindowpos API.
Now you can use the SendKeys method or keybd_event API.
You should make a pause between sending two keys using the Sleep API function. I don't have time to write the code right now but it is easy.Try it.
gwdash
Oct 7th, 2000, 03:07 PM
You can also use SendMessage, but i don't know the message. I'll look around for it and post
Merlyn27
Oct 7th, 2000, 06:48 PM
Sounds good. I'll look into it later when I get a chance.
Thanks
You could send the WM_KEYDOWN message but in this case, it's much easier to use the SendKeys function.
AppActivate "Calculator"
SendKeys "2+2="
Merlyn27
Oct 8th, 2000, 06:35 PM
Thanks, that worked great. It looks like the sendkeys command has problems with sending some character like the plus sign. Sendkeys "2+2" sends it as 22. That won't really be a problem with what I'm looking to use it for though.
Strange. It returns 4 for me...What code are you using to send it?
Merlyn27
Oct 9th, 2000, 02:14 PM
Do you have anything else defined in the program? All I have in the program is a command button with:Private Sub
Private Command1_Click()
AppActivate ("Calculator")
SendKeys "2+2="
End Sub
This displays the answer of:
1.4142135623730950488016887242097
Sending just "2+2" displays 22
Sending "2+2" and then sending vbkeyreturn displays 2213
Sending "2+2" and then sending "{ENTER}" displays the same long answer as "2+2=" does...
Weird...
enigmaICE
Oct 17th, 2000, 03:20 AM
Const WM_SETTEXT = &HC
SendMessage Handle, WM_SETTEXT, 0, ByVal Form1.Text1.Text
Try this. It's not much of a different but it might work.
AppActivate "Calculator"
SendKeys "2+2=", True
OR this.
AppActivate "Calculator"
SendKeys "2", True
SendKeys "+", True
SendKeys "2", True
SendKeys "=", True
bonjour
Oct 18th, 2000, 11:02 PM
so what is it that SendKeys does?
Tell me if I'm right here...
You make an app that records the keyboard events from another app?
So, like if my kid is typing a letter using notepad, I can have an app running in the background recording what he's typing?
Sendkeys can send keyboard strokes to other applications.
Open Notepad and put this in a Command Button and click it.
Private Sub Command1_Click()
AppActivate "Untitled - Notepad"
SendKeys "Hello"
SendKeys "{ENTER}"
SendKeys "Welcome to the SendKeys World."
SendKeys "{ENTER}"
SendKeys "Enjoy your stay."
End Sub
To retrieve keystrokes, use GetAsyncKeyState.
Add the following to a Form with a TextBox and Timer.
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 50
End Sub
Private Sub Timer1_Timer()
For I = 13 To 255
If GetAsyncKeyState(I) Then Text1 = Text1 & Chr(I)
Next I
Text1.SelStart = Len(Text1)
End Sub
EdwardMillen
Oct 19th, 2000, 08:54 PM
+ in sendkeys means shift, so to send + you have to put: {+}. You can also use ~ instead of {ENTER}, its easier.
AppActivate "Calculator"
SendKeys "2{+}2~"
Merlyn27
Oct 19th, 2000, 09:33 PM
Yeah I saw in the help files that + meant shift so I was wondering how to send it since there is no vbkey constant for the plus key...
Dude Man Vox
Oct 23rd, 2000, 11:08 PM
So using send keys, can you simulate a mouse click event?
No, you must use PostMessage or mouse_event to click the mouse.
Here is an example. When you press Command1, it will 'click' Command2.
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const MK_LBUTTON = &H1
Private Sub Command1_Click()
PostMessage Command2.hwnd, WM_LBUTTONDOWN, MK_LBUTTON, 0
PostMessage Command2.hwnd, WM_LBUTTONUP, MK_LBUTTON, 0
End Sub
Dude Man Vox
Oct 25th, 2000, 11:04 PM
Is it possible to cause a click without having to know a window handle?
Can I just creat one on the desktop?
Dude Man Vox
Oct 26th, 2000, 11:12 PM
Could yo utype an example of using mouse evnt to click at coords (0,0)?
Sorry if this gets annoying, but I am new to the whole API concept.
Thanks for all the help so far.
kedaman
Oct 27th, 2000, 06:01 AM
'declarations for a module
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Sub MouseDown(Optional Left As Boolean = True, Optional Right As Boolean, Optional Middle As Boolean, Optional Click As Boolean)
mouse_event -Left * 2 - Right * 8 - Middle * 32, 0&, 0&, 0&, 0&
If Click Then mouse_event -Left * 4 - Right * 16 - Middle * 64, 0&, 0&, 0&, 0&
End Sub
Sub MouseUp(Optional Left As Boolean = True, Optional Right As Boolean, Optional Middle As Boolean)
mouse_event -Left * 4 - Right * 16 - Middle * 64, 0&, 0&, 0&, 0&
End Sub
'Example of how To use:
SetCursorPos 10, 20 'move cursor to 10,20
MouseDown True, False, False, True 'left click there
jsallen
Oct 29th, 2000, 03:43 PM
I tried to use sendkeys to automate a group of keystrokes that need to be made precisely. It involves activating a window pressing the "+" to bring up a dialog box. Once the dialog box appears I "shift-tab and enter some text then I tab over and enter a number and tab again enter a number and then press ENTER. After hitting enter the focus normally returns to the window that I first activated. I then repeat the process over only I begin by pressing "-". It works sometimes. When it doesn't work I assumed I lost focus of the window. I also employed the "doevent" to take care of the delay that occurs after hitting enter.
I was wondering how to use sendmessage to do the same task. I have already used Spyxx to find the handles of each window and textboxs in question. But I do not know how to use Sendmessage or any variant of it.
Any help would be appreciated.
Thanks..
agent
Oct 29th, 2000, 06:17 PM
I've been using this method for a while to send text to an AOL chatroom:
Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Public Const WM_SETTEXT = &HC
Public Const WM_CHAR = &H102
SendMessageByString hwnd, WM_SETTEXT, 0, "My text here"
SendMessageByNum hwnd, WM_CHAR, 13, 0
the wParam on SendMessageByNum is the ascii code of the char to send.
jsallen
Oct 29th, 2000, 06:29 PM
Thanks. How do I specify the hwnd? I have window handle numbers for each item I want to send text or numbers to but I don't know how to direct them there.
SendMessageByNum hwnd, WM_CHAR, 13, 0
Are you sending the number 13 ?
gwdash
Oct 30th, 2000, 03:13 PM
1) Hun? Just put them in the hWnd Parameter of SendMessage
2) No, The Ascii Code, like Asc("a"). It's the opposite of Chr()
jeroenh
Nov 2nd, 2000, 02:22 AM
Well I must say that this tread is beginning to be very interesting.
When I use sendkeys to send something to the GroupWise mailer, I get the error "Object does not support function."
But I would like to know how to use the SendMessage API to send an application the TAB key, without returning a tab space, but goes to the next field.
Or is there a way to send a code like "<ALT> A".
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.