How to cause button click event when enter is pressed in an hta application

北城以北 提交于 2020-01-06 08:24:12

问题


I have a script that allows me to hide a folder on my pc (using batch file). I've just implemented an hta application to it for the password so It's more seccure. Now the problem I'm having is that if I want to press enter to submit the form on the hta application, it just clears the password field and does nothing. I would like it to have the same effect as if I were to push the OK button. This is the hta part of my batch file:

:HTA
    <html>
    <head>
    <title>Password Entry</title>
    <hta:application id="htaid"
    applicationName="Password"
    border="thin"
    borderStyle="normal"
    caption="yes"
    contextMenu="no"
    maximizeButton="no"
    minimizeButton="yes"
    navigable="yes"
    showInTaskbar="yes"
    singleInstance="yes"
    sysmenu="yes"
    version="0.1">
    <script language="vbscript">
        window.resizeTo 210, 110
        Sub SaveBatch()
            set fs=CreateObject("Scripting.FileSystemObject")
            strFile=fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2), "UserIn.bat"))
            set ts=fs.OpenTextFile(strFile, 2, True)
            ts.WriteLine "set PASSWORD=" & document.Forms(0).elements("password").value
            ts.Close
        End Sub
    </script>
    </head>
    <body>
    <form style=margin-top:-15;>
        Password:
        <br><input type=password name=password>
        <br><input type=button language="vbscript" value="OK" onclick="SaveBatch : Window.Close">
    </form>
    <script language=vbscript>
        document.Forms(0).elements("password").focus
    </script>
    </body>
    </html>

I've tried creating a method that checks for keycode 13 onkeypress, also tried implementing it into the html portion of the password field. It either does what it did before, or causes a script error.


回答1:


Try my modification code and tell me if this did the trick or not ?

<html>
<head>
<title>Password Entry</title>
<hta:application id="htaid"
applicationName="Password"
border="thin"
icon="wlrmdr.exe"
borderStyle="normal"
caption="yes"
contextMenu="no"
maximizeButton="no"
minimizeButton="yes"
navigable="yes"
showInTaskbar="yes"
singleInstance="yes"
sysmenu="yes"
SCROLL="NO" 
SHOWINTASKBAR="Yes"   
SELECTION="no"
MINIMIZEBUTTON="no" 
>
</head>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES"> 
<BODY TOPMARGIN="1" LEFTMARGIN="1"><CENTER><DIV><SPAN ID="ONSCR"></SPAN></DIV></CENTER></BODY>
<script language="vbscript">
'---------------------------------------------------------------------------------------
Sub Window_OnLoad
    CenterWindow 250,150
    Call PasswordForm()
    Call TextFocus()
end sub
'---------------------------------------------------------------------------------------
Sub CenterWindow(x,y)
    Dim iLeft,itop
    window.resizeTo x,y
    iLeft = window.screen.availWidth/2 - x/2
    itop = window.screen.availHeight/2 - y/2
    window.moveTo ileft,itop
End Sub
'----------------------------------------------------------------------------------------
Sub SaveBatch()
    set fs=CreateObject("Scripting.FileSystemObject")
    strFile=fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2), "UserIn.bat"))
    set ts=fs.OpenTextFile(strFile,2,True)
    If PasswordArea.value <> "" Then
        ts.WriteLine "set PASSWORD="& PasswordArea.value
        ts.Close
        self.Close 'To close this HTA after saving the password as a variable into UserIn.bat
    else
        Msgbox "The password entry is empty ! "& Vbcrlf & "Please type again your passowrd",VbExclamation,"The password entry"
        Location.reload(true) 'To reload this HTA again
    end if
End Sub
'----------------------------------------------------------------------------------------
Sub PasswordForm()
    Self.document.title = "My Password Enrty"
    Self.document.bgColor = "#BBBFFF"
    ONSCR.InnerHTML="<center><FONT COLOR=""#FFFFFF"" SIZE=""+1"" FACE=""VERDANA,ARIAL,HELVETICA,SANS-SERIF"">Password Entry</FONT<br>"_
    &"<input type=""password"" name=""PasswordArea"" size=""20"" onKeyUp=""TextFocus""><P>"_
    &"<input type=""Submit"" STYLE=""HEIGHT:25;WIDTH:110"" value=""OK"" onClick=""SaveBatch"">"
END Sub
'----------------------------------------------------------------------------------------
Sub TextFocus
    PasswordArea.Focus 
End Sub
'----------------------------------------------------------------------------------------
</script>
</body>
</html>



回答2:


I have just finished writing this batch that generate a HTA BOX to hide a password in CommandLine , so i want to share it with you !

