How to Press or Fire Keyboard Event by Shell Scripting Technique

In most of the programming language the key press events are not comes as inbuilt feathers.At that point of time we need support of the shell scripting technique.In this post you will find the use and the different shortcut key available for this .

For VB Shell Scripting Scripting we need to create the code using set command as below

‘Set WshShell = WScript.CreateObject(“WScript.Shell”)
‘WshShell.Run “%windir%\notepad”
set objSendKey=CreateObject(“WScript.shell”)
set objSetFocus = Window(“nativeclass:=Notepad”,”index:=0″)
SystemUtil.Run “notepad.exe”, “”, “”, ”
wait 2
objSetFocus.Move 10,10
wait 1
objSetFocus.Click
[smartads]

1. How do I send an enter keystroke using QTP?

You could use the ASCII character for a carriage return Chr(13)

objSendKey.SendKeys(Chr(13))

(For more ASCII codes to use in QTP check out :QTP ASCII CHR() CODE CHART )

or use the Tilde character:
objSendKey.SendKeys(“~”)

Both will emulate an ‘Enter’ key.

2. How do I send a space?

To send a space, send the string ” “

3. How do I to send multiple keystrokes at one time using QTP’s VBscript?

To send multiple keys you can create compound string arguments. For example the following will hold down the Ctrl key, press the H key for the ‘Replace’ and type joe into the Replace window’s find textbox:
objSendKey.SendKeys(“^(h)joe”

)

4. How do I send a SHIFT, CTRL or Alt keystroke?

The special character for the Shift key in vbscript is the + sign.
For example to hold down the shift key and type a string all in capital letters try this:
objSendKey.SendKeys(“+(joecolantonio)”)

The special character for the Ctrl key in vbscript is the ^ sign and the character for the Alt key in vbscript is the % sign
Keystroke Equivalent
Alt %
Ctrl ^
Shift +

5. How do a send a right mouse click?

Try sending a a shift F10:
objSendKey.

SendKeys(“+{F10}”);

6. Are there any Sendkeys best practices?

There are a few I can think of :

Always move the application to a known start position.
Always set focus to the object you want to interact with before using SendKeys.
For synchronization issues use the .exist or wait functionality often in your script.

For Example:
objSetFocus.Move 10,10
wait 1
objSetFocus.Click

7. How do I send parenthesis using SendKeys?

Parenthesis are special characters in QTP so one way to do this is to use a combination of the shift key with the numeric nine and zero keys:
objSendKey.SendKeys(“+9”)
objSendKey.SendKeys(“joe colantonio”)
objSendKey.SendKeys(“+0”)

8. My SendKeys is not working when I try to send a multiple values.What should I do?

If QTP’s SendKeys is not performing as expected try sending each keystroke a separate line.

9. What is the QTPs equivalent for shell available in the market?

In QTP you can use the Run Method which can be used to run a file or an application. For example to start notepad:
SystemUtil.Run “notepad.exe”, “”, “”, “”

10. How to do a select a checkbox or a row in an object?

If the object like a checkbox has focus sending a blank space should select it:
objSendKey.SendKeys(” “)

11. How do I repeat a keystroke multiple times?

This only works for singe keystrokes but if you wanted to type a letter five times you could use this shortcut:
objSendKey.SendKeys(“{J 5}”)

12. What is VBScript’s equivalent for keystroke :

These are the short code for the equivalent keystroke available for the particular key.
Keystroke Equivalent
Alt %
Backspace {BACKSPACE}
Break {BREAK}
Caps Lock {CAPSLOCK}
Ctrl ^
Delete {DELETE}
Down Arrow {DOWN}
End {END}
Esc {ESC}
Help {HELP}
Home {HOME}
Insert {INSERT}
Left Arrow {LEFT}
Num Lock {NUMLOCK}
Page Down {PGDN}
Page Up {PGUP}
Print Screen {PRTSC}
Right Arrow {RIGHT}
Scroll Lock {SCROLLOCK}
Shift +
Tab {TAB}
UP Arrow {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}

13. What are the QTP VBScript’s string constants for non-visible characters in strings?

Constant Value Description
VbCr Chr(13) Carriage return
VbCrLf Chr(13) & Chr(10) Carriage return and a Linefeed
VbLf Chr(10) Line Feed
VbNewLine Chr(13) & Chr(10) New Line
VbTab Chr(9) Horizontal tab

14. I don’t see the action I need to perform in the chart above – what should I do?

Try the Device Replay method instead – check out the Device Replay chart (QTP DEVICE REAPLY CODE CHART )

15. I’m using C# not QTP how do I start an application

Use Process() for example:
using System;
using System.Diagnostics;
using System.Windows.Forms;
Process myProcess = new Process():
myProcess.StartInfo.FileName = “cmd”;
myProcess.Start();

16. In CSharp what are the SendKeys methods?

Flush() – processes all Window messages in the queue

Send() – this sends keystrokes to an app

SendWait() – Sends keystrokes to an app and waits for the keystrokes to complete.

More Info:

If you found this helpful you might want to also check out my post 3 ways to use keyboard input in QuickTest Professional: Type, SendKeys and Device Replay.

Bibliomaniacs:

And as always for my fellow bibliomaniacs who may want to dive deeper into SendKeys , I would also recommend these two books:

1. VBScript Programmer’s Reference. (Sendkey info starts on page 338)

2. A Tester’s Guide to .NET Programming (Expert’s Voice) – This book is for the automation imagineer who may want to create a simple custom GUI sendkeys app (check out page 173 of this book)

VBScript in Internet Explorer

VBScript in Internet Explorer
Here are simple steps to turn on or turn off VBScript in your Internet Explorer:
 Follow Tools -> Internet Options from the menu
 Select Security tab from the dialog box
 Click the Custom Level button
 Scroll down till you find Scripting option
 Select Enable radio button under Active scripting
 Finally click OK and come out
To disable VBScript support in your Internet Explorer, you need to select Disable radio button under Active scripting.
3. VBScript– Enabling in Browsers
VBScript
15
VBScript Placement in HTML File
There is a flexibility given to include VBScript code anywhere in an HTML document. But the most preferred way to include VBScript in your HTML file is as follows:
 Script in <head>…</head> section.
 Script in <body>…</body> section.
 Script in <body>…</body> and <head>…</head> sections.
 Script in an external file and then include in <head>…</head> section.
In the following section, we will see how we can put VBScript in different ways:
VBScript in <head>…</head> section
If you want to have a script run on some event, such as when a user clicks somewhere, then you will place that script in the head as follows:
<html>
<head>
<script type=”text/Vbscript”>
<!–
Function sayHello()
Msgbox(“Hello World”)
End Function
//–>
</script>
</head>
<body>
<input type=”button” onclick=”sayHello()” value=”Say Hello” />
</body>
</html> 4. VBScript– Placements
VBScript
16
It will produce the following result: A button with the name SayHello. Upon clicking on the Button, the message box is displayed to the user with the message “Hello World”.
VBScript in <body>…</body> section
If you need a script to run as the page loads so that the script generates content in the page, the script goes in the <body> portion of the document. In this case, you would not have any function defined using VBScript:
<html>
<head>
</head>
<body>
<script type=”text/vbscript”>
<!–
document.write(“Hello World”)
//–>
</script>
<p>This is web page body </p>
</body>
</html>
It will produce the following result:
Hello World
This is web page body
VBScript in <body> and <head> Sections
You can put your VBScript code in <head> and <body> section altogether as follows:
<html>
<head>
<script type=”text/vbscript”>
<!–
Function sayHello()
VBScript
17
msgbox(“Hello World”)
End Function
//–>
</script>
</head>
<body>
<script type=”text/vbscript”>
<!–
document.write(“Hello World”)
//–>
</script>
<input type=”button” onclick=”sayHello()” value=”Say Hello” />
</body>
</html>
It will produce the following result: Hello World message with a ‘Say Hello’ button. Upon Clicking on the button a message box with a message “Hello World” is displayed to the user.
VBScript in External File
As you begin to work more extensively with VBScript, you will likely find that there are cases, where you are reusing identical VBScript code on multiple pages of a site. You are not restricted to be maintaining identical code in multiple HTML files.
The script tag provides a mechanism to allow you to store VBScript in an external file and then include it into your HTML files. Here is an example to show how you can include an external VBScript file in your HTML code using script tag and its src attribute:
<html>
<head>
<script type=”text/vbscript” src=”filename.vbs” ></script>
</head>
<body>
…….
</body>
VBScript
18
</html>
To use VBScript from an external file source, you need to write your all VBScript source code in a simple text file with extension “.vbs” and then include that file as shown above. For example, you can keep the following content in filename.vbs file and then you can use sayHello function in your HTML file after including filename.vbs file.
Function sayHello()
Msgbox “Hello World”
End Function

