A reliable SENDKEYS for vb 2005 using Windows API


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: A reliable SENDKEYS for vb 2005 using Windows API

  1. #1
    Join Date
    Jul 2007
    Posts
    142

    A reliable SENDKEYS for vb 2005 using Windows API

    A reliable SENDKEYS for vb 2005 using Windows API

    Can someone help me please

    I am disabled and have been working for sometime on an application that will help me interface with my PC better. it involves sending text and commands to third party applications .I have tried appactivate and sendkeys but find this to be unreliable. The best I have come up with so far is
    Code:
    Public Class Form2
        'Get a handle to an application window.
        Declare Auto Function FindWindow Lib "USER32.DLL" ( _
            ByVal lpClassName As String, _
            ByVal lpWindowName As String) As IntPtr
    
        '=====needed for 'set to foreground and show  =====
       
        Public Const SW_RESTORE As Integer = 9
    
        Public Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    
        ' Activate an application window.
        Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
            (ByVal hWnd As IntPtr) As Boolean
    
    
        Dim target_title As String = "Untitled - Notepad"
        Dim send_keys_count As Int16 = 0
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
       Dim target_handle As IntPtr = FindWindow(Nothing, target_title) 
    
            'set to foreground and show
            SetForegroundWindow(target_handle)
            ShowWindow(target_handle, SW_RESTORE)
    
            '=== I  need to  replace the  next  three  lines  
            For n As Int64 = 1 To 5000000 'DELAY IS ESSENTIAL
            Next
            SendKeys.SendWait("Hello")
            '=== replacemet ends
    
    
        End Sub
    
        End Class
    This works fine for applications like notepad and word but gives unreliable results in internet explorer (ie7), Google search page at least, it sends multiple characters at times.

    I would like to do away with sendkeys altogether, especially as vista does not support it. Information on the web seems to suggest that the sendinput api is probably the api to use or possibly sendmessage. Could someone please show me exactly how to replace the lines indicated above, along with any imports or declarations etc. as I am new to both windows api and vb.net.

    Alternatively a way of sending paste (and perhaps copy) to internet explorer (ie7) would help.

    Code:
    Sendkeys(“^v”)
    and variations doesn’t work in the code above



    Many thanks

    Steve

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    You might try the keybd_event API function: http://www.vbaccelerator.com/home/VB...PI/article.asp
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Jul 2007
    Posts
    142
    Thanks but this is way above me.



    Steve

    PS I have searched the web extensively and can fin nothing suitable. I need specific help please, and by the look of the web postings so do many other people.

  4. #4
    Join Date
    Jan 2009
    Posts
    2

    Send Keys replacement

    Hi Steve - you've probably found this by now - but I use http://www.autoitscript.com/autoit3/ - from my vb.net applications.

    It also comes with a scripting editor, so you can run it 'out of the box' so to speak.

    Anyway, hope it's useful and not too late.

    Kind regards,

    Mark

  5. #5
    Join Date
    Jul 2007
    Posts
    142
    No I hadn't come across it thanks.

    The thing is this is just a small but cruccial part of a fairly large and complex vb.net program.
    Would I have to rewrite the whole pragram in autoit3?


    Thanks,
    Steve

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by Steve36445 View Post
    Would I have to rewrite the whole pragram in autoit3?
    I think simply coming up with a suitable alternative for SendKeys would be more than sufficient.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    Join Date
    Jan 2009
    Posts
    2

    Send Keys replacement

    Hi Steve - no - you don't need to use the script editor - you can use it from within your vb.net (I'm using it in VS2005 and VS2008 and VWD 2005) application - just add a reference to the dll, and you can use it like this:


    Code:
    Imports System.IO
    Imports AutoItX3Lib
    
    Public Class Form1
    
        Sub simSchedules(ByVal SSG As Integer)
            Dim cAutoit As New AutoItX3Lib.AutoItX3
            On Error Resume Next
    
            cAutoit.WinActivate("Management")
            cAutoit.Sleep(500)
            cAutoit.WinWaitActive("Management")
            
            cAutoit.Send("!k")
            cAutoit.Sleep(50)
            cAutoit.Send("i")
            cAutoit.Sleep(50)
            ....
            ....
            ....
    Let me know if you've any other queries about it - I've been able to get it to work quite well with VB.Net.

    Thanks, Mark

  8. #8
    Join Date
    Jul 2007
    Posts
    142
    Thanks,

    I'll give both a try.

    Steve

  9. #9
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by mtait View Post
    I use http://www.autoitscript.com/autoit3/ - from my vb.net applications.
    Thanks for the link. I was not aware of this.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  10. #10
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Here is a version that uses SendInput
    Code:
    Option Explicit
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const INPUT_KEYBOARD = 1
    Private Type KEYBDINPUT
      wVk As Integer
      wScan As Integer
      dwFlags As Long
      time As Long
      dwExtraInfo As Long
    End Type
    Private Type GENERALINPUT
      dwType As Long
      xi(0 To 23) As Byte
    End Type
    Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Public Function SendKeysA(ByVal vKey As Integer, Optional booDown As Boolean = False)
      Dim GInput(0) As GENERALINPUT
      Dim KInput As KEYBDINPUT
      KInput.wVk = vKey
      If Not booDown Then
        KInput.dwFlags = KEYEVENTF_KEYUP
      End If
      GInput(0).dwType = INPUT_KEYBOARD
      CopyMemory GInput(0).xi(0), KInput, Len(KInput)
      Call SendInput(1, GInput(0), Len(GInput(0)))
    End Function
    It is supposed to work in Vista; but I have not actually tried it.
    Unfortunately it was saved as a plain text file with no additional
    documentation, so I don't even know who to credit for it.
    Try it, if you want to and if it Works let us know!

  11. #11
    Join Date
    Jul 2007
    Posts
    142
    Thanks.

  12. #12
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by Steve36445 View Post
    Thanks.
    Does that mean what Ron posted worked for you?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  13. #13
    Join Date
    Jul 2007
    Posts
    142
    No, havn't tried it. Just being polite. I don''t have the knowlege to implement that nor a vista machine handy to try it on.

    I've made a mental note so I can look at it again in the future.

    Steve
    Last edited by Steve36445; 01-14-2009 at 09:30 AM.

Similar Threads

  1. Layered Windows: API
    By Onetoomanysodas in forum VB Classic
    Replies: 0
    Last Post: 01-05-2006, 05:46 PM
  2. REAL Software Ships REALbasic 5 for WINDOWS
    By Gwen Smith in forum vb.announcements
    Replies: 0
    Last Post: 04-28-2003, 03:37 PM
  3. Windows api is now basic in VB with the GabitaSoft Upgrade
    By GabitaSoft in forum vb.announcements
    Replies: 0
    Last Post: 09-04-2000, 05:27 PM
  4. Windows API for changing system date format
    By metta in forum VB Classic
    Replies: 2
    Last Post: 05-05-2000, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center