' sendkeys.vbs
'
' Script "types" the contents of file into the window that currently
' has focus.
'
' Copyright (C) 2009 Ryan O'Horo www.cravediy.com
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'

' This delay can be adjusted to account for network latency for remote
' terminal connections. Should be at least 1 (nasty things happen at 0).
keyDelay = 1

Set wshshell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Set fileIn = fso.OpenTextFile("hello.eml")

' Give us an opportunity to give the focus to the target window.
wscript.sleep 2000

Do Until fileIn.AtEndOfStream

	tmp = fileIn.Read(1)

	' We need to ignore the LF character of the Windows CR/LF newline
	' sequence to prevent double linebreaks
	If tmp <> Chr(10) Then

		' We have to escape some characters that are special for the
		' SendKeys function.
		If tmp = Chr(9) Then tmp = "{TAB}"
		If tmp = Chr(33) Then tmp = "{!}"
		If tmp = Chr(40) Then tmp = "{(}"
		If tmp = Chr(41) Then tmp = "{)}"
		If tmp = Chr(43) Then tmp = "{+}"
		If tmp = Chr(94) Then tmp = "{^}"
		If tmp = Chr(123) Then tmp = "{{}"
		If tmp = Chr(125) Then tmp = "{}}"
		If tmp = Chr(126) Then tmp = "{~}"

		wscript.Sleep keyDelay
		wshshell.SendKeys tmp

	End If
Loop