【问题标题】:How to cause button click event when enter is pressed in an hta application如何在 hta 应用程序中按下回车键时触发按钮单击事件
【发布时间】:2014-08-16 23:29:13
【问题描述】:

我有一个脚本可以让我在我的电脑上隐藏一个文件夹(使用批处理文件)。我刚刚为它实现了一个用于密码的 hta 应用程序,所以它更安全。现在我遇到的问题是,如果我想按 Enter 键在 hta 应用程序上提交表单,它只会清除密码字段并且什么都不做。我希望它具有与按下 OK 按钮相同的效果。这是我的批处理文件的 hta 部分:

: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>

我尝试创建一个检查键码 13 onkeypress 的方法,还尝试将其实现到密码字段的 html 部分。它要么执行以前的操作,要么导致脚本错误。

【问题讨论】:

    标签: batch-file button hta enter onkeypress


    【解决方案1】:

    试试我的修改代码,告诉我这是否成功?

    <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>
    

    【讨论】:

    • 我复制并粘贴了您的代码并且它可以工作,现在我正试图弄清楚当您的代码按 Enter 时它是如何工作的。有 onkeypress 或 onkeydown 事件处理,所以我很无能哈哈
    • 代码很简单;如果你看它;它向您展示了如何使用 InnerHTML 方法创建密码表单,我们使用类型 Submit 而不是类型按钮,因此当您按 Enter 时,他提交您的输入并执行 sub SaveBatch() 并使用 Self 关闭 HTA。 Close 或 Window.Close 你可以看看这个例子 Matrix Login + Simulation DOS ==> pastebin.com/NcK5nkFV
    【解决方案2】:

    我刚刚完成了这批生成 HTA BOX 以在命令行中隐藏密码的批处理,所以我想与你分享!

    喜欢的话别忘了投票哦!

    @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%
    ::'***********************************************************************************************
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多