Results 1 to 20 of 20

Thread: SendMessage/Sendkeys Questions

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    SendMessage/Sendkeys Questions

    I'm not sure to use sendmessage or sendkeys with the following questions but anyways here they are:

    1.) How can I use sendkeys/sendmessage to send all the text from a .txt file?

    2.) How can I use sendkeys/sendmessage to send two backspaces and then the words "this is a test" and then the "enter" key?

    Thanks.
    Did you find a post in this thread useful? Please click Rate This Post.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SendMessage/Sendkeys Questions

    Quote Originally Posted by JBD2
    I'm not sure to use sendmessage or sendkeys with the following questions but anyways here they are:

    1.) How can I use sendkeys/sendmessage to send all the text from a .txt file?
    Send all text from a text file where?
    Quote Originally Posted by JBD2
    2.) How can I use sendkeys/sendmessage to send two backspaces and then the words "this is a test" and then the "enter" key?
    In order to do this, the appropriate code would need to be put into some type of event. Under what circumstances do you need to do this? Are you talking about a textbox? If so, what would trigger the SendKeys command? A click? A mousemove?

    SendMessage is an API which can maniuplate messages used by a window.

    SendKeys is a built in VB Function which permits certain keyboard sequences, or a single sequence, to be sent to a control or form.

  3. #3
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: SendMessage/Sendkeys Questions

    1) You would have to input the text from the text file into a string and then use SendMessage (hwnd of where to send,WM_SetText, len of string, string to send)

    2) You would have to use:
    VB Code:
    1. SendKeys "{BS}"
    2. SendKeys "{BS}"
    3. SendKeys ("THIS IS A TEST")
    4. SendKeys "{ENTER}"

    -Sir Loin

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    Both is to be sent to a textbox.
    Did you find a post in this thread useful? Please click Rate This Post.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    How can I "input the text from the text file into a string"?
    Did you find a post in this thread useful? Please click Rate This Post.

  6. #6
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: SendMessage/Sendkeys Questions

    Then why would you need SendMessage to send the text from a text file into the textbox? If you want the text of a text file to be in a text box, input the text into a String and then set the Textbox's Text property to that string.

    Here's a BETTER example., the first was pretty crappy.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim FF As Integer, StrText As String
    5.  
    6. FF% = FreeFile
    7.  
    8. Open "C:\TextFile.Txt" For Input As #FF
    9.  
    10. StrText$ = Input(LOF(FF), FF)
    11.  
    12. Close #FF
    13.  
    14. Text1.Text = StrText$
    15. End Sub

    -Sir Loin
    Last edited by Sir Loin; Aug 9th, 2005 at 12:21 PM.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SendMessage/Sendkeys Questions

    So, you have a text file that you want to load into a text box. Ok, try this:
    VB Code:
    1. Dim sfile As String
    2. sfile = "c:\yourfile.txt"
    3. Open sfile For Input As #1
    4. Text1.Text = Input(LOF(1), #1)
    5. Close #1
    I'm still not completely sure what it is you want to do with the second part of your question, so I'll just give you some code, and you can place it in whatever event you wish.
    VB Code:
    1. 'to use the enter key
    2. SendKeys "{tab}"
    3. 'to use the backspace key
    4. SendKeys "{backspace}"

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    Ignore the second Question, I've got that figured out, but what I want to do for my first one is get all the text from a .txt file (let's say "C:\Myfile.txt"), and then using sendkeys or sendmessage, send the text from that text file. I just need this to be a function because I'm doing something else that checks (if, then etc...) so it checks if a window is open and if it is, it sends the text from that textfile. the window that is open has a textbox etc...Anyways all I need is the function that reads text from a text file and then sends it. Thanks.
    Did you find a post in this thread useful? Please click Rate This Post.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SendMessage/Sendkeys Questions

    Quote Originally Posted by JBD2
    Ignore the second Question, I've got that figured out, but what I want to do for my first one is get all the text from a .txt file (let's say "C:\Myfile.txt"), and then using sendkeys or sendmessage, send the text from that text file.
    And this is what, after re-reading all of the posts in this thread, I still don't get...send it WHERE?

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    As I said...to another textbox in another window, but I don't need that code. All I need is getting the text from the .txt file, and sending the text from it...lets say I made a string value called "textfromtxtfile", something like:

    SendKeys "textfromtxtfile"

    But if you want to know the class names of the textbox or whatever/window classname let me know.
    Did you find a post in this thread useful? Please click Rate This Post.

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: SendMessage/Sendkeys Questions

    If it's not your app that you are sending the text to, you would need to know the handle of the textbox in question, and then use SendMessage to paste in text from a file. Is this what you want to do?

  12. #12
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: SendMessage/Sendkeys Questions

    Use Spy++ to get the classname of the textbox that you want to paste text in, then use SendMessage with WM_SETTEXT to paste the text.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    The thing is is it's a conversation window in MSN that I'm sending it to, and the whole window except for the title bar has the same classname...does that matter? Anyways I highlighted the text field though, it's still the same as the rest of the conversation window but anyways the classname is:

    DirectUIHWND

    and the handle is:

    202E2

    Can somebody give me a code example?
    Did you find a post in this thread useful? Please click Rate This Post.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    I tried this:

    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2.  
    3. Private Sub Form_Load()
    4. Dim sfile As String
    5. sfile = "c:\test.txt"
    6. Open sfile For Input As #1
    7. SendMessage(DirectUIHWND, Input(LOF(1), #1), vbNullString, vbNullString) As Long
    8. Close #1
    9. 'Input(LOF(1), #1)
    10. End Sub

    But it doesn't work.
    Did you find a post in this thread useful? Please click Rate This Post.

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: SendMessage/Sendkeys Questions

    You have to read the file into a string variable, and then send the string.
    Also, you can't use the classname, you have to use the hwnd of the textbox.

  16. #16
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: SendMessage/Sendkeys Questions

    Here, I'll give you an example.

    VB Code:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    5. Private Const WM_SETTEXT = &HC
    6. Private Const WM_CLOSE = &H10
    7.  
    8. Dim Notepad As Long
    9.  
    10. Private Sub Command1_Click()
    11. Dim Text As Long
    12.  
    13. Notepad& = FindWindow("Notepad", vbNullString)
    14. Text& = FindWindowEx(Notepad&, 0&, "edit", vbNullString)
    15.  
    16. Call SendMessage(Text&, WM_SETTEXT, Len(Text1.Text), ByVal Text1.Text)
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20. Shell "C:\WINDOWS\Notepad.exe", vbNormalNoFocus
    21. End Sub
    22.  
    23. Private Sub Form_Unload(Cancel As Integer)
    24. Call SendMessage(Notepad&, WM_CLOSE, 0&, 0&)
    25. End Sub

    That will open Notepad, find the text area, and set it to what you have in a text box. Try it, you need a Command Button and a textbox.

    -Sir Loin

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    How do I modify that to get text from a text file?
    Did you find a post in this thread useful? Please click Rate This Post.

  18. #18
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: SendMessage/Sendkeys Questions

    I posted an example on how to get text from a text file earlier, I'm sure you can figure out how to modify it yourself.

    -Sir Loin

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: SendMessage/Sendkeys Questions

    Can you explain to me each of the parameters you used in SendMessage, and what they do?
    Did you find a post in this thread useful? Please click Rate This Post.

  20. #20
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: SendMessage/Sendkeys Questions

    This was taken from AllAPI.net.

    The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread’s message queue and returns immediately.
    VB4-32,5,6
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Operating Systems Supported
    Requires Windows NT 3.1 or later; Requires Windows 95 or later

    Library
    User32

    Parameter Information
    · hWnd
    Identifies the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.

    · Msg
    Specifies the message to be sent.

    · wParam
    Specifies additional message-specific information.

    · lParam
    Specifies additional message-specific information.

    Return Values
    The return value specifies the result of the message processing and depends on the message sent.
    -Sir Loin

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