用VB 向指定的程序窗口发键盘事件该如何编写?
比如CS里任务前后左右移动但是切换到别的窗口,按键仍然有效,如何把这个模拟的键盘事件只发给这个窗口.谢谢你的回答,但是可不可以再详细点.还是没明白^APPACTIVATE...
比如CS 里 任务 前后左右 移动
但是切换到别的窗口 ,按键仍然有效,如何把这个模拟的键盘事件 只发给 这个窗口.
谢谢你的 回答,但是 可不可以再详细点.
还是没明白^ APPACTIVATE 这个只是激活窗口到最顶层了, 如果这个窗口在后台,模拟的事件还是 在当前的窗口上了…… 展开
但是切换到别的窗口 ,按键仍然有效,如何把这个模拟的键盘事件 只发给 这个窗口.
谢谢你的 回答,但是 可不可以再详细点.
还是没明白^ APPACTIVATE 这个只是激活窗口到最顶层了, 如果这个窗口在后台,模拟的事件还是 在当前的窗口上了…… 展开
3个回答
展开全部
以下方法供参考
方法1: 用 SendKeys 语句
AppActivate "接收键盘事件的窗口标题" ' 激活指定窗口
SendKeys "A", True '发送字符 A
方法2: 用 API 函数
这个方法实现起来较复杂,给你一个思路:
用 FindWindow 查找特定标题的窗口,获得该窗口的句柄,用 SendMessage 向该窗口发送键盘消息。
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
方法1: 用 SendKeys 语句
AppActivate "接收键盘事件的窗口标题" ' 激活指定窗口
SendKeys "A", True '发送字符 A
方法2: 用 API 函数
这个方法实现起来较复杂,给你一个思路:
用 FindWindow 查找特定标题的窗口,获得该窗口的句柄,用 SendMessage 向该窗口发送键盘消息。
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
展开全部
Option Explicit
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const WM_SETFOCUS = &H7
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Sub KeyDown(ByVal vKey As Long) '用keybd_event模拟按下键盘
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY, 0
End Sub
Private Sub KeyUp(ByVal vKey As KeyCodeConstants) '用keybd_event模拟松开键盘
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub
Private Function SendKeyToWnd(MainWnd As Long, vbkey, Shift) As Boolean
SetForegroundWindow MainWnd
SendMessage MainWnd, WM_SETFOCUS, 0&, 0& '选中按钮
If Shift Then KeyDown Shift
KeyDown vbkey
Sleep 100
KeyUp vbkey
If Shift Then KeyUp Shift
End Function
Private Sub Form_Load()
'向指定的程序窗口发键盘事件
SendKeyToWnd 目标窗口的hwnd, 命令, 复合键
End Sub
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const WM_SETFOCUS = &H7
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Sub KeyDown(ByVal vKey As Long) '用keybd_event模拟按下键盘
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY, 0
End Sub
Private Sub KeyUp(ByVal vKey As KeyCodeConstants) '用keybd_event模拟松开键盘
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub
Private Function SendKeyToWnd(MainWnd As Long, vbkey, Shift) As Boolean
SetForegroundWindow MainWnd
SendMessage MainWnd, WM_SETFOCUS, 0&, 0& '选中按钮
If Shift Then KeyDown Shift
KeyDown vbkey
Sleep 100
KeyUp vbkey
If Shift Then KeyUp Shift
End Function
Private Sub Form_Load()
'向指定的程序窗口发键盘事件
SendKeyToWnd 目标窗口的hwnd, 命令, 复合键
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建一个Command1。我以计算器为例。
代码如下。
============
Private Sub Command1_Click()
Dim t As Long
t = Shell("calc")
AppActivate t
SendKeys "123"
End Sub
代码如下。
============
Private Sub Command1_Click()
Dim t As Long
t = Shell("calc")
AppActivate t
SendKeys "123"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询