VB Script Syntax Basic

YourFirstVBScript
Let us write a VBScript to print out “Hello World”.
<html>
<body>
<script language=”vbscript” type=”text/vbscript”>
document.write(“Hello World!”)
</script>
</body>
</html>
In the above example, we called a function document.write, which writes a string into the
HTML document. This function can be used to write text, HTML, or both. So, the above code
will display the following result:
Hello World!
WhitespaceandLineBreaks
VBScript ignores spaces, tabs, and newlines that appear within VBScript programs. One can
use spaces, tabs, and newlines freely within the program, so you are free to format and indent
your programs in a neat and consistent way that makes the code easy to read and understand.
Formatting
VBScript is based on Microsoft’s Visual Basic. Unlike JavaScript, no statement terminators
such as semicolon is used to terminate a particular statement.
Single Line Syntax
Colons are used when two or more lines of VBScript ought to be written in a single line. Hence,
in VBScript, Colons act as a line separator.
<script language=”vbscript” type=”text/vbscript”>
var1 = 10 : var2 = 20

</script>
Multiple Line Syntax
When a statement in VBScript is lengthy and if user wishes to break it into multiple lines, then
the user has to use underscore “_”. This improves the readability of the code. The following
example illustrates how to work with multiple lines.
<script language=”vbscript” type=”text/vbscript”>
var1 = 10
var2 = 20
Sum = var1 + var2
document.write(“The Sum of two numbers”&_
“var1 and var2 is ” & Sum)
</script>
ReservedWords
The following list shows the reserved words in VBScript. These reserved words SHOULD NOT
be used as a constant or variable or any other identifier names.

Loop LSet Me
Mod New Next
Not Nothing Null
On Option Optional
Or ParamArray Preserve
Private Public RaiseEvent
ReDim Rem Resume
RSet Select Set
Shared Single Static
Stop Sub Then
To True Type
And As Boolean
ByRef Byte ByVal
Call Case Class
Const Currency Debug
Dim Do Double
Each Else ElseIf
Empty End EndIf
Enum Eqv Event
Exit False For
Function Get GoTo
If Imp Implements
In Integer Is
Let Like Long
TypeOf Until Variant
Wend While With
Shared Single Static
Stop Sub Then
To True Type
And As Boolean
ByRef Byte ByVal
Call Case Class
Const Currency Debug
Dim Do Double
Each Else ElseIf
Empty End EndIf
Enum Eqv Event
Exit False For
Function Get GoTo
If Imp Implements
In Integer Is
Let Like Long
TypeOf Until Variant
Wend While With
Xor Eval Execute
Msgbox Erase ExecuteGlobal
Option Explicit Randomize SendKeys

