Results 1 to 5 of 5

Thread: [C#] Best way to simulate keystrokes

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    22

    [C#] Best way to simulate keystrokes

    Hello guys,

    I'm studying c# and trying to develop a bot which simulates keyboard strokes and mouse clicks.

    But i'm having some troubles:
    1# I only manage to send keyboard strokes with the client in focus, with the SendKeys. Tried SendMessage, and PostMessage... did not work.


    Can you guys put me in right direction?
    Whats the best way to simulate keyboard?
    Is there any ready api? (tried InputSimulator API, but does not work on non focused windows)

    Thank you very much!

  2. #2
    Senior Member Lolrapa's Avatar
    Join Date
    Mar 2014
    Posts
    125
    Never tried it but how are you getting the parameters for sendMessage?

    Sending keys and mooving mouse is ok, but sometime the element you want to interact with moove in the screen, like creatures
    The best method to do this is by sending the packets directly to the server and there are several ways to do this

    Tibia has a function that sends packets to the server, once you find you can use it at will
    1) by thread hijacking
    thread hijacking it basically to create a thread in tibia with a cavecode (a little code) that will call tibia function and thath will do the action like mooving, eating etc

    http://tpforums.org/forum/threads/48...ight=tibiasock
    http://tpforums.org/forum/threads/5463-Update-tibiasock
    (there is a c# version written by klusbert)

    2) by injected dll
    You can create a dll which you can inject into tibia and tibia will run all the code you write in your dll, that way you will gain access to tibia memory and function call via pointers
    So you just to call the packet function from there

    3)by injected dll
    Same way as before but instead calling the function to send the packet you can use the socket to send the packet directly to the server (you have to build the packet this way)
    https://bitbucket.org/farsa/minidash...default#cl-165

    Ofc both three ways need some work out and solve some synchronization problems

    There is a lot of info in this forum that will help you out and you can write whenever you need help I will be glad to help you!!
    Bye!
    Last edited by Lolrapa; 05-31-2015 at 03:49 PM.

  3. #3
    Administrator
    Join Date
    Mar 2007
    Posts
    1,723
    SendMessage/PostMessage is the best way to simulate keyboard and mouse events. Use Spy++ to record these messages and view their parameters, then you can recreate them to get the desired outcome.

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    22
    thanks for hleping guys.

    @Lolrapa
    I dont want to send packages to the client, one may think this is detectable.

    @Jo3Bingham
    i used the spy++ tool (thanks for the tip), and i've came to this code.

    Code:
            [DllImport("user32.dll")]
            public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
            [DllImport("user32.dll")]
            public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
            static void Main(string[] args)
            {
               
               Process notepad = Process.GetProcessesByName("notepad")[0];
               
               const uint WM_KEYDOWN = 0x100;
               const uint WM_KEYUP = 0x0101;
               const uint F1_KEY = 0x70;
    
               SendMessage(notepad.MainWindowHandle, WM_KEYDOWN, ((IntPtr)F1_KEY), (IntPtr)0);
               SendMessage(notepad.MainWindowHandle, WM_KEYUP, ((IntPtr)F1_KEY), (IntPtr)0);
    
                System.Console.ReadLine();
            }
    What i'm trying to do here, is send a F1 keystroke to notepad, but its not working. Can you give a hint?

  5. #5
    Senior Member Lolrapa's Avatar
    Join Date
    Mar 2014
    Posts
    125
    Code:
                const uint WM_KEYDOWN = 0x100;
                const uint WM_KEYUP = 0x0101;
                const uint F1_KEY = 0x70;
                Process k = Process.GetProcessesByName("notepad")[0];
                IntPtr H = k.MainWindowHandle;
                PostMessage(H, WM_KEYDOWN, ((IntPtr)F1_KEY), (IntPtr)0);
                PostMessage(H, WM_KEYUP, ((IntPtr)F1_KEY), (IntPtr)0);
    This is working for me

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •