Jump to content

Converting a VBS script to AutoIt.


BeBoP
 Share

Recommended Posts

Hi, Sorry for the rather vague title. Recently I bought a PS3 controller to play around with the SIXAXIS motion controls. After finding a way to get the joystick's axis data with AutoIt I ran into a problem.sending keyboard input corresponding to the controllers output to some games. While looking for a solution to this I found the Device Simulation Framework which allows for a driver simulated keyboard. I should mention I have tried many other methods for emulation of keyboard keys which failed to affect some games and I'm not looking for any other method.

With all that out of the way what I have so far is a vbs script that works. It has no problem sending keys to Just Cause 2 for example. However the way I made the script work is by having the VBS script read data using AutoItX from an AutoIt GUI that would relay the necessary information. Even though that works its very clunky and my VBS knowledge is very limited to the whole thing was very basic.

Before I post the code I'll give some necessary steps to take before it can be ran, DSF must be installed, this can be acquired from the Windows Driver Kit 7.1 http://www.microsoft.com/en-us/download/details.aspx?id=11800. Inside of that ISO is a folder called DSF within that folder are 3 files. To install DSF run either DSFx86Runtime.msi or DSFx64Runtime.msi

depending on your OS. Once the installation is complete DSF can be found installed in C:Program FilesDSF. Before running the script make sure to run softehcicfg.exe /install in CMD on C:Program FilesDSFsoftehci. Finally you may either use my script which is just the two provided by Microsoft put together into one. Or run the examples in C:Program Filesdsfusbhid yourself through CMD.

Getting to the point here is the VBS scrip I am trying to convert, it will press and release wasd and type out "test string":

<PACKAGE>

<JOB id=UseKBD>
<REFERENCE OBJECT="DSF.DSF"/>
<REFERENCE OBJECT="SOFTUSB.SoftUSBHub"/>
<REFERENCE OBJECT="SOFTUSB.SoftUSBDevice"/>
<REFERENCE OBJECT="SOFTEHCI.SoftEHCI"/>
<REFERENCE OBJECT="INPUTKBD.SoftKeyboard"/>
<REFERENCE OBJECT="SOFTHIDUSBK.SoftHIDProtocolXlator"/>
<REFERENCE OBJECT="SOFTHID.SoftHIDInputKbdMapper"/>
<SCRIPT LANGUAGE="VBScript">

Option Explicit

const IID_IDSFBus                = "{E927C266-5364-449E-AE52-D6A782AFDA9C}"
const IID_ISoftUSBDevice         = "{9AC61697-81AE-459A-8629-BF5D5A838519}"
const IID_ISoftHIDProtocolXlator = "{78C6932B-1B53-469F-9DE3-38114BE3B41E}"
const IID_EHCICtrlrObj           = "{16017C34-A2BA-480B-8DE8-CD08756AD1F8}"
const IID_ISoftUSBHub            = "{0442B742-2BD3-4B07-99D8-65B6395BCDB0}"

WriteLine "UseKBD.wsf starting"

'Create the DSF base object
Dim DSF       : Set DSF = CreateObject("DSF.DSF")

Dim ExtHub  : Set ExtHub = CreateObject("SOFTUSB.SoftUSBHub")
WriteLine "Setting diagnostics for external hub"
SetEndpointDiagnostics ExtHub.SoftUSBDevice

WriteLine "Enumerating simulated devices to find EHCI controller"
Dim CtrlrDev : Set CtrlrDev = EnumSimulatedDevices(IID_IDSFBus)

If CtrlrDev Is Nothing Then
    Error "Could not find simulated a EHCI controller. Did you remember to run softehcicfg.exe /install?"
End If

Dim CtrlrObj : Set CtrlrObj = CtrlrDev.Object(IID_EHCICtrlrObj)

Dim RootHubPort : Set RootHubPort = CtrlrObj.Ports(1)

RootHubPort.HotPlug ExtHub.SoftUSBDevice.DSFDevice

