- 帖子
- 523
- 主題
- 56
- 精華
- 0
- 積分
- 601
- 點名
- 86
- 作業系統
- win 10
- 軟體版本
- []
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2013-3-19
- 最後登錄
- 2025-4-13
           
|
7#
發表於 2016-9-27 12:36
| 只看該作者
回復 6# jackyq
回復 3# PKKO
當然 sendkeys 很作怪
它不僅影響 numlock, capslock, scroll, 還會莫名其妙跑出定義名稱參照哪一個位置!!
#3樓 提到 https://support.microsoft.com/zh-tw/kb/179987
這份文件的解決方案有3, 前2個試了都不行
再試第3個很迂迴的方案如下,好像可以穩定的解決問題 (win10)
Option Explicit
' API declarations:
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
' Constant declarations:
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_KEYUP = &H2
'https://support.microsoft.com/zh-tw/kb/179987
'Resolution:
'Determine the setting of the NumLock key prior to using SendKeys.
'Then, turn off the NumLock before using SendKeys.
'After using SendKeys,reset the NumLock to its previous setting.
'This is accomplished using the GetKeyboardState, keybd_event and SetKeyboardState API functions.
'================================================================================================
Private Sub test() '測試 sendkeys
presetNumlock
SendKeys "^g^a{DEL}" ', True
TurnNumlock
End Sub
Sub presetNumlock()
Dim bKeys(0 To 255) As Byte
Dim LockState%
'Get status of the 256 virtual keys
GetKeyboardState bKeys(0)
'turn off the num_lock key
If bKeys(vbKeyNumlock) = 1 Then
TurnNumlock
End If
DoEvents '一定要 ?
End Sub
Sub TurnNumlock()
'Simulate Key Press
keybd_event vbKeyNumlock, 1, 0, 0
'Simulate Key Release
keybd_event vbKeyNumlock, 1, KEYEVENTF_KEYUP, 0
End Sub |
|