Results 1 to 26 of 26

Thread: Sending keyboard commands to another application

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    25

    Exclamation

    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

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    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]

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    25
    Sounds good. I'll look into it later when I get a chance.

    Thanks

  5. #5
    Guest
    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="

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    25
    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.

  7. #7
    Guest
    Strange. It returns 4 for me...What code are you using to send it?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    25
    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...

  9. #9
    New Member
    Join Date
    Oct 2000
    Location
    Chicago
    Posts
    6

    Const WM_SETTEXT = &HC
    SendMessage Handle, WM_SETTEXT, 0, ByVal Form1.Text1.Text

  10. #10
    Guest
    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

  11. #11
    Member
    Join Date
    Jul 2000
    Posts
    37

    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?

  12. #12
    Guest
    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

  13. #13
    Guest
    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

  14. #14
    New Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    6

    Smile 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~"

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Posts
    25
    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...

  16. #16
    New Member
    Join Date
    Oct 2000
    Posts
    3

    Question snedKeys

    So using send keys, can you simulate a mouse click event?

  17. #17
    Guest
    No, you must use PostMessage or mouse_event to click the mouse.

  18. #18
    Guest
    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

  19. #19
    New Member
    Join Date
    Oct 2000
    Posts
    3

    Question PostMessage

    Is it possible to cause a click without having to know a window handle?
    Can I just creat one on the desktop?

  20. #20
    New Member
    Join Date
    Oct 2000
    Posts
    3

    Question 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.

  21. #21
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  22. #22
    New Member
    Join Date
    Oct 2000
    Posts
    3
    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..

  23. #23
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    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.

  24. #24
    New Member
    Join Date
    Oct 2000
    Posts
    3
    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 ?

  25. #25
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    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]

  26. #26
    Addicted Member jeroenh's Avatar
    Join Date
    Aug 2000
    Location
    Rotterdam, Holland
    Posts
    201
    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
  •  



Click Here to Expand Forum to Full Width