Dim SoftKbd        : Set SoftKbd = CreateObject("INPUTKBD.SoftKeyboard")
Dim KbdMapper      : Set KbdMapper = CreateObject("SOFTHID.SoftHIDInputKbdMapper")
Dim ProtocolXlator : Set ProtocolXlator = CreateObject("SOFTHIDUSBK.SoftHIDProtocolXlator")

KbdMapper.AttachKbd SoftKbd
KbdMapper.AttachProtocolXlator ProtocolXlator

Dim DSFDev : Set DSFDev = ProtocolXlator.DSFDevice
Dim USBDev : Set USBDev = DSFDev.Object(IID_ISoftUSBDevice)
USBDev.USB = &H110 ' make this a 1.1 keyboard
WriteLine "Setting diagnostics for keyboard device"
SetEndpointDiagnostics USBDev

Dim ExtHubPort : Set ExtHubPort = ExtHub.Ports(1)
ExtHubPort.HotPlug DSFDev

Dim RootHubPorts : Set RootHubPorts = CtrlrObj.Ports

Dim DSFDevExtHub : Set DSFDevExtHub = RootHubPorts(1).Device

Dim ExtHubPorts : Set ExtHubPorts = ExtHub.Ports
Dim DSFDevProtocolXlator : Set DSFDevProtocolXlator = ExtHubPorts(1).Device

WriteLine "Press Enter to get text."
WScript.StdIn.ReadLine()

SoftKbd.PressKey KEY_W
SoftKbd.ReleaseKey KEY_W

SoftKbd.PressKey KEY_A
SoftKbd.ReleaseKey KEY_A

SoftKbd.PressKey KEY_S
SoftKbd.ReleaseKey KEY_S

SoftKbd.PressKey KEY_D
SoftKbd.ReleaseKey KEY_D

SoftKbd.PressAndReleaseKeys "test string"

WriteLine "Press Enter to exit."
WScript.StdIn.ReadLine()

KbdMapper.AttachKbd Nothing
KbdMapper.AttachProtocolXlator Nothing

ExtHubPort.Unplug

RootHubPort.Unplug

'clean up the protocolXlator device
USBDev.Destroy

'Clean up the external hub device
ExtHub.Destroy

WriteLine "Script complete"

WScript.Quit 0

'/////////////////////////////////////////////////////////////////////////////
' Function EnumSimulatedDevices
'
' This function searches the collection of simulated devices
' referenced by DSF.Devices for a device that exposes an ancillary
' object from DSFDevice.Object with the specified GUID. If found it returns the
' DSFDevice object otherwise it returns Nothing.
'/////////////////////////////////////////////////////////////////////////////
Private Function EnumSimulatedDevices(SearchObjectGUID)

    Dim DevSought : Set DevSought = Nothing
    Dim Dev       : Set Dev = Nothing
    Dim ObjSought : Set ObjSought = Nothing

    For Each Dev in DSF.Devices
        If Dev.HasObject(SearchObjectGUID) Then
            Set ObjSought = Dev.Object(SearchObjectGUID)
            If Not ObjSought Is Nothing Then
                Set DevSought = Dev

                Exit For
            End If
        End If
    Next

    Set EnumSimulatedDevices = DevSought

End Function

