|
-
Oct 7th, 2000, 01:32 AM
#1
Thread Starter
Junior Member
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
-
Oct 7th, 2000, 02:56 PM
#2
Frenzied Member
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.
-
Oct 7th, 2000, 03:07 PM
#3
Fanatic Member
You can also use SendMessage, but i don't know the message. I'll look around for it and post
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Oct 7th, 2000, 06:48 PM
#4
Thread Starter
Junior Member
Sounds good. I'll look into it later when I get a chance.
Thanks
-
Oct 8th, 2000, 05:45 PM
#5
You could send the WM_KEYDOWN message but in this case, it's much easier to use the SendKeys function.
Code:
AppActivate "Calculator"
SendKeys "2+2="
-
Oct 8th, 2000, 06:35 PM
#6
Thread Starter
Junior Member
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.
-
Oct 9th, 2000, 01:43 PM
#7
Strange. It returns 4 for me...What code are you using to send it?
-
Oct 9th, 2000, 02:14 PM
#8
Thread Starter
Junior Member
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...
-
Oct 17th, 2000, 03:20 AM
#9
New Member
Const WM_SETTEXT = &HC
SendMessage Handle, WM_SETTEXT, 0, ByVal Form1.Text1.Text
-
Oct 17th, 2000, 02:32 PM
#10
Try this. It's not much of a different but it might work.
Code:
AppActivate "Calculator"
SendKeys "2+2=", True
OR this.
Code:
AppActivate "Calculator"
SendKeys "2", True
SendKeys "+", True
SendKeys "2", True
SendKeys "=", True
-
Oct 18th, 2000, 11:02 PM
#11
Member
what??
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?
-
Oct 19th, 2000, 06:20 AM
#12
Sendkeys can send keyboard strokes to other applications.
Open Notepad and put this in a Command Button and click it.
Code:
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
-
Oct 19th, 2000, 02:39 PM
#13
To retrieve keystrokes, use GetAsyncKeyState.
Add the following to a Form with a TextBox and Timer.
Code:
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
-
Oct 19th, 2000, 08:54 PM
#14
New Member
I know whats wrong...
+ in sendkeys means shift, so to send + you have to put: {+}. You can also use ~ instead of {ENTER}, its easier.
Code:
AppActivate "Calculator"
SendKeys "2{+}2~"
-
Oct 19th, 2000, 09:33 PM
#15
Thread Starter
Junior Member
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...
-
Oct 23rd, 2000, 11:08 PM
#16
New Member
snedKeys
So using send keys, can you simulate a mouse click event?
-
Oct 24th, 2000, 03:12 PM
#17
No, you must use PostMessage or mouse_event to click the mouse.
-
Oct 24th, 2000, 03:15 PM
#18
Here is an example. When you press Command1, it will 'click' Command2.
Code:
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
-
Oct 25th, 2000, 11:04 PM
#19
New Member
PostMessage
Is it possible to cause a click without having to know a window handle?
Can I just creat one on the desktop?
-
Oct 26th, 2000, 11:12 PM
#20
New Member
mouse_event
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.
-
Oct 27th, 2000, 06:01 AM
#21
transcendental analytic
Code:
'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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 29th, 2000, 04:43 PM
#22
New Member
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..
-
Oct 29th, 2000, 07:17 PM
#23
Fanatic Member
I've been using this method for a while to send text to an AOL chatroom:
Code:
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
Code:
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.
-
Oct 29th, 2000, 07:29 PM
#24
New Member
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 ?
-
Oct 30th, 2000, 04:13 PM
#25
Fanatic Member
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()
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Nov 2nd, 2000, 03:22 AM
#26
Addicted Member
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".
Catch you later,
Jeroen Hoekemeijer
Code:
If 1 = 2 Then MajorError
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
|