2013-04-25 48 views
0

我想在按下F1键后终止程序。不知道如何编写do while while循环。 任何想法?Visual Basic脚本 - KeyPress检测?

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

我不相信这是可能使用VBScript – seeker 2013-04-25 06:28:53

回答

2

这不是简单的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

我使用混合VBS脚本嵌入在一个小的C#程序它使用adodb.stream。

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

您可以删除清理线,并将.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