'/////////////////////////////////////////////////////////////////////////////
' Sub SetEndpointDiagnostics
'
' This routine sets a diagnostic property on all the endpoints in the
' specified simulated USB device so that you can see all transfers to and
' from the device when running under a kernel debugger. This routine will
' write information to the console describing every congiguration, interface,
' and endpoint that it finds.
'/////////////////////////////////////////////////////////////////////////////
Private Sub SetEndpointDiagnostics(USBDevice)

    const SOFTUSBENDPOINT_OBJECTFLAGS                     = 100
    const SOFTUSBENDPOINT_DONOTTRACETRANSFERS             = &H00000000
    const SOFTUSBENDPOINT_TRACETRANSFERINPUT              = &H00000001
    const SOFTUSBENDPOINT_TRACETRANSFEROUPUT              = &H00000002
    const SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT      = &H00000003

    Dim Config    : Set Config    = Nothing
    Dim Interface : Set Interface = Nothing
    Dim Endpoint  : Set Endpoint  = Nothing

    Dim CtlFlags   : CtlFlags   = SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT
    Dim OtherFlags : OtherFlags = SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT
    Dim FlagsType  : FlagsType  = SOFTUSBENDPOINT_OBJECTFLAGS
    Dim EPNum :  EPNum = 0
    Dim EPDir :  EPDir = 0
    Dim EPType : EPType = 0

    USBDevice.Endpoint0.SetObjectFlags FlagsType, CtlFlags

    For Each Config In USBDevice.Configurations

        WriteLine "Setting endpoint diagnostics for configuration " & Config.ConfigurationValue

        For Each Interface In Config.Interfaces

            WriteLine "    Setting endpoint diagnostics for interface " & Interface.InterfaceNumber & " alternate " & Interface.AlternateSetting

            For Each Endpoint In Interface.Endpoints

                EPNum = Endpoint.EndpointAddress And &H0F
                EPDir = Endpoint.EndpointAddress And &H80
                If EPDir = 0 Then
                    EPDir = "OUT"
                Else
                    EPDir = "IN"
                End If

                EPType = Endpoint.Attributes And &H03
                Select Case EPType
                    Case 0:
                        EPType = "Control"

                    Case 1:
                        EPType = "Isoch"

                    Case 2:
                        EPType = "Bulk"

                    Case 3:
                        EPType = "Interrupt"
                End Select

                WriteLine "        Endpoint.SetObjectFlags for " & EPType & " " & EPDir & " endpoint " & Hex(EPNum)
                Endpoint.SetObjectFlags FlagsType, OtherFlags

            Next

        Next

    Next

End Sub


'/////////////////////////////////////////////////////////////////////////////
' Sub WriteLine
'
' This routine writes the specified text to the console followed by a
' newline.
'/////////////////////////////////////////////////////////////////////////////
Private Sub WriteLine(Text)

    WScript.StdOut.WriteLine text

End Sub


'/////////////////////////////////////////////////////////////////////////////
' Sub Write
'
' This routine writes the specified text to the console
'/////////////////////////////////////////////////////////////////////////////
Private Sub Write(Text)

    WScript.StdOut.Write text

End Sub



'/////////////////////////////////////////////////////////////////////////////
' Sub Error
'
' This routine writes the specified text to the console and
' terminates the script
'/////////////////////////////////////////////////////////////////////////////
Private Sub Error(msg)
    WriteLine "Script terminating due to error"
    WriteLine msg
    WScript.Quit 1
End Sub


</SCRIPT>
</JOB>
</PACKAGE>

