Auto press TAB key in VBScript

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
ankeshcs2007
Newbie
Posts: 1
Joined: Thu May 26, 2016 8:39 am

Auto press TAB key in VBScript

Post by ankeshcs2007 » Thu May 26, 2016 8:45 am

Hi All,

I want to code a script that should perform the following function:
1. runs in background
2. automatically send key {TAB} whenever user presses any number key from 0 to 9.
3. Script ends when user press F1 key.
4. Coded in VBS

I tied using the following code but its not working:

Set wshShell =wscript.CreateObject("WScript.Shell")
public a
do
a = WScript.StdIn.ReadLine()
select case a
case 1
wshshell.sendkeys "{TAB}"
case 2
wshshell.sendkeys "{TAB}"
case 3
wshshell.sendkeys "{TAB}"
case 4
wshshell.sendkeys "{TAB}"
case 5
wshshell.sendkeys "{TAB}"
case 6
wshshell.sendkeys "{TAB}"
case 7
wshshell.sendkeys "{TAB}"
case 8
wshshell.sendkeys "{TAB}"
case 9
wshshell.sendkeys "{TAB}"
case 0
wshshell.sendkeys "{TAB}"
case else
end select
loop

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Auto press TAB key in VBScript

Post by hagchr » Sun Jun 12, 2016 11:00 am

Hi,
Sorry, I am not sure how to do it in VBS so cannot help you there. In case you have MS then you could do something like the code below. Maybe there are other things one should consider, eg focus etc, but that was not mentioned in the question. Hope it helps, at least it helped me better understand the MS event handling.

Code: Select all

//Initiate Variables and create/activate OnEvents
CODEBLOCK
    //SRTtoInclude will use the template templateSRT (label below)
    //to build up the script with all SRTs needed for the
    //onEvents, and then included in script.
    Let>SRTtoInclude=
    LabelToVar>templateSRT,templateSRT

    //Create the code for the OnEvents.
    //VK48 = 1, VK96 = 1 (numpad)
    Let>ctr=47
    While>ctr<57
        Add>ctr,1
        Let>tmpx=ctr
        OnEvent>KEY_DOWN,VK%tmpx%,0,KeyPress%tmpx%
        Let>SRTtoInclude=%SRTtoInclude%%templateSRT%
        Let>tmpx=%ctr%+48
        OnEvent>KEY_DOWN,VK%tmpx%,0,KeyPress%tmpx%
        Let>SRTtoInclude=%SRTtoInclude%%templateSRT%
    EndWhile

    IncludeFromVar>SRTtoInclude

    //keyOld will contain the last pressed key
    Let>keyOld=

    //OnEvent to handle exit when F1 is pressed
    OnEvent>KEY_DOWN,VK112,0,KeyPressExit
ENDCODEBLOCK

//--------------------------------------------------------------------
Label>Main
Goto>Main
//--------------------------------------------------------------------

//SRT for exiting when pressing F1
SRT>KeyPressExit
    Exit>code
END>KeyPressExit

//SRT to check time since last key press and possibly press TAB
SRT>timecheck
    Let>timeNew=timecheck_VAR_1
    Let>timeOld=timecheck_VAR_2
    Let>tdiff={%timeNew%-%timeOld%}

    //There needs to be at least 15 msec since the
    //(same) number was previously pressed and then press TAB.
    //Can be modified/optimized.
    If>%tdiff%>15
        PRESS>TAB
    Endif
END>timecheck

//--------------------------------------------------------------------
//Template that will create an SRT for each number with onEvent
/*
templateSRT:
SRT>KeyPress%tmpx%
        Let>keyNew=%tmpx%
        If>keyNew<>keyOld
            Press>TAB
        Else
            Timer>currtime
            GoSub>timecheck,currtime,timeOld
        Endif

        Let>keyOld=keyNew
        Timer>timeOld
END>KeyPress%tmpx%
*/

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts