How to "sendkeys" with "alt" button?-VBForums
Results 1 to 7 of 7

Thread: How to "sendkeys" with "alt" button?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    How to "sendkeys" with "alt" button?

    Hello,

    I'd like to send keys ("sendkeys" function) to one (or more) of the opened windows on the desktop.

    The key is consisted of Alt+F6 buttons.

    I know this code - but how to add the "Alt" button? Do you know for other or better code?

    Thanks in advance!

    Code:
    Option Explicit
    Dim wshShell
    Dim intCount
    
    'Open notepad
    Set wshShell= CreateObject("WScript.Shell")
    wshShell.Run "clearexplorer g:\views\tg_ClearCheck_1.0_int_2"
    
    'Wait a second before we do anything
    WScript.sleep 1000
    
    intCount=0
    
    'Send the keys to notepad application
    Do While intCount <= 10
      wshShell.SendKeys "Line No: "
      wshShell.SendKeys intCount
      wshShell.SendKeys "{F6}"  ' I have to add "Alt" button!
    
      intCount = intCount + 1
    Loop
    
    'Quit
    WScript.Quit

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    19,827

    Re: How to "sendkeys" with "alt" button?

    from msdn
    SendKeys "%{F4}", True ' Send ALT+F4 to close Calculator.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,334

    Re: How to "sendkeys" with "alt" button?

    One note of caution...if you move to Vista, SendKeys is no longer going to work for you.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    623

    Re: How to "sendkeys" with "alt" button?

    >Vista, SendKeys is no longer going to work for you

    SendKeys did not work under beta versions of Vista but in RC1 and SP1 but it works pretty much as before in .exes; the occasional line may need a tweak but that's all.

    Sendkeys will give you an Error 70 Permission denied under the VB6 IDE in Vista, something like the following will overcome that;

    Code:
    Public Sub Sendkeys(Text$, Optional wait As Boolean = False)
    
        Static init As Boolean, IsIDEUnderVista As Boolean, WshShell As Object
        
        'wrapper for Sendkeys which does not cause an Error 70 in the IDE under Windows Vista
        'Errors due to WScript disablement on the OS can only happen under the IDE
        'Extra overhead for the WshShell object is only required under the IDE
    
        If Not init Then
            If IsDevEnv() Then
                IsIDEUnderVista = (OsVersion() >= 6)
                If IsIDEUnderVista Then Set WshShell = CreateObject("WScript.Shell")
            End If
            init = True
        End If
        
        If Not IsIDEUnderVista Then
            VBA.Sendkeys Text$, wait
        Else
            WshShell.Sendkeys Text$, wait
        End If
        
    End Sub
    You are left to devise your own IsDevEnv() and OsVersion() functions.
    Last edited by Magic Ink; Mar 4th, 2009 at 05:49 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: How to "sendkeys" with "alt" button?

    Thank you very much.

    I forgot to ask - how do I send the keys to an already running application? In the examples above, a new object is created and then "sendkeys" are executed. I just want to find an already running app (e.g. "notepad") and then send it keys.

    Thanks again!

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    19,827

    Re: How to "sendkeys" with "alt" button?

    try appactivate

    otherwise search on findwindow API
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: How to "sendkeys" with "alt" button?

    Thank you very much.

    However, I tried to use appactivate and findwindow functions, but I can't find how to find activated window (not to open a new one) and then send him keys. Do you have such an example?

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.