|
6#
楼主 |
发表于 2012-1-8 05:17:00
|
只看该作者
gotfocus我用了,不行的。
Option Compare Database
Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
'Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_CHAR = &H102
Public Const VK_A = &H41
Public Const VK_RIGHT = &H27
Public Const VK_RETURN = &HD
Public Const VK_END = &H23
Public Const VK_0 = &H30
Public Const VK_1 = &H31
Public Const VK_2 = &H32
Public Const VK_3 = &H33
Public Const VK_4 = &H34
Public Const VK_5 = &H35
Public Const VK_6 = &H36
Public Const VK_7 = &H37
Public Const VK_8 = &H38
Public Const VK_9 = &H39
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Public Function GetHwnd() As Long
' Dim Hwnd As Long
' Dim PID As Long
' Dim TID As Long
' Dim hWndFocus As Long
'
' Hwnd = GetForegroundWindow
'
' If Hwnd Then
'TID = GetWindowThreadProcessId(Hwnd, PID)
'AttachThreadInput App.ThreadID, TID, True
'
GetHwnd = GetFocus
'AttachThreadInput App.ThreadID, TID, False
' End If
'
End Function
Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
'参数VirtualKey表示按键虚拟码,flag表示是按下键还是释放键,用WM_KEYDOWN和WM_KEYUP这两个常数表示
Dim s As String
Dim Firstbyte As String 'lparam参数的24-31位
If flag = WM_KEYDOWN Then '如果是按下键
Firstbyte = "00 "
Else
Firstbyte = "C0 " '如果是释放键
End If
Dim Scancode As Long '获得键的扫描码
Scancode = MapVirtualKey(VirtualKey, 0)
Dim Secondbyte As String 'lparam参数的16-23位,即虚拟键扫描码
Secondbyte = Right("00 " & Hex(Scancode), 2)
s = Firstbyte & Secondbyte & "0001 " '0001为lparam参数的0-15位,即发送次数和其它扩展信息
MakeKeyLparam = Val("&H " & s)
End Function
|
|