'Modified by Guidance (http://citymap.126.com) 2002-12-22

'From:  "Gregg Morrison" <greggmor@b...> Date:  Sat Aug 31, 2002  5:24 am
' the API way to send keys to any window, so here it is:
'-----------------
'Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd _
'As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Declare Sub keybd_event Lib "user32" alias "keybd_event" (ByVal bVk As Byte, ByVal bScan _
As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
' This is cross-application SendKeys

'Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Const KEYEVENTF_KEYUP = &H2 ' Release key
Const WM_SETFOCUS = &H7& ' &h0007
Const WM_KILLFOCUS = &H8& '&h0008


' set focus to any displayed window using hWnd = WindowHandle
SendMessage hWnd, WM_SETFOCUS, 0&, 0&


' Send a string of keys with optional keypress delay
' This simulates pressing an releasing each key in the Byte Array
'bytKetStack()

Sub SendKeysAPI(bytKeystack() As Byte, _ 'Optional 
DelayMS As Long)' = 0)
Dim nKeyCnt As Long

For nKeyCnt = LBound(bytKeystack) To UBound(bytKeystack)
keybd_event bytKeystack(nKeyCnt), 0, 0, 0 ' Simulate KeyPress
DoEvents
' let Windows Process Message
If DelayMS Then Sleep DelayMS ' Delay - 0, 5, 10 milliseconds if needed

' Simulate KeyRelease
keybd_event bytKeystack(nKeyCnt), 0, KEYEVENTF_KEYUP, 0
DoEvents
' Let Windows Process Message
Next nKeyCnt

End Sub


' Here is an example using control keys
defBYTE vbKeyControl = 17 ' CTRL key
defBYTE vbKeyEscape = 27 ' ESC key

Sub ShowStartMenu()

' Press the Ctrl-Esc key - Same as the 'Windows' key
keybd_event vbKeyControl, 0, 0, 0
keybd_event vbKeyEscape, 0, 0, 0
DoEvents

' Release the two keys
keybd_event vbKeyControl, 0, KEYEVENTF_KEYUP, 0
keybd_event vbKeyEscape, 0, KEYEVENTF_KEYUP, 0
DoEvents

End Sub