0

ive seen other questions about this issue but none of them resolved it for me so here i go. I'm trying to send a keypress to a game to save the progress(its a 64bit game). what i've coded so far is :

Dim p() As Process
Dim GameID As Integer = 0
 Dim p As Process() = Process.GetProcessesByName("Gamename")

    If p.Length > 0 Then
        For i As Integer = 0 To p.Length - 1
            GameID = (p(i).Id)
        Next
    End If
AutoSaveTimer.Enabled = True
    Dim Test As Integer = 0
    GetAsyncKeyState(Test)
    AppActivate("GameName")
    My.Computer.Keyboard.SendKeys(";", True)

now i've tried Sendkeys.Send(";") but without luck, and the game runs under "GameName" but then the keypress needs to be sent in a window under the game : Blacked out is the game and under the first window is where the keypress needs to be sent

thanks in advance for the help

sniperfull
  • 27
  • 1
  • 9
  • 1
    Possible duplicate of [Why do some applications not accept some sendkeys at some times](https://stackoverflow.com/questions/39809095/why-do-some-applications-not-accept-some-sendkeys-at-some-times) – Visual Vincent Jul 05 '17 at 08:08
  • You need to send the key strokes as _hardware scan codes_ in order for some games to allow them. Doing so sort of "tricks" the game into believing that the key stroke came from the actual keyboard rather than a program. This is described in my answer in the link above. – Visual Vincent Jul 05 '17 at 08:12

2 Answers2

0

I use InputSimulator in my app and it works great.

https://inputsimulator.codeplex.com/

It could be this easy to send keys. The active window will see that as if the user actually used those keys.

var sim = new InputSimulator();
sim.Keyboard.KeyPress(VirtualKeyCode.SPACE);
sim = null;

Here's a screen shot of the methods you can use for keyboard.

enter image description here

Michael Z.
  • 1,453
  • 1
  • 15
  • 21
  • But the codeplex page says this is for C# wouldn't that cause an issue as im coding in VB.NET? – sniperfull Jul 04 '17 at 19:53
  • You could compile the c# into a DLL that you could reference in your project. The project should already be setup to compile to DLL. – Michael Z. Jul 04 '17 at 19:55
  • Sorry if im being a bit dumb but if the .dll is written in C# how could i use it in a VB.net program i've tried adding the dll as a reference but it dindt work . – sniperfull Jul 04 '17 at 20:28
  • What do you mean it didn't work? If you have a compiled c# library, it will work as a reference in a VB.net program. Are you getting an error? Can you post what you have tried. – Michael Z. Jul 04 '17 at 20:32
  • I have set it up to send a keypress but it only sets focus on the game window and does not proceed to do what would happen if that key was pressed – sniperfull Jul 04 '17 at 22:17
  • yes input simulator works with both c# and vb.net, import it using NuGet – user1234433222 Jul 04 '17 at 23:09
  • I've done some more testing and i've found that this does work on 32 bit instance of the game but not the new 64 bit version? any idea why? – sniperfull Jul 05 '17 at 13:55
  • Are you compiling your app to 64-bit? – Michael Z. Jul 05 '17 at 14:04
  • It might have a different process name for the 64-bit version of the game. Can you check if that is the case? – Michael Z. Jul 05 '17 at 16:06
  • No the Process name just changes from V3 to V4 witch i've also changed in the code ... the Game window gets focus but nothing happens – sniperfull Jul 05 '17 at 16:39
  • Have you tried @Visual Vincent answer in this question? I can't imagine this would be the issue though since the 32-bit takes the input. A worth a shot maybe....https://stackoverflow.com/a/39811061/4114591 – Michael Z. Jul 05 '17 at 16:56
  • i've tried it but with no luck... The game window pop ups but nothing happens – sniperfull Jul 05 '17 at 18:43
  • Sorry this is completely my fault Inputsimulator works like a charm !! it just had to be ran in administrator mode.... sorry beginner mistake here thanks for the help! – sniperfull Jul 05 '17 at 19:14
  • Actually after i have built my app and ran the .exe as admin it does not work i only see the icon of the game Flashing on my taskbar but it doesnt send the keystrokes... as when i run visual studio as administrator it does ?? – sniperfull Jul 09 '17 at 14:46
  • That doesn't make sense to me. It should work. Start a new question about this and post a link here so I can visit it. Show us your code and explain that it works when debugging with VS, but not compiled. – Michael Z. Jul 09 '17 at 15:24
  • @MichaelZ. https://stackoverflow.com/questions/44998106/inputsimulator-works-while-debugging-but-not-when-program-is-built – sniperfull Jul 09 '17 at 15:36
  • Thanks, I'm viewing it now – Michael Z. Jul 09 '17 at 15:48
0

You can simulate keyboard input to a program like this:

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

Public Sub Send()
        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

        SendKeys.SendWait("a")
        'send key "a" to notepad
    End Sub
Michael Z.
  • 1,453
  • 1
  • 15
  • 21
  • This does not work in the game that im trying to send keypress to – sniperfull Jul 04 '17 at 20:28
  • Are you sure you have the handle of the exact window you want to send keys? Instead of using GetProcessByName, try using some WinAPI to get the handle of the exact window. – Michael Z. Jul 04 '17 at 20:34