Send input to Keyboard stream

Dec 16, 2011 at 9:33 PM

Hello,

I have an external application that is hooking into the Windows keyboard input stream.

I want to be able to send message into this stream. Can I do this with input simulator? Currently, the input is going to whatever the foreground window is ?

Thanks.

Coordinator
Dec 18, 2011 at 9:33 PM
Hi There,

The InputSimulator is just a lightweight .NET friendly wrapper for the win32 SendInput API which targets the currently active window. Best thing I can recommend is diving into the MSDN documentation and experimenting yourself.

Good luck,
Mike

On Sat, Dec 17, 2011 at 8:33 AM, nitinsobti <notifications@codeplex.com> wrote:

From: nitinsobti

Hello,

I have an external application that is hooking into the Windows keyboard input stream.

I want to be able to send message into this stream. Can I do this with input simulator? Currently, the input is going to whatever the foreground window is ?

Thanks.

Read the full discussion online.

To add a post to this discussion, reply to this email (inputsimulator@discussions.codeplex.com)

To start a new discussion for this project, email inputsimulator@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Jan 25, 2012 at 2:09 PM
nitinsobti wrote:

Hello,

I have an external application that is hooking into the Windows keyboard input stream.

I want to be able to send message into this stream. Can I do this with input simulator? Currently, the input is going to whatever the foreground window is ?

Thanks.


Sorry for the late reply, but hopefully this will help someone with similar problems, even if only in theory and design. I had the same problem with keypresses always going to my own app instead of the app I wanted to reach. What I did was add each keypress to a queue (hidden combobox, but could probably use list or array) and use the appactivate (function?) to grab the app window I wanted before each keypress. This way if they interrupted application with multiple clicks it would wait until it had focus again before pressing another key (focus pass/fail). The only real caveat is if they were to perhaps rapid fire click and catch it between window grab and keypress. I've only done half a million lines or so of code and totally self-taught so my methods may be wrong and sloppy but they work!

Forgive me using vb, but here is a sample  :)

 

Private Sub StartKeyPressTimer()
        KeyPressQueueTimer = New DispatcherTimer
        KeyPressQueueTimer.Interval = New TimeSpan(0, 0, 0, 0, 100)
        AddHandler KeyPressQueueTimer.Tick, AddressOf PerformKeyPressTickMe
        KeyPressQueueTimer.Start()
    End Sub
    Private Sub PerformKeyPressTickMe()
        'Do whatever here  

        If ComboBoxKeyPressQueue.Items.Count = 0 Then
            KeyPressQueueTimer.Stop()
            'TODO: 1
            'Timer stops when there are no keypresses left in the queue and the combobox is empty
        Else
            'The macro keypress combobox has an entry (or entries)...
            'Now gotta start the steps to check and see if iracing is...
            'A. running and..
            'B. Has focus before making a keypress
            IsProcessRunning("iracingsim.exe")

            ComboBoxKeyPressQueue.Items.RemoveAt(0)
            KeyPressQueueTimer.Stop()
            If ComboBoxKeyPressQueue.Items.Count = 0 Then
                'nothing 
            Else
                ComboBoxKeyPressQueue.SelectedIndex = 0
            End If
        End If
        StartKeyPressTimer()


    End Sub

 

