1

In my project im using inputsimulator and it works great when visual studio is ran as an administrator, but when i build it into a .exe it doesn't work even when i run it as administrator. here's my code

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    AutoSaveTimer.Enabled = True

    Try
        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.OemSemicolon)
        System.Threading.Thread.Sleep(2000)
        GameConnection.SendKeyTo(Keys.K)
        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.Enter)

    Catch AutoSaveExeption As GameException
        If AutoSaveExeption.GameErrorCode = GameError.GAME_ERR_SENDMSG Then
            ' Send message error - connection to Game lost.
            ' 
            MessageBox.Show("cant make a connection.... can't autosave sadly", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            SimConnectionBar.BackColor = Color.Red
        End If
    End Try
End Sub

it does send focus to the window i specify but it doesn't send the keystrokes

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
sniperfull
  • 27
  • 1
  • 9
  • Format your code by indenting every row with four spaces instead. Using the backticks \` is only for `inline code` such as when mentioning something about `Button1` or a `ListBox`, etc. -- For more info see the [Markdown and formatting help](https://stackoverflow.com/help/formatting). – Visual Vincent Jul 09 '17 at 15:49
  • This should work, but another option would be to hook into the game's message loop or simply post messages to the game's message loop. With this code, I can't see any reason why it would work in debug, but not release. Are you calling winAPI 'SetForegroundWindow'? How do you know that the game has focus? You should call the API just before sending your input. – Michael Z. Jul 09 '17 at 15:53
  • Ok, this is a button click event which means your app has focus when this code is run. You need SetForegoundWindow to be called in this method. Can you get the handle of the game? – Michael Z. Jul 09 '17 at 16:00
  • Or create a delay at the beginning that will give you time to switch windows back to the game. – Michael Z. Jul 09 '17 at 16:01
  • Instead of using a button click, which when clicked gives your app focus, instead try using hotkeys. When your app gets the message that a specific hotkey was pressed you can execute this code. This means that you can be in the game and press a hotkey that tells your app to do something. Your app would never need focus for this. Let me know if you want to try hotkeys and I can send you an example. – Michael Z. Jul 09 '17 at 16:04
  • I'm using another .dll that works with the game witch sets focus to the game .. the only reason i want to use inputsimulator is that that .dll cant send strings only single keys. And that .dll is a bit buggy and sometimes doesnt even send the key – sniperfull Jul 09 '17 at 16:11
  • I only have a button press for starting a timer and sending the initial autosave. i then have the timer do that every 10 minutes .the whole point of my program is to automate saving progress in the game – sniperfull Jul 09 '17 at 16:12
  • The code you posted is not initiating a timer. It is sending input and your app will receive the input because clicking the button gave it focus. Where is the timer being initiated? Can you post more code? – Michael Z. Jul 09 '17 at 16:15
  • sent some more of the code ... and the timer just does the same but only tries to send the keys but what i really need is the program to send a string as the save file needs to be named .. and the Game .dll what im using does not support strings. thats the whole reason why i want to use inputsimulator – sniperfull Jul 09 '17 at 16:26
  • Please do not add the backticks (`\``) when formatting whole code blocks, they're only for `code` located _**inside**_ a sentence. When adding whole code blocks you shall add four spaces in the beginning of each _**line of code**_ instead. Check the [**source of my edit**](https://stackoverflow.com/revisions/ff6ee25b-40e0-46a4-a9c0-1ed26f2c21fa/view-source) to see the difference between regular text and code. :) – Visual Vincent Jul 10 '17 at 01:35

1 Answers1

0

Try using SetForegroundWindow before sending any input to ensure your game does in fact have focus.The call to SetForegroundWindow should be made in your method just before sending the input.

    <DllImport("user32.dll")> _
Public Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click `
AutoSaveTimer.Enabled = True
Try
    'Find the handle to the game. This can do it by searching for the process.
        Dim p As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("notepad")
        'search for process notepad
        If p.Length > 0 Then
            'check if window was found
            'bring notepad to foreground
            SetForegroundWindow(p(0).MainWindowHandle)
        End If

        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.OemSemicolon)
        System.Threading.Thread.Sleep(2000)
        GameConnection.SendKeyTo(Keys.K)
        System.Threading.Thread.Sleep(50)
        GameConnection.SendKeyTo(Keys.Enter)

    Catch AutoSaveExeption As GameException
        If AutoSaveExeption.GameErrorCode = GameError.GAME_ERR_SENDMSG Then
            ' Send message error - connection to Game lost.
            ' 
            MessageBox.Show("cant make a connection.... can't autosave sadly", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            SimConnectionBar.BackColor = Color.Red
        End If
    End Try
end sub
Michael Z.
  • 1,453
  • 1
  • 15
  • 21
  • i get 3 errors: Statement cannot appear outside of a method body... Declaration expected ( on SetForegroundWindow)... End if must be preceded by a matching if... – sniperfull Jul 09 '17 at 17:35
  • The PInvoke declaration must be out side the function. This is just example code. Let me update it for how it would look in your code. – Michael Z. Jul 09 '17 at 17:41
  • Check out the updated code. You will need to ensure your game has focus in the timer elapsed method too otherwise there is no guarantee that your game has focus. – Michael Z. Jul 09 '17 at 17:45