VB.Net:向远程桌面应用程序发送虚拟击键

时间:2015-05-20 15:34:01

标签: vb.net

我正在尝试使用VB.net和MS Visual Studios构建屏幕上的数字小键盘。我有一个工作原型,在我的本地机器上工作得很好,除非在Windows RDC应用程序中发送键盘到活动表格。我尝试过使用SendKeys()和keybd_event的解决方案,这两个解决方案都遇到了相同的障碍。到目前为止,我的研究表明,虽然SendKeys有一些限制,但当与正确的键盘扫描代码一起使用时,SendMessage和SendInput API都应该与Windows RDP应用程序一起使用;但是我在代码中实现这些API并且搜索得很远广泛的工作示例代码,但到目前为止都没有成功。

有人可以提供一些示例代码,概述如何在VB.Net / Visual Studios中实现这些API函数。

下面我提供了一些示例代码,它可以与主机PC上的大多数应用程序配合使用,但不适用于Windows RDC软件。

声明十六进制变量

localStorage

防止屏幕键盘窃取焦点

Private Const WM_NCLBUTTONDOWN As Int32 = &HA1
Private Const WM_NCMOUSEMOVE As Int32 = &HA0
Private Const WS_EX_NOACTIVATE As Int32 = &H8000000
Private Const KEYEVENTF_KEYDOWN As Integer = &H0
Private Const KEYEVENTF_KEYUP As Integer = &H2 
Private Const VK_SPACE As Integer = &H20
Private Const WM_KEYDOWN As Int32 = &H100
Private Const WM_KEYUP As Int32 = &H101
Private Const WM_CHAR = &H102

从" user32"

导入keybd_event函数
Protected Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = cp.ExStyle Or CInt(Fix(WS_EX_NOACTIVATE))
        Return cp
    End Get
End Property

Dim previousForegroundWindow As IntPtr
Protected Overrides Sub WndProc(ByRef m As Message)

    Dim foregroundWindow As IntPtr
    Select Case m.Msg
        Case WM_NCLBUTTONDOWN

            foregroundWindow = GetForegroundWindow()

            If foregroundWindow = Me.Handle Then
                SetForegroundWindow(Me.Handle)

                If foregroundWindow = IntPtr.Zero Then
                    previousForegroundWindow = foregroundWindow
                End If
            End If

        Case WM_NCMOUSEMOVE

            If IsWindow(previousForegroundWindow) Then
                SetForegroundWindow(previousForegroundWindow)
            End If

        Case Else
    End Select

    MyBase.WndProc(m)
End Sub

单击标签时将关键事件发送到窗口/窗体

Declare Function keybd_event Lib "user32" Alias "keybd_event" _
    (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer,
     ByVal dwExtraInfo As Integer) As Integer 

0 个答案:

没有答案