Private Structure ProcessData
        Dim Caption As String
        ' Dim CommandLine As String
        ' Dim Owner As String
        ' Dim Priority As String
        'Dim ProcessId As String
        ' Dim ThreadCount As String
        ' Dim HandleCount As String
    End Structure
    Public Function IsProcessRunning(ByVal _processName As String) As Boolean
        Try
            Dim Win32_ProcessProperties() As String = {"Caption"}
            Dim propertyIndex As Integer = 0
            For Each obj As ManagementObject In wmi.GetInstances()
                ReDim Preserve processList(processCount)
                propertyIndex = 0
                ' Caption property.       
                If obj(Win32_ProcessProperties(propertyIndex)) IsNot Nothing Then
                    processList(processCount).Caption = obj(Win32_ProcessProperties(propertyIndex)).ToString
                    If LCase(_processName) = LCase(obj(Win32_ProcessProperties(propertyIndex)).ToString) Then
                        'Ok, so iracing is running, now see if it's the active app
                        'MsgBox("iracing is running")
                        CheckIfiRacingIsActiveApp()

                        'dunno if the return is needed or will break the process chain
                        ' Return True
                    End If
                End If
                processCount += 1
            Next
            '  MsgBox("error. iracing not running")
            Return False

        Catch ex As Exception
            Return False
        End Try
    End Function
    Private Sub CheckIfiRacingIsActiveApp()
        ' Get current Active Window handle
        Dim hdl As IntPtr = GetForegroundWindow()
        ' Create Buffer for Active Window title
        Dim windowTitle As String = Space(GetWindowTextLength(hdl) + 1)
        ' Get Active Window title, that is strTitle
        GetWindowText(hdl, windowTitle, windowTitle.Length)
        ' Write (appended) Active Window title to 1.txt file for verification
        ' My.Computer.FileSystem.WriteAllText("C:\1.txt", windowTitle, True)

        If windowTitle.Contains("iRacing.com Simulator") Then
            ' MsgBox("iracing is the active app")
            PerformKeyPress()
        Else
            'Activate the app before starting the macro
            ActivateAppiRacing()
            ' MsgBox("iracing is NOT the active app")
        End If
    End Sub
    Private Sub ActivateAppiRacing()
        Try

            'Activates the app by bringing it into focus (by Description)
            AppActivate("iracing.com simulator")
            PerformKeyPress()

        Catch ex As Exception

            'This should only trip if program crashes or shuts down in between the time it checked to see if it was running and if it was the active app
            ' MsgBox("error. external program not running")

        End Try
    End Sub
Private Sub PerformMacro()
        'App should be activated (has focus), so every single action here must re-confirm app is running and active before committing
        'MIGHT need some timing adjustment on the timer intervals to make it fast enough to seem seamless.

        ComboBoxMacroQueue.SelectedIndex = 0

        If ComboBoxMacroQueue.Text = "Toggle LF Tire" Then

            If ChangeLFTire = False Then
                'Selecting LF tire to change
                'need to get actual macro sequence of keypresses
                ComboBoxKeyPressQueue.Items.Add("BEGINNING OF MACRO")
                If F5TireHighlightLF = True Then
                    ComboBoxKeyPressQueue.Items.Add("KeyboardPressF5")
                    ComboBoxKeyPressQueue.Items.Add("SPACE")
                ElseIf F5TireHighlightLR = True Then
                    ComboBoxKeyPressQueue.Items.Add("KeyboardPressF5")
                    ComboBoxKeyPressQueue.Items.Add("PREV")
                    ComboBoxKeyPressQueue.Items.Add("SPACE")
                ElseIf F5TireHighlightRF = True Then
                    ComboBoxKeyPressQueue.Items.Add("KeyboardPressF5")
                    ComboBoxKeyPressQueue.Items.Add("PREV")
                    ComboBoxKeyPressQueue.Items.Add("PREV")
                    ComboBoxKeyPressQueue.Items.Add("SPACE")
                ElseIf F5TireHighlightRR = True Then
                    ComboBoxKeyPressQueue.Items.Add("KeyboardPressF5")
                    ComboBoxKeyPressQueue.Items.Add("PREV")
                    ComboBoxKeyPressQueue.Items.Add("PREV")
                    ComboBoxKeyPressQueue.Items.Add("PREV")
                    ComboBoxKeyPressQueue.Items.Add("SPACE")

                End If
                F5TireHighlightLF = True
                F5TireHighlightRF = False
                F5TireHighlightLR = False
                F5TireHighlightRR = False
                ComboBoxKeyPressQueue.Items.Add("END OF MACRO")
                'macro complete
end sub
Private Sub PerformKeyPress()

        '  MsgBox(ComboBoxKeyPressQueue.Items.Count)
        'Read the selected item and perform its keypress
        ComboBoxKeyPressQueue.SelectedIndex = 0

        If ComboBoxKeyPressQueue.Text = "BEGINNING OF MACRO" Then
            'This will either turn on or off a non-adjustment f key so that when we have to press an actual f key
            'we won't turn it off by mistake
            PressBenignFKey()

            'ALL KEYPRESSES HERE
        ElseIf ComboBoxKeyPressQueue.Text = "KeyboardPressF1" Then
            System.Threading.Thread.Sleep(500)
            InputSimulator.SimulateKeyPress(VirtualKeyCode.F1)
        ElseIf ComboBoxKeyPressQueue.Text = "KeyboardPressF2" Then
            System.Threading.Thread.Sleep(500)
            InputSimulator.SimulateKeyPress(VirtualKeyCode.F2)...
Good luck!
Mark
www.teammpr.org