CaseSensitivity
VBScript is a case-insensitive language. This means that language keywords, variables,
function names and any other identifiers need NOT be typed with a consistent capitalization
of letters. So identifiers int_counter, INT_Counter and INT_COUNTER have the same meaning
within VBScript.
CommentsinVBScript
Comments are used to document the program logic and the user information with which other
programmers can seamlessly work on the same code in future. It can include information
such as developed by, modified by and it can also include incorporated logic. Comments are
ignored by the interpreter while execution. Comments in VBScript are denoted by two
methods.
Any statement that starts with a Single Quote (‘) is treated as comment. Following is the
example:
<script language=”vbscript” type=”text/vbscript”>
<!—
‘ This Script is invoked after successful login
‘ Written by : TutorialsPoint
‘ Return Value : True / False
//- >
</script>
Any statement that starts with the keyword “REM”. Following is the example:
<script language=”vbscript” type=”text/vbscript”>
<!—
REM This Script is written to Validate the Entered Input

REM Modified by
//- > </script>
: Tutorials point/user2

Not all the modern browsers support VBScript. VBScript is supported just by Microsoft’s
Internet Explorer while other browsers (Firefox and Chrome) support just JavaScript. Hence,
developers normally prefer JavaScript over VBScript.
Though Internet Explorer (IE) supports VBScript, you may need to enable or disable this
feature manually. This tutorial will make you aware of the procedure of enabling and disabling
VBScript support in Internet Explorer.

Features of VBScript

In this post, we will outline the features of VBScript, which stands for Visual Basic Scripting and is a subset of Visual Basic for Applications (VBA), a Microsoft product integrated not only into Microsoft software like MS Project and MS Office but also into third-party tools like AUTO CAD.

**Features of VBScript:**

1. **Lightweight:** VBScript is a lightweight scripting language with a rapid interpreter.

2. **Case Insensitive:** VBScript is predominantly case insensitive, featuring a straightforward syntax that is easy to learn and implement.

3. **Object-Based:** Unlike languages like C++ or Java, VBScript is an object-based scripting language, not an Object-Oriented Programming language.

4. **COM Usage:** It utilizes the Component Object Model (COM) to access elements within the execution environment.

5. **Host Environment:** VBScript requires a host environment for successful execution, such as Internet Explorer (IE), Internet Information Services (IIS), and Windows Scripting Host (WSH).

**VBScript Version History and Uses:**

VBScript was introduced by Microsoft in 1996 with its first version, 1.0. The current stable version is 5.8, available with IE8 and Windows 7. VBScript has a wide range of applications, including:

1. **Automation Testing:** It is used in popular automation testing tools like Quick Test Professional (QTP).

2. **Windows Automation:** Windows System administrators use VBScript within Windows Scripting Host to automate Windows Desktop tasks.

3. **Web Development:** Active Server Pages (ASP), a server-side scripting environment for dynamic webpages, employs VBScript or JavaScript.

4. **Client-Side Scripting:** VBScript is used for client-side scripting in Microsoft Internet Explorer.

5. **Microsoft Outlook Forms:** VBScript is commonly used in Microsoft Outlook Forms, with application-level programming relying on VBA in Outlook 2000 onwards.

**Disadvantages:**

1. **Browser Compatibility:** VBScript is only supported by Internet Explorer, as other browsers like Chrome and Firefox do not support it. JavaScript is often preferred for broader compatibility.

2. **Limited Command Line Support:** VBScript has limited command line support.

3. **Debugging Challenges:** The absence of a default development environment makes debugging in VBScript more challenging.

**Current Status of VBScript:**

The current version of VBScript is 5.8. With the development of the .NET framework, Microsoft has chosen to support VBScript within ASP.NET for web development. As a result, there won’t be any new versions of the VBScript engine, but Microsoft’s Sustaining Engineering Team is addressing defect fixes and security issues. VBScript will continue to be shipped as part of all Microsoft Windows and IIS installations by default.