Results 1 to 11 of 11

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
    25,618

    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
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    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 06: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
    25,618

    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

  8. #8
    Junior Member scrapersNbots.com's Avatar
    Join Date
    Dec 2016
    Location
    Torrington, CT
    Posts
    25

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

    dim wshShell: Set wshShell= CreateObject("WScript.Shell")
    if not(wshShell is nothing) then
    wshShell.sendkeys "%(F6)"
    doevents
    set wshShell = nothing
    end if

    works perfectly, just make sure you first set focus to the app you want to send keys to.
    〘SCRAPER〙software that extracts, organizes and displays data from the web.
    〘BOT〙software that automates and speeds up tasks on the web, mimicking human behavior.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

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

    Examples of things we do not discuss here:

    〘SCRAPER〙software that extracts, organizes and displays data from the web.
    〘BOT〙software that automates and speeds up tasks on the web, mimicking human behavior.

    The first almost always violates a site's terms of use, making such discussions dubious under this site's terms anyway. The second almost always violates the terms here.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

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

    This thread is eight years old. Most likely, the OP has moved on by now.
    My usual boring signature: Nothing

  11. #11
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

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

    Can SendKeys be substituted using SendMessage with "%{F$}"

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