Visual Basic脚本 - KeyPress检测?

时间:2013-04-25 06:02:06

标签: vbscript keyboard detection

我想在按下F1键后终止程序。 不确定如何编写do while循环。 任何想法?

Set WshShell = WScript.CreateObject("WScript.Shell")
Do While {F1} is not pressed
'...
Loop

2 个答案:

答案 0 :(得分:3)

这在普通的VBScript中是不可能的,但您可以使用HTA来使用它:

<head>
<title>Test</title>
<HTA:APPLICATION ID="oHTA"
  APPLICATIONNAME="Test"
>
</head>

<script language="VBScript">
Sub CheckKey
  If window.event.keyCode = 112 Then self.close()
End Sub
</script>

<body onKeyUp="CheckKey">
...
</body>

答案 1 :(得分:1)

我使用混合VBS脚本,其中使用adodb.stream嵌入了一个小型C#程序。

此示例使用每个步骤更新剪贴板(例如,用于登录),但可以轻松修改它以触发其他事件。

您可以删除清理行,并且.exe​​可以放在临时目录中。

为简洁起见,hex_部分已被截断,但整个文件在http://excelxll.com/VBS/keypress.txt处可用

Dim wsh, exe, HexKeyCode
Set wsh = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

HexKeyCode = "70" 'F1=70 F12=7B ALT-GR=A5 ESC=1B
exe = "Press F1 to step script until I disappear.exe"

if not fso.FileExists(exe) then call create_binary_file_

'EACH TIME F1 IS PRESSED UPDATE THE CLIPBOARD 
'############################################
wsh.Run "cmd.exe /c """ & exe  & """ " & HexKeyCode, 0, TRUE
wsh.Run "cmd.exe /c echo 1| clip", 0, TRUE

wsh.Run "cmd.exe /c """ & exe  & """ " & HexKeyCode, 0, TRUE
wsh.Run "cmd.exe /c echo 2| clip", 0, TRUE

'OPTIONAL TIDY-UP
'################
fso.DeleteFile exe


sub create_binary_file_()
'########################
hex_="4D5A90...0000"

'FOR THE BINARY FILE WRITE
'#########################
dim str
set str = WScript.CreateObject("adodb.stream") 
str.type = 2 
str.charset = "iso-8859-1" 
str.open

for x = 1 to len(hex_) step 2

high_ = asc(mid(hex_,x,1))
    if high_ < 58 then 
        high_ = (high_-48)*16
    else
        high_ = (high_-55)*16
    end if

low_ = asc(mid(hex_,x+1,1))
    if low_ < 58 then 
        low_ = (low_-48)
    else
        low_ = (low_-55)
    end if

str.writeText(chrW(high_ + low_))
next

str.saveToFile exe, 2 
str.close 

end sub