| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 833 人关注过本帖
标题:模拟按键的问题
只看楼主 加入收藏
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
模拟按键的问题
这是一段模拟按键的代码,不知道为什么要把字符转换成大写的(strKey = UCase(strKey)),我试了下,传"ceshishi"给strKey,它模拟的结果却是"cceshisshi",搞不懂,请大家帮忙解答。
   
   
   
Public Function QQ_AutoPressKey(hWnd As Long, strKey As String)
    Dim nLength As Long, VKey As Long, i As Long
    strKey = UCase(strKey)        
    nLength = Len(strKey)
    For i = 1 To nLength
        VKey = Asc(Mid(strKey, i, 1))
        Call AutoPressKey(VKey)
    Next
End Function
Public Function AutoPressKey(VKey As Long)
    keybd_event VKey, 0, 0, 0 '模拟键按下
    keybd_event VKey, 0, KEYEVENTF_KEYUP, 0 '模拟键弹起
End Function
   
   
   
搜索更多相关主题的帖子: 模拟 
2010-06-09 17:37
xiaomarn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:348
专家分:2026
注 册:2009-3-18
收藏
得分:20 
Option Explicit
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 KEYEVENTF_KEYUP = &H2


Private Function AutoPressKey(vKey As Byte)
    keybd_event vKey, 0, 0, 0 '模拟键按下
    'MsgBox "hello"
    keybd_event vKey, 0, KEYEVENTF_KEYUP, 0 '模拟键弹起
End Function


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Print KeyCode, Chr(KeyCode)
End Sub

Private Sub Form_Load()
    Dim vKey() As Byte, i As Integer
    Dim s1 As String
   
    Me.Show
    s1 = "hello"
    s1 = UCase(s1)
    vKey = StrConv(s1, vbFromUnicode)
    For i = 0 To 4
        'Debug.Print vKey(i)
        AutoPressKey (vKey(i))
    Next
End Sub
你模拟的是按键,发给电脑的当然是扫描码,为大写字母的ascii码
keybd_event函数的第一个参数vkey是byte型,我用了个字符类型转换,转成dbcs+sbcs型
2010-06-10 12:51
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
收藏
得分:0 
哦,原来是这样,谢谢xiaomarn。
2010-06-10 18:10
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
收藏
得分:0 
额,借鉴这为大侠的代码放到我程序里,结果还是错的,看来是别的地方也有问题。
2010-06-10 18:12
xiaomarn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:348
专家分:2026
注 册:2009-3-18
收藏
得分:0 
Option Explicit

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 KEYEVENTF_KEYUP = &H2

Private Function QQ_AutoPressKey(hWnd As Long, strKey As String)
    Dim vkey() As Byte, i As Integer
    'strKey = UCase(strKey)
    vkey = StrConv(strKey, vbFromUnicode)
    For i = 0 To UBound(vkey)
        Call AutoPressKey(vkey(i))
    Next
End Function
Private Function AutoPressKey(vkey As Byte)
    keybd_event vkey, 0, 0, 0 '模拟键按下
    'Debug.Print "yes", vkey, Chr(vkey)
    keybd_event vkey, 0, KEYEVENTF_KEYUP, 0 '模拟键弹起
End Function

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Print "no", KeyCode, Chr(KeyCode)
End Sub

Private Sub Form_Load()
    Dim str As String
    Me.Show
    str = "ceshishi"
    Call QQ_AutoPressKey(Form1.hWnd, str)
End Sub

那个窗体句柄是拿来干嘛的?根本没用到
正在学api,不过不是用vb,建议不要用keybd_event 了,换个函数
我也是菜鸟一个
2010-06-12 18:11
快速回复:模拟按键的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024662 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved