SendInput VB基本示例 [英] SendInput VB Basic Example

查看:342
本文介绍了SendInput VB基本示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能提供帮助,我试图找到一个模拟键盘命令的SendInput代码示例,希望找到记事本窗口并输入测试消息.

I hope someone can assist, I trying to find an example of a SendInput code that simulate keyboard commands, I wish to find the notepad window and enter a test message.

我最初在正在处理的项目中使用SendKeys,SendKeys功能使我能够将键盘命令转发到我们在工作场所使用的定制软件.

I had initially used SendKeys on a project I am working on, the SendKeys function enabled me to forward keyboard commands to a bespoke software that we are use at our workplace.

我希望有人能提供帮助,互联网上的示例似乎无效.

I hope someone can help, the examples on the internet does not seem to work.

任何人还可以告知SendInput方法是否是侵入性的,即是否会对接收方窗口造成任何损害.

Can anyone also advise if the SendInput method is intrusive, i.e. will it cause any damage to the recipient window.

SendKey方法有效,但是可靠性似乎很容易碰到.

The SendKey method worked, however the reliability seems to be very hit and miss.

非常感谢

萨拉

我在互联网上找到了以下代码,以下是SendInput方法吗?我注意到使用了"SendKey"这个词吗?

I have found the following code on the internet, is the following a SendInput method? As I notice that term 'SendKey' is used?

Private Declare Function SendInput Lib "user32.dll" _
 (ByVal nInputs As Long, ByRef pInputs As Any, _
 ByVal cbSize As Long) As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" _
 (ByVal cChar As Byte) As Integer

Private Type KeyboardInput      '   typedef struct tagINPUT {
 dwType As Long                '     DWORD type;
 wVK As Integer                '     union {MOUSEINPUT mi;
 wScan As Integer              '               KEYBDINPUT ki;
 dwFlags As Long               '               HARDWAREINPUT hi;
 dwTime As Long                '              };
 dwExtraInfo As Long           '     }INPUT, *PINPUT;
 dwPadding As Currency         '   8 extra bytes, because mouses take more.
End Type

Private Const INPUT_MOUSE As Long = 0
Private Const INPUT_KEYBOARD As Long = 1
Private Const KEYEVENTF_KEYUP As Long = 2
Private Const VK_LSHIFT = &HA0

Public Sub SendKey(ByVal Data As String)
Dim ki() As KeyboardInput
Dim i As Long
Dim o As Long ' output buffer position
Dim c As String ' character

ReDim ki(1 To Len(Data) * 4) As KeyboardInput
o = 1

For i = 1 To Len(Data)
 c = Mid$(Data, i, 1)
 Select Case c
   Case "A" To "Z": ' upper case
     ki(o).dwType = INPUT_KEYBOARD 'shift down
     ki(o).wVK = VK_LSHIFT
     ki(o + 1) = ki(o) ' key down
     ki(o + 1).wVK = VkKeyScan(Asc(c))
     ki(o + 2) = ki(o + 1) ' key up
     ki(o + 2).dwFlags = KEYEVENTF_KEYUP
     ki(o + 3) = ki(o) ' shift up
     ki(o + 3).dwFlags = KEYEVENTF_KEYUP
     o = o + 4
   Case Else: ' lower case
     ki(o).dwType = INPUT_KEYBOARD
     ki(o).wVK = VkKeyScan(Asc(c))
     ki(o + 1) = ki(o)
     ki(o + 1).dwFlags = KEYEVENTF_KEYUP
     o = o + 2
 End Select
Next i

Debug.Print SendInput(o - 1, ki(1), LenB(ki(1))),
Debug.Print Err.LastDllError
End Sub

Private Sub Command1_Click()
Text1.Text = ""
Text1.SetFocus
DoEvents
Call SendKey("This Is A Test")
End Sub

推荐答案

以下代码不是针对VB.net的,而是针对VB/VBA的,其类似于sendkeys方法,但可能会更加可靠,因为它专门将密钥发送给目标应用程序. (我得到它的帖子也显示了sendkeys方法)

The following code is not for VB.net but VB/VBA, its similar to the sendkeys method but probably a little more reliable as it sends the keys specifically to the target application. (the post where i got it shows the sendkeys method too)

Public Declare Function FindWindowX Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, ByVal lpsz1 As Long, ByVal lpsz2 As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Integer) As Long

Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101

Sub Three()
    hWind = FindWindow(vbNullString, "Untitled - Notepad")
    cWind = FindWindowX(hWind, 0, 0, 0)
    Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyA, 0)
    Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyB, 0)
    Debug.Print PostMessage(cWind, WM_KEYDOWN, vbKeyC, 0)
End Sub

代码取自如果将其粘贴到Excel/VBA中的新模块中并运行新的记事本实例,则在执行子项时,记事本中应显示"abc".

If you paste this into a new module in Excel/VBA and have an new instance of notepad running, when the sub is executed "abc" should appear in notepad.

我不知道如何使用它,或者sendkeys方法会损坏"目标窗口.只要您正确安排消息的时间(不要同时向窗口发送大量字符),就不会造成任何问题.

I don't see how using this, or the sendkeys method could "damage" the target window. So long as you time the messages properly (not sending tonnes of characters to the window all at the same time) it shouldn't cause any problems.

这篇关于SendInput VB基本示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