Jump to content

Why does this script drop the first character?


Recommended Posts

With the help of many helpful people here, I've been working on a script that pastes the contents of the Windows clipboard into the DOSBox MS-DOS emulator.

It seems to work fairly well, except that, for reasons I don't understand, the script usually drops the first character when pasting into DOSBox.

Here is the main part of the code. If DOSBox is open and active, when you press the Pause key, the contents of the Windows clipboard should be typed into DOSBox. As you see, it types in the tab and break codes at ASCII 9-13, the ordinary ASCII characters from 32 through 126 (I suppose that could be 127), and then types Alt-NumberPad### to enter in the higher-level characters.

What I don't understand is why the first character in the clipboard usually gets dropped when pasting. Any insight would be very welcome.

; Paste Windows clipboard into DOSBox
#include <WinAPI.au3>
$procname = "DOSBox.exe"
HotKeySet("{PAUSE}", "ClipboardPaste")
While 1
Sleep(500)
WEnd
Func ClipboardPaste()
If ProcessExists($procname) Then
If WinExists("DOSBox SVN") Then
$appwin = "DOSBox SVN"
ElseIf WinExists("DOSBox 0.7") Then
$appwin = "DOSBox 0.7"
Else
$appwin = ""
EndIf
If $appwin <> "" Then
$textto = ClipGet() ;; " " & ClipGet() seems to work
$iCodePage = 1 ;; 1 uses OEM string of current system
$clipUnicode = _WinAPI_WideCharToMultiByte($textto, $iCodePage, True)
; $clipUnicode = StringTrimRight($clipUnicode, 1)
$textto = $clipUnicode
If @error Then
$textto = " "
EndIf
If WinActive($appwin) Then
$inputArray = StringToASCIIArray($textto, 0, StringLen($textto), 1)
For $i = 0 To UBound($inputArray) - 1
     Switch $inputArray[$i]
     Case 9 To 13
     Sleep(5)
     If WinActive($appwin) Then
     Send(Chr($inputArray[$i]))
     EndIf
     Case 32 To 126
     Sleep(5)
     If WinActive($appwin) Then
     Send(Chr($inputArray[$i]), 1) ;; send raw, so +, !, ^ not translated to modifiers
     EndIf
     Case 128 To 255
     Sleep(5)
     $temp = String($inputArray[$i])
     If WinActive($appwin) Then
     Send("{ALTDOWN}")
     For $j = 1 To 3
         Send("{NUMPAD" & StringMid($temp, $j, 1) & "}")
     Next
     Send("{ALTUP}")
     EndIf
     Case Else
     Sleep(5)
     EndSwitch
Next
EndIf
EndIf
EndIf
EndFunc ;==>ClipboardPaste
Edited by emendelson
Link to comment
Share on other sites

I've not used StringMid before, but just looking at the help file it would appear that if $j is 2 or 3, then you can lose the first and/or second character in your string. But you do say that it "usually" drops the first character, which means that it doesn't always? Can you replicate a working and non-working condition for each case?

Link to comment
Share on other sites

I've not used StringMid before, but just looking at the help file it would appear that if $j is 2 or 3, then you can lose the first and/or second character in your string. But you do say that it "usually" drops the first character, which means that it doesn't always? Can you replicate a working and non-working condition for each case?

The StringMid part of the code only gets used when the character is an upper ASCII character like é or ç and those characters aren't present in the tests I've done.

I can't reliably create a situation where the first character is consistently dropped, but I've spent much of the morning trying to do exactly that. For an hour so so, the first character got dropped. Then it got fixed. I rebooted a few times, but that had no effect. Very puzzling.

Link to comment
Share on other sites

Maybe it's a timing issue. Try adding a Sleep(500) before starting to send the text (i.e. before the "For $i..." loop).

I think you figured it out - a delay seems to have sorted it out, and I'm not seeing that problem now.

Thank you (again - also for your help on other questions recently!).

Link to comment
Share on other sites

I don't know why you use _WinAPI_WideCharToMultiByte()

I use _WinAPI_WideCharToMultiByte() because, if I did NOT use it, the string at the top of the image would get pasted into DOSBox as the string at the bottom of the image.

Posted Image

Unfortunately, this shows that the first character is getting dropped again (the "a" isn't pasted). Back to work..!

Link to comment
Share on other sites

Is there a way to paste into DOSbox? If so could you load the text into a variable, load the variable into the clipboard and paste it in?

There is no way to paste into DOSBox. It doesn't communicate with the clipboard on any platform (Mac, Windows, Linux). That's why I've been working on this script and scratching my head over this problem.

Link to comment
Share on other sites

It seems that the problem is in DOSBox, which tends to drop the first character typed into it by AutoIt. I've worked around it by adding a space character to the start of the clipboard string. It's not perfect, but it seems to work. Thanks to everyone for suggestions and help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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