Don't forget to vote for it if you like it !

@echo off
Title Generate a HTA BOX to hide a password in CommandLine Copyright Hackoo 2014
mode con cols=80 lines=5 & color 9B
Set MyVBSFile=%tmp%\%~n0.vbs
Set MyHTAFile=%tmp%\%~n0.hta
Call :CreateMyVBS
Cscript.exe //NOLOGO %MyVBSFile%
start /wait mshta.exe "%MyHTAFile%"
Del "%MyVBSFile%" & Del "%MyHTAFile%"
for /f "tokens=*" %%i in (%tmp%\userIn) do set "Mypassword=%%i"
echo Your password is : %MyPassword%
Del %tmp%\userIn
pause 
:#Start
<html>
<head>
<title>Password Entry</title>
<hta:application id="htaid"
applicationName="Password"
border="thin"
icon="wlrmdr.exe"
borderStyle="normal"
caption="yes"
contextMenu="no"
maximizeButton="no"
minimizeButton="yes"
navigable="yes"
showInTaskbar="yes"
singleInstance="yes"
sysmenu="yes"
SCROLL="NO" 
SHOWINTASKBAR="Yes"   
SELECTION="no"
MINIMIZEBUTTON="no" 
>
</head>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES"> 
<BODY TOPMARGIN="1" LEFTMARGIN="1"><CENTER><DIV><SPAN ID="ONSCR"></SPAN></DIV></CENTER></BODY>
<script language="vbscript">
'---------------------------------------------------------------------------------------
Sub Window_OnLoad
    CenterWindow 250,150
    Call PasswordForm()
    Call TextFocus()
end sub
'---------------------------------------------------------------------------------------
Sub CenterWindow(x,y)
    Dim iLeft,itop
    window.resizeTo x,y
    iLeft = window.screen.availWidth/2 - x/2
    itop = window.screen.availHeight/2 - y/2
    window.moveTo ileft,itop
End Sub
'----------------------------------------------------------------------------------------
Sub SaveBatch()
    set fs=CreateObject("Scripting.FileSystemObject")
    strFile=fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2),"UserIn"))
    set ts=fs.OpenTextFile(strFile,2,True)
    If PasswordArea.value <> "" Then
        ts.WriteLine PasswordArea.value
        ts.Close
        self.Close 'To close this HTA after saving the password as a variable into UserIn.bat
    else
        Msgbox "The password entry is empty ! "& Vbcrlf & "Please type again your passowrd",VbExclamation,"The password entry"
        Location.reload(true) 'To reload this HTA again
    end if
End Sub
'----------------------------------------------------------------------------------------
Sub PasswordForm()
    Self.document.title = "My Password Enrty"
    Self.document.bgColor = "lightblue"
    ONSCR.InnerHTML="<center><FONT COLOR=""#FFFFFF"" SIZE=""+1"" FACE=""VERDANA,ARIAL,HELVETICA,SANS-SERIF"">Password Entry</FONT<br>"_
    &"<input type=""password"" name=""PasswordArea"" size=""20"" onKeyUp=""TextFocus""><P>"_
    &"<input type=""Submit"" STYLE=""HEIGHT:25;WIDTH:110"" value=""OK"" onClick=""SaveBatch"">"
END Sub
'----------------------------------------------------------------------------------------
Sub TextFocus
    PasswordArea.Focus 
End Sub
'----------------------------------------------------------------------------------------
</script>
</body>
</html>
:#End
::***********************************************************************************************
:CreateMyVBS
::'**********************************************************************************************
(
echo. Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo. Set f=fso.opentextfile^("%~f0",1^)
echo. a=f.readall
echo. Set r=new regexp
echo. r.pattern = "(?:^|(?:\r\n))(?::#Start\r\n)([\s\S]*?)(?:\r\n)(?::#End)"
echo. Set Matches = r.Execute^(a^)
echo. If Matches.Count ^> 0 Then Data = Matches^(0^).SubMatches^(0^)
echo. WriteFileText "%MyHTAFile%",Data
echo. f.close
::'**********************************************************************************************
echo. 
echo. Function WriteFileText^(sFile,Data^)
echo.     Dim objFSO,oTS,sText
echo.     Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo.     Set oTS = objFSO.CreateTextFile^(sFile,2^)
echo.     oTS.WriteLine Data
echo.     oTS.close
echo.     set oTS = nothing
echo.     Set objFSO = nothing
echo. End Function 
) > %MyVBSFile%
::'***********************************************************************************************


来源:https://stackoverflow.com/questions/25344960/how-to-cause-button-click-event-when-enter-is-pressed-in-an-hta-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!