VBSendKeys – Send keys to any Windows and MS-DOS application

VBSendKeys – Send keys to any Windows and MS-DOS application

Private Declare Function OemKeyScan Lib "user32" (ByVal wOemChar As Integer) As _    LongPrivate Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal _    lpszSrc As String, ByVal lpszDst As String) As LongPrivate Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar _    As Byte) As IntegerPrivate Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _    (ByVal wCode As Long, ByVal wMapType As Long) As LongPrivate Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)Private Const KEYEVENTF_KEYDOWN      As Long = &H0Private Const KEYEVENTF_KEYUP        As Long = &H2Type VKType    VKCode As Integer    scanCode As Integer    Control As Boolean    Shift As Boolean    Alt As BooleanEnd Type'---------------------------------------------------------------------' Routine: VbSendKeys()'' Author:  Bryan Wolf, 1999'' Purpose: Imitate VB's internal SendKeys statement, but add the'          ability to send keypresses to a DOS application.  You'          can use SendKeys, to paste ASCII characters to a DOS'          window from the clipboard, but you can't send function'          keys.  This module solves that problem and makes sending'          any keys to any Application, DOS or Windows, easy.'' Arguments: Keystrokes.  Note that this does not implement the'            SendKeys's 'wait' argument.  If you need to wait,'            try using a timing loop.''            The syntax for specifying keystrokes is the'            same as that of SendKeys - Please refer to VB's'            documentation for an in-depth description.  Support'            for the following codes has been added, in addition'            to the standard set of codes suppored by SendKeys:''            KEY                  CODE'            break                {CANCEL}'            escape               {ESCAPE}'            left mouse button    {LBUTTON}'            right mouse button   {RBUTTON}'            middle mouse button  {MBUTTON}'            clear                {CLEAR}'            shift                {SHIFT}'            control              {CONTROL}'            alt                  {MENU} or {ALT}'            pause                {PAUSE}'            space                {SPACE}'            select               {SELECT}'            execute              {EXECUTE}'            snapshot             {SNAPSHOT}'            number pad 0         {NUMPAD0}'            number pad 1         {NUMPAD1}'            number pad 2         {NUMPAD2}'            number pad 3         {NUMPAD3}'            number pad 4         {NUMPAD4}'            number pad 5         {NUMPAD5}'            number pad 6         {NUMPAD6}'            number pad 7         {NUMPAD7}'            number pad 8         {NUMPAD8}'            number pad 9         {NUMPAD9}'            number pad multiply  {MULTIPLY}'            number pad add       {ADD}'            number pad separator {SEPARATOR}'            number pad subtract  {SUBTRACT}'            number pad decimal   {DECIMAL}'            number pad divide    {DIVIDE}'' Sample Calls:'   VbSendKeys "Dir~"   ' View a directory of in DOS'' NOTE: there is a minor difference with SendKeys syntax. You can'       group multiple characters under the same shift key using'       curly brackets, while VB's SendKeys uses regular brackets.'       For example, to keep the SHIFT key pressed while you type'       A, B, and C keys, you must run the following statement'           VBSendKeys "+{abc}"'       while the syntax of the built-in VB function is'           SendKeys "+(abc)"'---------------------------------------------------------------------Sub VbSendKeys(ByVal sKeystrokes As String)    Dim iKeyStrokesLen As Integer    Dim lRepetitions As Long    Dim bShiftKey As Boolean    Dim bControlKey As Boolean    Dim bAltKey As Boolean    Dim lResult As Long    Dim sKey As String    Dim iAsciiKey As Integer    Dim iVirtualKey As Integer    Dim i As Long    Dim j As Long      Static bInitialized As Boolean    Static AsciiKeys(0 To 255) As VKType    Static VirtualKeys(0 To 255) As VKType      On Error GoTo 0    If Not bInitialized Then        Dim iKey As Integer        Dim OEMChar As String        Dim keyScan As Integer                ' Initialize AsciiKeys()        For iKey = LBound(AsciiKeys) To UBound(AsciiKeys)            keyScan = VkKeyScan(iKey)            AsciiKeys(iKey).VKCode = keyScan And &HFF   ' low-byte of key scan                                                         ' code            AsciiKeys(iKey).Shift = (keyScan And &H100)            AsciiKeys(iKey).Control = (keyScan And &H200)            AsciiKeys(iKey).Alt = (keyScan And &H400)            ' Get the ScanCode            OEMChar = "  " ' 2 Char            CharToOem Chr(iKey), OEMChar            AsciiKeys(iKey).scanCode = OemKeyScan(Asc(OEMChar)) And &HFF        Next iKey                ' Initialize VirtualKeys()        For iKey = LBound(VirtualKeys) To UBound(VirtualKeys)            VirtualKeys(iKey).VKCode = iKey            VirtualKeys(iKey).scanCode = MapVirtualKey(iKey, 0)            ' no use in initializing remaining elements        Next iKey        bInitialized = True     ' don't run this code twice    End If    ' End of initialization routine      ' Parse the string in the same way that SendKeys() would    Do While Len(sKeystrokes)        lRepetitions = 1 ' Default number of repetitions for each character        bShiftKey = False        bControlKey = False        bAltKey = False                ' Pull off Control, Alt or Shift specifiers        sKey = Left$(sKeystrokes, 1)        sKeystrokes = Mid$(sKeystrokes, 2)                Do While InStr(" ^%+", sKey) > 1 ' The space in " ^%+" is necessary            If sKey = "+" Then                bShiftKey = True            ElseIf sKey = "^" Then                bControlKey = True            ElseIf sKey = "%" Then                bAltKey = True            End If            sKey = Left$(sKeystrokes, 1)            sKeystrokes = Mid$(sKeystrokes, 2)        Loop                ' Look for "{}"        If sKey = "{" Then            ' Look for the  "}"            i = InStr(sKeystrokes, "}")            If i > 0 Then                sKey = Left$(sKeystrokes, i - 1) ' extract the content between                                                  ' the {}                sKeystrokes = Mid$(sKeystrokes, i + 1) ' Remove the }            End If                    ' Look for repetitions            i = Len(sKey)            Do While Mid$(sKey, i, 1) >= "0" And Mid$(sKey, i, _                1) <= "9" And i >= 3                i = i - 1            Loop                    If i < Len(sKey) Then ' If any digits were found...                If i >= 2 Then ' If there is something preceding it...                    If Mid$(sKey, i, 1) = " " Then  ' If a space precedes the                                                     ' digits...                        On Error Resume Next ' On overflow, ignore the value                        lRepetitions = CLng(Mid$(sKey, i + 1))                        On Error GoTo 0                        sKey = Left$(sKey, i - 1)                    End If                End If            End If        End If                ' Look for special words        Select Case UCase$(sKey)            Case "LBUTTON" ' New                iVirtualKey = vbKeyLButton            Case "RBUTTON" ' New                iVirtualKey = vbKeyRButton            Case "BREAK", "CANCEL"                iVirtualKey = vbKeyCancel            Case "MBUTTON" ' New                iVirtualKey = vbKeyMButton            Case "BACKSPACE", "BS", "BKSP"                iVirtualKey = vbKeyBack            Case "TAB"                iVirtualKey = vbKeyTab            Case "CLEAR" ' New                iVirtualKey = vbKeyClear            Case "ENTER", "~"                iVirtualKey = vbKeyReturn            Case "SHIFT" ' New                iVirtualKey = vbKeyShift            Case "CONTROL" ' New                iVirtualKey = vbKeyControl            Case "MENU", "ALT" ' New                iVirtualKey = vbKeyMenu            Case "PAUSE" ' New                iVirtualKey = vbKeyPause            Case "CAPSLOCK"                iVirtualKey = vbKeyCapital            Case "ESCAPE", "ESC"                iVirtualKey = vbKeyEscape            Case "SPACE" ' New                iVirtualKey = vbKeySpace            Case "PGUP"                iVirtualKey = vbKeyPageUp            Case "PGDN"                iVirtualKey = vbKeyPageDown            Case "END"                iVirtualKey = vbKeyEnd            Case "HOME" ' New                iVirtualKey = vbKeyHome            Case "LEFT"                iVirtualKey = vbKeyLeft            Case "UP"                iVirtualKey = vbKeyUp            Case "RIGHT"                iVirtualKey = vbKeyRight            Case "DOWN"                iVirtualKey = vbKeyDown            Case "SELECT" ' New                iVirtualKey = vbKeySelect            Case "PRTSC"                iVirtualKey = vbKeyPrint            Case "EXECUTE" ' New                iVirtualKey = vbKeyExecute            Case "SNAPSHOT" ' New                iVirtualKey = vbKeySnapshot            Case "INSERT", "INS"                iVirtualKey = vbKeyInsert            Case "DELETE", "DEL"                iVirtualKey = vbKeyDelete            Case "HELP"                iVirtualKey = vbKeyHelp            Case "NUMLOCK"                iVirtualKey = vbKeyNumlock            Case "SCROLLLOCK"                iVirtualKey = vbKeyScrollLock            Case "NUMPAD0" ' New                iVirtualKey = vbKeyNumpad0            Case "NUMPAD1" ' New                iVirtualKey = vbKeyNumpad1            Case "NUMPAD2" ' New                iVirtualKey = vbKeyNumpad2            Case "NUMPAD3" ' New                iVirtualKey = vbKeyNumpad3            Case "NUMPAD4" ' New                iVirtualKey = vbKeyNumpad4            Case "NUMPAD5" ' New                iVirtualKey = vbKeyNumpad5            Case "NUMPAD6" ' New                iVirtualKey = vbKeyNumpad6            Case "NUMPAD7" ' New                iVirtualKey = vbKeyNumpad7            Case "NUMPAD8" ' New                iVirtualKey = vbKeyNumpad8            Case "NUMPAD9" ' New                iVirtualKey = vbKeyNumpad9            Case "MULTIPLY" ' New                iVirtualKey = vbKeyMultiply            Case "ADD" ' New                iVirtualKey = vbKeyAdd            Case "SEPARATOR" ' New                iVirtualKey = vbKeySeparator            Case "SUBTRACT" ' New                iVirtualKey = vbKeySubtract            Case "DECIMAL" ' New                iVirtualKey = vbKeyDecimal            Case "DIVIDE" ' New                iVirtualKey = vbKeyDivide            Case "F1"                iVirtualKey = vbKeyF1            Case "F2"                iVirtualKey = vbKeyF2            Case "F3"                iVirtualKey = vbKeyF3            Case "F4"                iVirtualKey = vbKeyF4            Case "F5"                iVirtualKey = vbKeyF5            Case "F6"                iVirtualKey = vbKeyF6            Case "F7"                iVirtualKey = vbKeyF7            Case "F8"                iVirtualKey = vbKeyF8            Case "F9"                iVirtualKey = vbKeyF9            Case "F10"                iVirtualKey = vbKeyF10            Case "F11"                iVirtualKey = vbKeyF11            Case "F12"                iVirtualKey = vbKeyF12            Case "F13"                iVirtualKey = vbKeyF13            Case "F14"                iVirtualKey = vbKeyF14            Case "F15"                iVirtualKey = vbKeyF15            Case "F16"                iVirtualKey = vbKeyF16            Case Else                ' Not a virtual key                iVirtualKey = -1        End Select                ' Turn on CONTROL, ALT and SHIFT keys as needed        If bShiftKey Then            keybd_event VirtualKeys(vbKeyShift).VKCode, _                VirtualKeys(vbKeyShift).scanCode, KEYEVENTF_KEYDOWN, 0        End If                If bControlKey Then            keybd_event VirtualKeys(vbKeyControl).VKCode, _                VirtualKeys(vbKeyControl).scanCode, KEYEVENTF_KEYDOWN, 0        End If                If bAltKey Then            keybd_event VirtualKeys(vbKeyMenu).VKCode, _                VirtualKeys(vbKeyMenu).scanCode, KEYEVENTF_KEYDOWN, 0        End If                ' Send the keystrokes        For i = 1 To lRepetitions            If iVirtualKey > -1 Then                ' Virtual key                keybd_event VirtualKeys(iVirtualKey).VKCode, _                    VirtualKeys(iVirtualKey).scanCode, KEYEVENTF_KEYDOWN, 0                keybd_event VirtualKeys(iVirtualKey).VKCode, _                    VirtualKeys(iVirtualKey).scanCode, KEYEVENTF_KEYUP, 0            Else                ' ASCII Keys                For j = 1 To Len(sKey)                    iAsciiKey = Asc(Mid$(sKey, j, 1))                    ' Turn on CONTROL, ALT and SHIFT keys as needed                    If Not bShiftKey Then                        If AsciiKeys(iAsciiKey).Shift Then                            keybd_event VirtualKeys(vbKeyShift).VKCode, _                                VirtualKeys(vbKeyShift).scanCode, _                                KEYEVENTF_KEYDOWN, 0                        End If                    End If                            If Not bControlKey Then                        If AsciiKeys(iAsciiKey).Control Then                            keybd_event VirtualKeys(vbKeyControl).VKCode, _                                VirtualKeys(vbKeyControl).scanCode, _                                KEYEVENTF_KEYDOWN, 0                        End If                    End If                            If Not bAltKey Then                        If AsciiKeys(iAsciiKey).Alt Then                            keybd_event VirtualKeys(vbKeyMenu).VKCode, _                                VirtualKeys(vbKeyMenu).scanCode, _                                KEYEVENTF_KEYDOWN, 0                        End If                    End If                            ' Press the key                    keybd_event AsciiKeys(iAsciiKey).VKCode, _                        AsciiKeys(iAsciiKey).scanCode, KEYEVENTF_KEYDOWN, 0                    keybd_event AsciiKeys(iAsciiKey).VKCode, _                        AsciiKeys(iAsciiKey).scanCode, KEYEVENTF_KEYUP, 0                            ' Turn on CONTROL, ALT and SHIFT keys as needed                    If Not bShiftKey Then                        If AsciiKeys(iAsciiKey).Shift Then                            keybd_event VirtualKeys(vbKeyShift).VKCode, _                                VirtualKeys(vbKeyShift).scanCode, _                                KEYEVENTF_KEYUP, 0                        End If                    End If                            If Not bControlKey Then                        If AsciiKeys(iAsciiKey).Control Then                            keybd_event VirtualKeys(vbKeyControl).VKCode, _                                VirtualKeys(vbKeyControl).scanCode, _                                KEYEVENTF_KEYUP, 0                        End If                    End If                            If Not bAltKey Then                        If AsciiKeys(iAsciiKey).Alt Then                            keybd_event VirtualKeys(vbKeyMenu).VKCode, _                                VirtualKeys(vbKeyMenu).scanCode, _                                KEYEVENTF_KEYUP, 0                        End If                    End If                Next j ' Each ASCII key            End If  ' ASCII keys        Next i ' Repetitions                ' Turn off CONTROL, ALT and SHIFT keys as needed        If bShiftKey Then            keybd_event VirtualKeys(vbKeyShift).VKCode, _                VirtualKeys(vbKeyShift).scanCode, KEYEVENTF_KEYUP, 0        End If                If bControlKey Then            keybd_event VirtualKeys(vbKeyControl).VKCode, _                VirtualKeys(vbKeyControl).scanCode, KEYEVENTF_KEYUP, 0        End If                If bAltKey Then            keybd_event VirtualKeys(vbKeyMenu).VKCode, _                VirtualKeys(vbKeyMenu).scanCode, KEYEVENTF_KEYUP, 0        End If            Loop ' sKeyStrokesEnd Sub

devx-admin

devx-admin

Share the Post:
Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years,

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW)

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years, as reported by energy analytics

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the