iT邦幫忙

0

如何用vbs執行組合鍵

vbs

我需要在遠端發送一個指令以執行.VBS檔案,內容是執行一個組合鍵
請問 .VBS應該怎麼寫?

我搜尋結果有看到+特定字符
特殊控制键 特殊字符
Shift +
Ctrl ^
Alt %

我現在需要的是以下4個 .vbs:

  1. Ctrl+Alt +H
  2. Ctrl+Alt +Num 0
  3. Ctrl+Alt +Num 1
  4. Ctrl+Alt +Num 2

煩請協助,感恩

看更多先前的討論...收起先前的討論...
根據
這篇這篇
來看

答案可能是
WshShell.SendKeys "%^H"            'Ctrl+Alt+H
WshShell.SendKeys "%^" & chr(96)   'Ctrl+Alt+NumPad0
WshShell.SendKeys "%^" & chr(97)   'Ctrl+Alt+NumPad1
WshShell.SendKeys "%^" & chr(98)   'Ctrl+Alt+NumPad2


提供參考
做菜
原來是小寫.....

可是,改了存檔--run也沒動靜耶,怪了
Ctrl+Alt +右邊的數字鍵,目前測試不出來~暈暈暈
外獅佬 iT邦大師 1 級 ‧ 2012-09-04 11:09:07 檢舉
只好使出無雙大絕了
試試SendKeys "%^{NUMPAD0}"
外獅佬 iT邦大師 1 級 ‧ 2012-09-04 11:22:23 檢舉
筆電沒有數字鍵盤,沒辦法測試...Orz
我試著用這篇文章寫的小工具
來偵測按下的按鍵
得到NumPad 0,1,2的keycode是96,97,98

但是沒有測出
加上Ctrl/Alt的差別
臉紅
不知道要用什麼軟體
才能測出Ctrl-Alt-NumPad 0,1,2
有沒有按成功
做菜
嗯~只是想克服問題、累積經驗
我已經打算改用Ctrl-Alt-F10~F12了
Num 0-9 就是測不出來

我是因為某軟體的功能有組合鍵,但是人不在電腦前面,需要遠端執行
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
4
外獅佬
iT邦大師 1 級 ‧ 2012-09-03 15:10:22
最佳解答

可以透過Windows Script Shell物件的SendKeys去送出鍵盤上按鍵
例如:

<pre class="c" name="code">
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run("cmd")
WScript.Sleep 10000
WshShell.SendKeys "dir"
WshShell.SendKeys "{Enter}"
WScript.Quit

==========================================================
特殊按鍵:
Backspace -> {BACKSPACE} or {BKSP} or {BS}
Break -> {BREAK}
Caps Lock -> {CAPSLOCK}
Delete -> {DELETE} or {DEL}
Down Arrow -> {DOWN}
Right Arrow -> {RIGHT}
Left Arrow -> {LEFT}
Up Arrow -> {UP}
Home -> {HOME}
End -> {END}
Enter ->{ENTER} or ~
Escape -> {ESC}
Insert -> {INSERT} or {INS}
Num Lock -> {NUMLOCK}
Page Down -> {PGDN}
Page Up -> {PGUP}
Tab -> {TAB}
F1~F12 -> {F1} ~ {F12}

組合鍵:
Alt -> %
Ctrl -> ^
Shift -> +
如果要送出Ctrl + Alt + H => WshShell.SendKeys "^%H"

看更多先前的回應...收起先前的回應...

數字鍵?
0-->96
1-->97
2-->98
以下都失敗

<pre class="c" name="code">WshShell.SendKeys "^%" & CHR(96) 
WshShell.SendKeys "^%" & chr(96) 
WshShell.SendKeys "^%" & "chr(96)"
外獅佬 iT邦大師 1 級 ‧ 2012-09-03 16:13:11 檢舉

應該是
WshShell.SendKeys "^%{0}"
WshShell.SendKeys "^%{1}"
WshShell.SendKeys "^%{2}"
這樣才對

以下失敗~
WshShell.SendKeys "^%" & {chr98}...不正確字元
WshShell.SendKeys "^%" &{chr98}....不正確字元
WshShell.SendKeys "^%" & "{chr98}"..引數不正確
WshShell.SendKeys "^%" & chr{98}...不正確字元
WshShell.SendKeys "^%" & (chr98)
[/code]

外獅佬 iT邦大師 1 級 ‧ 2012-09-03 16:22:08 檢舉

哈哈...糖叔叔會錯意了
請照抄~
WshShell.SendKeys "^%{0}"
WshShell.SendKeys "^%{1}"
WshShell.SendKeys "^%{2}"

wiselou提到:
WshShell.SendKeys "^%{2}"

這樣沒錯誤,但是沒動靜耶.....

外獅佬 iT邦大師 1 級 ‧ 2012-09-03 16:28:29 檢舉

汗...奇怪...我用WshShell.SendKeys "^+{1}"會啟動我的輸入法....
因為我有設定Ctrl + Shift + 1啟動輸入法...倒

外獅佬 iT邦大師 1 級 ‧ 2012-09-03 16:31:10 檢舉

如果是用來啟動某個程式...加上一個WScript.Sleep(單位:毫秒)
等待一下可能會有效果

實測結果,還是無法做到右邊數字鍵,只好在ap上更改快速鍵

其他請參照以上組合

6
wiseguy
iT邦超人 1 級 ‧ 2012-09-03 14:59:03

都寫成一個了,麻煩 bigcandy 大大自己切分囉~

<pre class="c" name="code">Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^%H"
WshShell.SendKeys "^%0"
WshShell.SendKeys "^%1"
WshShell.SendKeys "^%2"

感謝,XP執行結果:
數字那邊不對耶,NUM 0-9 不是上面的0-9
感謝前面ok!!

6
ihon822
iT邦研究生 2 級 ‧ 2012-09-03 15:09:11
<pre class="c" name="code">
1. WshShell.SendKeys "^%H"
2. WshShell.SendKeys "^%" & CHR(96)
3. WshShell.SendKeys "^%" & CHR(97)
4. WshShell.SendKeys "^%" & CHR(98)
<pre class="c" name="code">Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^%h"

以上是wiseguy兄提供出來正常可用的

請問數字碼部份?
<pre class="c" name="code">Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^%" & CHR(96)

這樣不行耶

CHR(96) 改小寫 chr(96)也沒動靜

外獅佬 iT邦大師 1 級 ‧ 2012-09-03 16:05:56 檢舉

糖叔叔:試試SendKeys "^%{0}"
把數字用大括弧括起來

我要發表回答

立即登入回答