VB中怎样编一个自动按键盘的软件~

由于一款程序需求,须在电脑键盘上不停得点字母A键,大约0.5秒按一次~怎样在VB6.0里编一个后台按字母A键的程序~~执行后就是模拟键盘不停的按A的功能~1楼的~~不行呀... 由于一款程序需求,须在电脑键盘上不停得点字母A键,大约0.5秒按一次~
怎样在VB6.0里编一个后台按字母A键的程序~~执行后就是模拟键盘不停的按A的功能~
1楼的~~不行呀~~我的意思是让VB模拟键盘上的A键,比如让它后台运行,在Word里就每隔0.5秒出现一个A字~~
展开
 我来答
Rainbow_Route
2007-08-27 · TA获得超过646个赞
知道小有建树答主
回答量:689
采纳率:0%
帮助的人:0
展开全部
参考了前辈们的文章,得到了sendinput的一个小实例,先学习了:

这个是真正的模拟按键,跟sendkeys完全不同哦~~~注意在按下form中的按钮后,迅速切换到自己的程序中测试。

'form中放一个command1和一个timer,为了方便演示,建议放一个text1在form中

Const VK_A = 65
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
dx As Long
dy As Long
mouseData As Long
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
uMsg As Long
wParamL As Integer
wParamH As Integer
End Type
Private Type GENERALINPUT
dwType As Long
xi(0 To 23) As Byte
End Type
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub SendKey(bKey As Byte)
Dim GInput(0 To 1) As GENERALINPUT
Dim KInput As KEYBDINPUT
KInput.wVk = bKey 'the key we're going to press
KInput.dwFlags = 0 'press the key
'copy the structure into the input array's buffer.
GInput(0).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
'do the same as above, but for releasing the key
KInput.wVk = bKey ' the key we're going to realease
KInput.dwFlags = KEYEVENTF_KEYUP ' release the key
GInput(1).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
'send the input now
Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub

Private Sub Command1_Click()
Timer1.Enabled = Not (Timer1.Enabled)
End Sub

Private Sub Form_Load()
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
SendKey VK_A
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2007-08-27
展开全部
参考了前辈们的文章,得到了sendinput的一个小实例,先学习了:

这个是真正的模拟按键,跟sendkeys完全不同哦~~~注意在按下form中的按钮后,迅速切换到自己的程序中测试。

'form中放一个command1和一个timer,为了方便演示,建议放一个text1在form中

Const VK_A = 65
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
dx As Long
dy As Long
mouseData As Long
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
wVk As Integer
wScan As Integer
dwFlags As Long
time As Long
dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
uMsg As Long
wParamL As Integer
wParamH As Integer
End Type
Private Type GENERALINPUT
dwType As Long
xi(0 To 23) As Byte
End Type
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub SendKey(bKey As Byte)
Dim GInput(0 To 1) As GENERALINPUT
Dim KInput As KEYBDINPUT
KInput.wVk = bKey 'the key we're going to press
KInput.dwFlags = 0 'press the key
'copy the structure into the input array's buffer.
GInput(0).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(0).xi(0), KInput, Len(KInput)
'do the same as above, but for releasing the key
KInput.wVk = bKey ' the key we're going to realease
KInput.dwFlags = KEYEVENTF_KEYUP ' release the key
GInput(1).dwType = INPUT_KEYBOARD ' keyboard input
CopyMemory GInput(1).xi(0), KInput, Len(KInput)
'send the input now
Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub

Private Sub Command1_Click()
Timer1.Enabled = Not (Timer1.Enabled)
End Sub

Private Sub Timer1_Timer()
SendKey VK_A
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
无条件为您
2007-08-28 · TA获得超过112个赞
知道小有建树答主
回答量:145
采纳率:0%
帮助的人:45.4万
展开全部
先放一个定时器控件,设置它为500毫秒(0.5秒)激发一次。然后写它的事件为:

keybd_event(65,0,0,0);
keybd_event(65,0,KEYEVENTF_KEYUP,0);

编译成EXE运行,这样就是你要的效果了。给分吧!
很简单哦。最小化也照样可以用。不过你最好是学发消息和句柄控制,那样不用按键也能达到按键的效果。

-----------------

如果用sendkeys "A" 的话也行,但不能模拟Ctrl+ESC键,换句话来说:这种方法兼容性不好。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
在世贸天阶灌篮的高飞燕草
2007-08-27 · TA获得超过2378个赞
知道大有可为答主
回答量:2070
采纳率:0%
帮助的人:2028万
展开全部
'添加1个计时器就可以了
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const KEYEVENTF_KEYUP = &H2
Private Sub Form_Load()
Timer1.Interval = 500
End Sub

Private Sub Timer1_Timer()
keybd_event 97, 0, 0, 0
keybd_event 97, 0, KEYEVENTF_KEYUP, 0
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友7874b69
2007-08-27 · TA获得超过545个赞
知道小有建树答主
回答量:463
采纳率:0%
帮助的人:397万
展开全部
上面的太麻烦了,VB有SendKeys方法,可以模拟按键
用一个Timer控件,并设置时间间隔为500毫秒(就是timer1.interval=500)
Private Sub Timer1_Timer()
sendkeys "A"
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(9)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式