[VB.NET]Can't send key to game with API

Discussion in 'Mixed Languages' started by stevemk14ebr, Dec 8, 2012.

  1. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #1 stevemk14ebr, Dec 8, 2012
    Last edited by a moderator: Apr 20, 2017
    i am just trying to send w (forward move button) to battlefield 3 with windows api's
    Battlefield must be preventing this because in all other programs it works fine but in battlefield it doesn't register
    is there another way of doing this (NOT MAKING A HACK JUST TO SEE IF IT'S POSSIBLE/FOR FUN)
    code:
    Code:
    Public Class Form1
        Const KEYEVENTF_EXTENDEDKEY = &H1
        Const KEYEVENTF_KEYUP = &H2
        Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
        Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Integer, ByVal bScan As Integer, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If GetAsyncKeyState(Keys.T) Then
                keybd_event(Keys.W, 0, 0, 0)   ' press w
                keybd_event(Keys.W, 0, KEYEVENTF_KEYUP, 0)   ' release w
            End If
        End Sub
    End Class
     
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,170
    120
    #2 Josh Cell, Dec 8, 2012
    Last edited by a moderator: Apr 20, 2017
    Cool, very useful for make auto quick-scope for MMOFPS games.. haha.

    Thanks! :D

    C#

    Code:
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    public class Form1
    {
    const  KEYEVENTF_EXTENDEDKEY = 0x1;
    const  KEYEVENTF_KEYUP = 0x2;
    [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    public static extern int GetAsyncKeyState(int vKey);
    [DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    private static extern void keybd_event(int bVk, int bScan, int dwFlags, int dwExtraInfo);
    
    private void Timer1_Tick(object sender, EventArgs e)
    {
    if (GetAsyncKeyState(Keys.T)) {
    keybd_event(Keys.W, 0, 0, 0);
    // press w
    keybd_event(Keys.W, 0, KEYEVENTF_KEYUP, 0);
    // release w
    }
    }
    }
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,218
    22,277
    210
    Does this only not work in battlefield process, but does for others ?
     
  4. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,170
    120
    #4 Josh Cell, Dec 8, 2012
    Last edited by a moderator: Apr 20, 2017
    It's because the Game Hack engine that blocks the virtual send keys.

    Is needed to emulate the keyboard via driver or other thing to really have success on the stroke.

    I haven't any way on how to do it, but I'll look on it for the way.

    The code work on other apps or games that haven't virtual keys restrictions.

    I haven't the sure if it will work, but can inspire you:

    Code:
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading; 
    
    using Microsoft.DirectX; 
    using Microsoft.DirectX.DirectInput; 
    
    namespace ConsoleApplication1 
    { 
       class Program 
       { 
          static void Main(string[] args) 
          { 
             Device keyboard; 
    
             keyboard = new Device(SystemGuid.Keyboard); 
             try 
             { 
                keyboard.Acquire(); 
             } 
             catch (DirectXException ex) 
             { 
                Console.WriteLine(ex.Message); 
             } 
    
             KeyboardState keyboardState; 
    
             try 
             { 
                keyboard.Poll(); 
                keyboardState = keyboard.GetCurrentKeyboardState(); 
             } 
             catch (NotAcquiredException) 
             { 
                try 
                { 
                   keyboard.Acquire(); 
                } 
                catch (InputException iex) 
                { 
                   Console.WriteLine(iex.Message); 
                } 
             } 
             catch (InputException ex2) 
             { 
                Console.WriteLine(ex2.Message); 
             } 
    
             //-------------------------------- 
    
             Console.WriteLine("Wait 2 seconds:"); 
             Thread.Sleep(2000); 
          } 
       } 
    }
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #5 stevemk14ebr, Dec 8, 2012
    Last edited: Dec 8, 2012
    (OP)
    thanks josh i'll look into it, i just needed someone to point me in a direction

    EDIT: i found some code that i believe will do the trick, but it's in a language i don't understand (it's c++)
    if anyone could tell me what's going on in it or if they were super kind even port it to vb.net i would love them take a look here
    Involves a kernel hook