Finally here is what I have so far in terms of AutoIt, You may notice I added a bit of code for error handling I found somewhere on the forums here, I also had to force the script to run in 64bit mode in order for the script to see the class references correctly. Right now I am getting some errors I don't really understand at all. I'm worried the errors are steming from me attempting to run this script in 64 bit mode. The current error I am getting is "Memeber not found" on line 174( $USBDevice.Endpoint0.SetObjectFlags($FlagsType, $CtlFlags). And here is the code:

#RequireAdmin

#include <Misc.au3>

#AutoIt3Wrapper_UseX64=y

Opt("ExpandEnvStrings", 1)

Local $hDLL = DllOpen("user32.dll")

;Option Explicit

Const $IID_IDSFBus                = "{E927C266-5364-449E-AE52-D6A782AFDA9C}"
Const $IID_ISoftUSBDevice         = "{9AC61697-81AE-459A-8629-BF5D5A838519}"
Const $IID_ISoftHIDProtocolXlator = "{78C6932B-1B53-469F-9DE3-38114BE3B41E}"
Const $IID_EHCICtrlrObj           = "{16017C34-A2BA-480B-8DE8-CD08756AD1F8}"
Const $IID_ISoftUSBHub            = "{0442B742-2BD3-4B07-99D8-65B6395BCDB0}"

$objErr = ObjEvent("AutoIt.Error","MyErrFunc")

WriteLine("Use1.1Kbd.wsf starting")
Dim $DSF
$DSF = ObjCreate("DSF.DSF")

Dim $ExtHub
$ExtHub = ObjCreate("SOFTUSB.SoftUSBHub")
WriteLine("Setting diagnostics for external hub")
SetEndpointDiagnostics($ExtHub.SoftUSBDevice)

WriteLine("Enumerating simulated devices to find EHCI controller")
Dim $CtrlrDev
$CtrlrDev = EnumSimulatedDevices($IID_IDSFBus)

If IsObj($CtrlrDev) = 0 Then
    _Error("Could not find simulated a EHCI controller. Did you remember to run softehcicfg.exe /install?")
EndIf

Dim $CtrlrObj
$CtrlrObj = $CtrlrDev.Object($IID_EHCICtrlrObj)

Dim $RootHubPort
$RootHubPort = $CtrlrObj.Ports(1)

$RootHubPort.HotPlug($ExtHub.SoftUSBDevice.DSFDevice)

Dim $SoftKbd
$SoftKbd = ObjCreate("INPUTKBD.SoftKeyboard")
Dim $KbdMapper
$KbdMapper = ObjCreate("SOFTHID.SoftHIDInputKbdMapper")
Dim $ProtocolXlator
$ProtocolXlator = ObjCreate("SOFTHIDUSBK.SoftHIDProtocolXlator")

$KbdMapper.AttachKbd($SoftKbd)
$KbdMapper.AttachProtocolXlator($ProtocolXlator)

Dim $DSFDev
$DSFDev = $ProtocolXlator.DSFDevice
Dim $USBDev
$USBDev = $DSFDev.Object($IID_ISoftUSBDevice)
$USBDev.USB = 0x110 ; make this a 1.1 keyboard
WriteLine("Setting diagnostics for keyboard device")
SetEndpointDiagnostics($USBDev)

Dim $ExtHubPort
$ExtHubPort = $ExtHub.Ports(1)
$ExtHubPort.HotPlug($DSFDev)

Dim $RootHubPorts
$RootHubPorts = $CtrlrObj.Ports

Dim $DSFDevExtHub
$DSFDevExtHub = $RootHubPorts(1).Device

Dim $ExtHubPorts
$ExtHubPorts = $ExtHub.Ports
Dim $DSFDevProtocolXlator
$DSFDevProtocolXlator = $ExtHubPorts(1).Device

WriteLine("Press Enter to get text.")

While 1
    If _IsPressed("0D", $hDLL) Then
        ExitLoop
    EndIf
    Sleep(25)
WEnd

$SoftKbd.PressKey("KEY_S")
$SoftKbd.ReleaseKey("KEY_S")

$SoftKbd.PressKey("KEY_W")
$SoftKbd.ReleaseKey("KEY_W")

$SoftKbd.PressKey("KEY_A")
$SoftKbd.ReleaseKey("KEY_A")

$SoftKbd.PressKey("KEY_D")
$SoftKbd.ReleaseKey("KEY_D")

$SoftKbd.PressAndReleaseKeys("test string")

$KbdMapper.AttachKbd()
$KbdMapper.AttachProtocolXlator()

WriteLine("Press Enter to exit.")

While 1
    If _IsPressed("0D", $hDLL) Then
        ExitLoop
    EndIf
    Sleep(25)
WEnd

DllClose($hDLL)


$ExtHubPort.Unplug

$RootHubPort.Unplug

;clean up the protocolXlator device
$USBDev.Destroy

;Clean up the external hub device
$ExtHub.Destroy

WriteLine("Script complete")

Exit

Func EnumSimulatedDevices($SearchObjectGUID)

    Dim $DevSought,$Dev ,$ObjSought

    For $Dev in $DSF.Devices
        If $Dev.HasObject($SearchObjectGUID) Then
            $ObjSought = $Dev.Object($SearchObjectGUID)
            If IsObj($ObjSought) Then
                $DevSought = $Dev
                ExitLoop
            EndIf
        EndIf
    Next

    $EnumSimulatedDevices = $DevSought

    Return $EnumSimulatedDevices
EndFunc


Func SetEndpointDiagnostics($USBDevice)

    Const $SOFTUSBENDPOINT_OBJECTFLAGS                     = 100
    Const $SOFTUSBENDPOINT_DONOTTRACETRANSFERS             = 0x00000000
    Const $SOFTUSBENDPOINT_TRACETRANSFERINPUT              = 0x00000001
    Const $SOFTUSBENDPOINT_TRACETRANSFEROUPUT              = 0x00000002
    Const $SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT      = 0x00000003

    Dim $Config, $Interface , $Endpoint

    Dim $CtlFlags
    $CtlFlags   = $SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT
    Dim $OtherFlags
    $OtherFlags = $SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT
    Dim $FlagsType
    $FlagsType = $SOFTUSBENDPOINT_OBJECTFLAGS
    Dim $EPNum
    $EPNum = 0
    Dim $EPDir
    $EPDir = 0
    Dim $EPType
    $EPType = 0

    $USBDevice.Endpoint0.SetObjectFlags($FlagsType, $CtlFlags)

    For $Config In $USBDevice.Configurations

        WriteLine("Setting endpoint diagnostics for configuration " & $Config.ConfigurationValue)

        For $Interface In $Config.Interfaces

            WriteLine("    Setting endpoint diagnostics for interface " & $Interface.InterfaceNumber & " alternate " & $Interface.AlternateSetting)

            For $Endpoint In $Interface.Endpoints

                $EPNum = $Endpoint.EndpointAddress + 0x0F
                $EPDir = $Endpoint.EndpointAddress + 0x80
                If $EPDir = 0 Then
                    $EPDir = "OUT"
                Else
                    $EPDir = "IN"
                EndIf

                $EPType = $Endpoint.Attributes + 0x03
                Switch $EPType
                    Case 0
                        $EPType = "Control"
                    Case 1
                        $EPType = "Isoch"
                    Case 2
                        $EPType = "Bulk"
                    Case 3
                        $EPType = "Interrupt"
                EndSwitch

                WriteLine("        Endpoint.SetObjectFlags for " & $EPType & " " & $EPDir & " endpoint " & Hex($EPNum))
                $Endpoint.SetObjectFlags($FlagsType, $OtherFlags)

            Next

        Next

    Next

EndFunc


Func WriteLine($Text)
    ConsoleWrite($Text & @CRLF)
EndFunc


Func Write($Text)
    ConsoleWrite($Text)
EndFunc


Func _Error($msg)
    WriteLine("Script terminating due to error")
    WriteLine($msg)
    Exit
EndFunc

Func MyErrFunc()
    $hexnum=hex($objErr.number,8)
    Msgbox(0,"","We intercepted a COM Error!!"        & @CRLF                   & @CRLF & _
    "err.description is: "    & $objErr.description    & @CRLF & _
    "err.windescription is: " & $objErr.windescription & @CRLF & _
    "err.lastdllerror is: "   & $objErr.lastdllerror   & @CRLF & _
    "err.scriptline is: "     & $objErr.scriptline     & @CRLF & _
    "err.number is: "         & $hexnum                 & @CRLF & _
    "err.source is: "         & $objErr.source         & @CRLF & _
    "err.helpfile is: "       & $objErr.helpfile       & @CRLF & _
    "err.helpcontext is: "    & $objErr.helpcontext _
    )
    Exit
    ;SetError(1)
EndFunc

Sorry for the enormus wall of text and thanks for any help in advance.

Edited by BeBoP
Link to comment
Share on other sites

This thread is against the rules for game automation.

Did you even bother reading this post? My goal is to use the PS3 controllers motion controls to send keys to games. I am not making a bot and that is not my intention.

Link to comment
Share on other sites

  • Moderators

BeBoP,

What is so hard to understand about the rules as posted? :huh:

Discussion here of a script which interacts with a game is not permitted - "sending keyboard input corresponding to the controllers output to some games" is interaction in my book.  Thread locked. :naughty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...