0

can someone help me? I coded a simple auto press spacebar key program using Visual Basic 2010. It runs and works, it sends space through notepad but unfortunately it is not working in the online game (because spacebar is the control to get Items).

MY CONCERN: Can someone HELP me to get it to work directly through the game?

Here is the simple code.

Public Class Form1
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        SendKeys.Send(TextBox1.Text) 'Sends the message you typed in the textbox1
        SendKeys.Send(" ") 'presses the SPACE key from your keyboard
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Interval = TextBox2.Text
        Timer1.Enabled = True

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Interval = TextBox2.Text
        Timer1.Enabled = False
    End Sub

End Class

THANKS IN ADVANCE!:D

Lews Therin
  • 3,707
  • 2
  • 27
  • 53
  • Are you trying to send the space key to trigger a command or simply trying to append " " to the end of the text you're sending? – Josh Oct 15 '15 at 14:51
  • I just want to automatically perform space in the game because instead of manually pressing spacebar to take/loot a item i want a a program to do that simultaneously. thanks if you got it. – Nielven Aranda Oct 16 '15 at 15:16

1 Answers1

0

I think the problem is that you aren't changing focus to the game window. So your program is sending the spaces to the vb.net program you wrote instead of to the game.

You'll need to do something to change focus to the game window. Check out this question as it has the information you need about using the Win32 API to change which window has focus.

EDIT:

Per your request, here's how you'd work it into your code. Please note that all the information you needed to write this code was in the link I provided. Please read the sources that are provided when someone answers your questions in the future. I left copious comments to help you understand what each line does.

Imports System.Runtime.InteropServices 'Needed to import the Win32 API functions

Public Class Form1
    'Make sure the programTitle const is set to the _EXACT_ title of the 
    'program you are trying to set focus to or this won't work.
    Private Const programTitle As String = "Title of program"
    Private zero As IntPtr = 0 'Required for FindWindowByCaption first parameter

    'Import the SetForegroundWindow Function
    <DllImport("user32.dll")> _
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    'Import the FindWindowByCaption Function, called as a parameter to SetForegroundWindow
    <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowByCaption( _
      ByVal zero As IntPtr, _
      ByVal lpWindowName As String) As IntPtr
    End Function

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'Call SetForegroundWindow whenever you want to send keys to the specified window.
        SetForegroundWindow(FindWindowByCaption(zero, programTitle))

        SendKeys.Send(TextBox1.Text) 'Sends the message you typed in the textbox1
        SendKeys.Send(" ") 'presses the SPACE key from your keyboard
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Interval = TextBox2.Text
        Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Interval = TextBox2.Text
        Timer1.Enabled = False
    End Sub

End Class

Please note: I tested this code and verified that it can set window focus and send the space key and any other key as it should. If this code doesn't work for you, then you have the wrong name in the programTitle constant. For example, if I wanted to set focus to this notepad window:

notepad

and if I set programTitle to "Notepad" trying to get it to send the keys to the notepad window, this wont work. That's because "notepad" is only a part of the title. To get it to work, programTitle should be set to "Untitled - Notepad".

Community
  • 1
  • 1
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
  • Hmm.. Im Having a trouble. It did not Fuction. I follow you instructions properly – Nielven Aranda Oct 18 '15 at 03:10
  • Something else is at play then. Make a new project and only add the code to change windows. That's how I tested it and it worked without issue. The only exception is you need to be careful with the window name. Also, what is the game you are trying to make this work with? – Lews Therin Oct 18 '15 at 16:43