Results 1 to 9 of 9
  1. #1
    Ell0ll's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    62
    Reputation
    10
    Thanks
    4

    Question Send Function key to a game

    Hello every one

    i have tried this code and did not work

    Code:
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)
        Const KEYEVENTF_KEYUP = &H2
    
        <DllImport("user32.dll", SetLastError:=True)> _
        Private Shared Function GetForegroundWindow() As IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=True)> _
        Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
                          ByRef lpdwProcessId As Integer) As Integer
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Timer1.Enabled = True
    
        End Sub
    
        Private Sub press(ByVal mykey As Keys)
    
            keybd_event(CByte(mykey), 0, 0, 0)
            keybd_event(CByte(mykey), 0, KEYEVENTF_KEYUP, 0)
    
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    
           
            Dim fgWin = GetForegroundWindow()
            Dim fgPid As New Integer()
            GetWindowThreadProcessId(fgWin, fgPid)
    
            Dim proc = System.Diagnostics.Process.GetProcessById(fgPid)
    
            If (proc.ProcessName = "bout.dat") Then
               
                press(Keys.F8)
    
            End If
    
        End Sub
    also i have tried this code and did not work too

    Code:
    Imports System.Runtime.InteropServices
    
    Public NotInheritable Class InputHelper
        Private Sub New()
        End Sub
    
    #Region "Methods"
    #Region "PressKey()"
        ''' <summary>
        ''' Virtually presses a key.
        ''' </summary>
        ''' <param name="Key">The key to press.</param>
        ''' <param name="HardwareKey">Whether or not to press the key using its hardware scan code.</param>
        ''' <remarks></remarks>
        Public Shared Sub PressKey(ByVal Key As Keys, Optional ByVal HardwareKey As Boolean = False)
            If HardwareKey = False Then
                InputHelper.SetKeyState(Key, False)
                InputHelper.SetKeyState(Key, True)
            Else
                InputHelper.SetHardwareKeyState(Key, False)
                InputHelper.SetHardwareKeyState(Key, True)
            End If
        End Sub
    #End Region
    
    #Region "SetKeyState()"
        ''' <summary>
        ''' Virtually sends a key event.
        ''' </summary>
        ''' <param name="Key">The key of the event to send.</param>
        ''' <param name="KeyUp">Whether to push down or release the key.</param>
        ''' <remarks></remarks>
        Public Shared Sub SetKeyState(ByVal Key As Keys, ByVal KeyUp As Boolean)
            Key = ReplaceBadKeys(Key)
    
            Dim KeyboardInput As New KEYBDINPUT With {
                .wVk = Key,
                .wScan = 0,
                .time = 0,
                .dwFlags = If(KeyUp, KEYEVENTF.KEYUP, 0),
                .dwExtraInfo = IntPtr.Zero
            }
    
            Dim Union As New INPUTUNION With {.ki = KeyboardInput}
            Dim Input As New INPUT With {
                .type = INPUTTYPE.KEYBOARD,
                .U = Union
            }
    
            SendInput(1, New INPUT() {Input}, Marshal.SizeOf(GetType(INPUT)))
        End Sub
    #End Region
    
    #Region "SetHardwareKeyState()"
        ''' <summary>
        ''' Virtually sends a key event using the key's scan code.
        ''' </summary>
        ''' <param name="Key">The key of the event to send.</param>
        ''' <param name="KeyUp">Whether to push down or release the key.</param>
        ''' <remarks></remarks>
        Public Shared Sub SetHardwareKeyState(ByVal Key As Keys, ByVal KeyUp As Boolean)
            Key = ReplaceBadKeys(Key)
    
            Dim KeyboardInput As New KEYBDINPUT With {
                .wVk = 0,
                .wScan = MapVirtualKeyEx(CUInt(Key), 0, GetKeyboardLayout(0)),
                .time = 0,
                .dwFlags = KEYEVENTF.SCANCODE Or If(KeyUp, KEYEVENTF.KEYUP, 0),
                .dwExtraInfo = IntPtr.Zero
            }
    
            Dim Union As New INPUTUNION With {.ki = KeyboardInput}
            Dim Input As New INPUT With {
                .type = INPUTTYPE.KEYBOARD,
                .U = Union
            }
    
            SendInput(1, New INPUT() {Input}, Marshal.SizeOf(GetType(INPUT)))
        End Sub
    #End Region
    
    #Region "ReplaceBadKeys()"
        ''' <summary>
        ''' Replaces bad keys with their corresponding VK_* value.
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared Function ReplaceBadKeys(ByVal Key As Keys) As Keys
            Dim ReturnValue As Keys = Key
    
            If ReturnValue.HasFlag(Keys.Control) Then
                ReturnValue = (ReturnValue And Not Keys.Control) Or Keys.ControlKey 'Replace Keys.Control with Keys.ControlKey.
            End If
    
            If ReturnValue.HasFlag(Keys.Shift) Then
                ReturnValue = (ReturnValue And Not Keys.Shift) Or Keys.ShiftKey 'Replace Keys.Shift with Keys.ShiftKey.
            End If
    
            If ReturnValue.HasFlag(Keys.Alt) Then
                ReturnValue = (ReturnValue And Not Keys.Alt) Or Keys.Menu 'Replace Keys.Alt with Keys.Menu.
            End If
    
            Return ReturnValue
        End Function
    #End Region
    #End Region
    
    #Region "WinAPI P/Invokes"
        <DllImport("user32.dll", SetLastError:=True)>
        Private Shared Function SendInput(ByVal nInputs As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal pInputs() As INPUT, ByVal cbSize As Integer) As UInteger
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function MapVirtualKeyEx(uCode As UInteger, uMapType As UInteger, dwhkl As IntPtr) As UInteger
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function GetKeyboardLayout(idThread As UInteger) As IntPtr
        End Function
    
    #Region "Enumerations"
        Private Enum INPUTTYPE As UInteger
            MOUSE = 0
            KEYBOARD = 1
            HARDWARE = 2
        End Enum
    
        <Flags()> _
        Private Enum KEYEVENTF As UInteger
            EXTENDEDKEY = &H1
            KEYUP = &H2
            SCANCODE = &H8
            UNICODE = &H4
        End Enum
    #End Region
    
    #Region "Structures"
        <StructLayout(LayoutKind.Explicit)> _
        Private Structure INPUTUNION
            <FieldOffset(0)> Public mi As MOUSEINPUT
            <FieldOffset(0)> Public ki As KEYBDINPUT
            <FieldOffset(0)> Public hi As HARDWAREINPUT
        End Structure
    
        Private Structure INPUT
            Public type As Integer
            Public U As INPUTUNION
        End Structure
    
        Private Structure MOUSEINPUT
            Public dx As Integer
            Public dy As Integer
            Public mouseData As Integer
            Public dwFlags As Integer
            Public time As Integer
            Public dwExtraInfo As IntPtr
        End Structure
    
        Private Structure KEYBDINPUT
            Public wVk As UShort
            Public wScan As Short
            Public dwFlags As UInteger
            Public time As Integer
            Public dwExtraInfo As IntPtr
        End Structure
    
        Private Structure HARDWAREINPUT
            Public uMsg As Integer
            Public wParamL As Short
            Public wParamH As Short
        End Structure
    #End Region
    #End Region
    
    
    
    End Class
    Code:
     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
           
            Dim fgWin = GetForegroundWindow()
            Dim fgPid As New Integer()
            GetWindowThreadProcessId(fgWin, fgPid)
    
            Dim proc = System.Diagnostics.Process.GetProcessById(fgPid)
    
            If (proc.ProcessName = "bout.dat") Then
               
                InputHelper.PressKey(Keys.F8, True)
    
            End If
    
        End Sub
    if i send letters or numbers both are working with no problems but not Function keys

    to make sure i have tried windows 7 On screen keyboard it can send Function keys
    also i tried some free virtual keyboard all can send numbers and letters but not Function keys

    except This FVK (OSK) it is like windows 7 OSK both can send Function keys to the game

    sorry for this long story but i want it to be clear

    My question : is there any code (VB2010) that can send Function keys to the game like windows 7 OSK ?

    I hope some can do it

  2. #2
    infidel_'s Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    blah
    Posts
    184
    Reputation
    10
    Thanks
    138
    My Mood
    Twisted
    Which game you are trying to send the keys to?
    There are games that blocks direct input if not from keyboards.
    Also, there are classes that will not work on DirectX games.
    There are also games the blocks SendInput, SendMessage, PostMessage & VK_Keys.

    If you are trying to make a bot, its best you work on OCR & Look for Interceptor.dll.

  3. #3
    Ell0ll's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    62
    Reputation
    10
    Thanks
    4
    Quote Originally Posted by infidel_ View Post
    Which game you are trying to send the keys to?
    There are games that blocks direct input if not from keyboards.
    Also, there are classes that will not work on DirectX games.
    There are also games the blocks SendInput, SendMessage, PostMessage & VK_Keys.

    If you are trying to make a bot, its best you work on OCR & Look for Interceptor.dll.
    Link of game

    how do i know if it is blocking - direct input if not from keyboards

    i think it is not blocking because Windows 7 OSK and FVK that i download it both are working i can send Function keys with it

    Windows 7 OSK and FVK >>> which way do you think are using to make it

    SendInput or SendMessage or PostMessage or VK_Keys

    Can you write an example code for each way of these ( SendInput, SendMessage, PostMessage and VK_Keys )

    also what about this ( Interceptor.dll )

    Thanks
    Last edited by Ell0ll; 09-10-2017 at 10:29 AM.

  4. #4
    infidel_'s Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    blah
    Posts
    184
    Reputation
    10
    Thanks
    138
    My Mood
    Twisted
    There are ways.
    1. Try AutoIt or AutoHotkey
    2. Im using https://www.kbdedi*****m/manual/low_level_vk_list.html
    3. https://******.com/jasonpang/Interce...ster/README.md

    Each of those are different approach but achieves the same thing, automation of Key Sends & Mouse Inputs.

    VB.NET
    For VK_Keys, you have to declare it as constants.
    Code:
    #Region "Mouse & Key Events Declaration"
        Declare Function mouse_event Lib "user32.dll" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
    
        Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                 (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                     (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
        Function MakeDWord(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
            MakeDWord = (HiWord * &H10000) Or (LoWord And &HFFFF&)
        End Function
    #End Region
    Code:
    Const WM_KEYDOWN As Integer = &H100
    Const WM_KEYUP As Integer = &H101
    Const VK_TAB As Integer = &H9 'Sample key (TAB Key)
    Calling it like this:
    Code:
      Dim GameWin As IntPtr = FindWindow(Nothing, "YourTargetGameWindowName")
        SendMessage(gameWin, WM_KEYDOWN, VK_TAB, 0)
        SendMessage(gameWin, WM_KEYUP, VK_TAB, 0)
    FOR AUTOIT
    Reference AutoItX64.dll into your project and call it like this:
    Import AutoIt Library first
    Code:
    Imports AutoItX3Lib
    Then, declare a variable that links to the AutoItX3Lib
    Code:
    Dim au3 As New AutoItX3
    And Call it like this:
    Code:
    'ControlSend ( "title", "text", controlID, "string" [, flag = 0] ) '<= The AutoIt Syntax
    au3.ControlSend("TargetGameWindowName", "", "", "{2}") '<= The Vb.net AutoIt Syntax
    {2} is the Raw Key for #2.
    Which means, it sends Keys.2 directly to the game window.
    Which means, it works EVEN if the window is hidden or minimized.
    Last edited by infidel_; 09-12-2017 at 03:27 AM.

  5. The Following User Says Thank You to infidel_ For This Useful Post:

    Ell0ll (09-12-2017)

  6. #5
    Ell0ll's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    62
    Reputation
    10
    Thanks
    4
    Quote Originally Posted by infidel_ View Post
    There are ways.
    1. Try AutoIt or AutoHotkey
    2. Im using https://www.kbdedi*****m/manual/low_level_vk_list.html
    3. https://******.com/jasonpang/Interce...ster/README.md

    Each of those are different approach but achieves the same thing, automation of Key Sends & Mouse Inputs.

    VB.NET
    For VK_Keys, you have to declare it as constants.
    Code:
    #Region "Mouse & Key Events Declaration"
        Declare Function mouse_event Lib "user32.dll" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
    
        Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                 (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
        Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                     (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
        Function MakeDWord(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
            MakeDWord = (HiWord * &H10000) Or (LoWord And &HFFFF&)
        End Function
    #End Region
    Code:
    Const WM_KEYDOWN As Integer = &H100
    Const WM_KEYUP As Integer = &H101
    Const VK_TAB As Integer = &H9 'Sample key (TAB Key)
    Calling it like this:
    Code:
      Dim GameWin As IntPtr = FindWindow(Nothing, "YourTargetGameWindowName")
        SendMessage(gameWin, WM_KEYDOWN, VK_TAB, 0)
        SendMessage(gameWin, WM_KEYUP, VK_TAB, 0)
    FOR AUTOIT
    Reference AutoItX64.dll into your project and call it like this:
    Import AutoIt Library first
    Code:
    Imports AutoItX3Lib
    Then, declare a variable that links to the AutoItX3Lib
    Code:
    Dim au3 As New AutoItX3
    And Call it like this:
    Code:
    'ControlSend ( "title", "text", controlID, "string" [, flag = 0] ) '<= The AutoIt Syntax
    au3.ControlSend("TargetGameWindowName", "", "", "{2}") '<= The Vb.net AutoIt Syntax
    {2} is the Raw Key for #2.
    Which means, it sends Keys.2 directly to the game window.
    Which means, it works EVEN if the window is hidden or minimized.

    Firstly Thanks for your reply

    Secondly explanation of what i have done

    i tried every way you told me about it

    AutoIt : Error: A reference could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component
    i tried use regsvr32 AutoItX3.dll > so i can add it as reference
    i used your code to test it >>> it is working also but letters numbers only it can't send Function keys to the game

    AutoHotkey : this time the Error: A reference could not be added > did not appear and i could add it as reference with no problems
    but i tried use Import AutoHotkey Library i could not there is nothing so i could not test it

    Interceptor : Error: A reference could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component
    this time i tried use regsvr32 interception.dll > so i can add it as reference
    another error appear >>> the module was loaded but the entry-point dllregisterserver was not found Make sure that "interception.dll" is a valid DLL or ocx file and then try again
    i tried again to add it as a reference same error appear so i could not test it too

    SendMessage : i tried this way it does not send any thing not even numbers or letters

    Code:
      Dim GameWin As IntPtr = FindWindow(Nothing, "YourTargetGameWindowName")
        SendMessage(gameWin, WM_KEYDOWN, VK_TAB, 0)
        SendMessage(gameWin, WM_KEYUP, VK_TAB, 0)
    PostMessage : i replaced "SendMessage" with "PostMessage" >>> it can send letters and numbers but not function keys

    Code:
      Dim GameWin As IntPtr = FindWindow(Nothing, "YourTargetGameWindowName")
        PostMessage(gameWin, WM_KEYDOWN, VK_TAB, 0)
        PostMessage(gameWin, WM_KEYUP, VK_TAB, 0)

  7. #6
    infidel_'s Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    blah
    Posts
    184
    Reputation
    10
    Thanks
    138
    My Mood
    Twisted
    Try sending Function Keys using Autoit without making it a Raw Key.
    So just Send("F1") see if that works.

    As for PostMessage / SendMessage, like I said, declare the Key first as constant.
    Const VK_F1 As Integer = &H70

  8. #7
    MikeRohsoft's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Los Santos
    Posts
    797
    Reputation
    593
    Thanks
    26,314
    for GTA it was enough to do a break, between calling up and down
    Code:
      keybd_event(0x57, xKey, 0, 0)
      Sleep(50)
      keybd_event(0x57, xKey, KEYEVENTF_KEYUP, 0)

  9. #8
    infidel_'s Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    blah
    Posts
    184
    Reputation
    10
    Thanks
    138
    My Mood
    Twisted
    Its also possible that he is sending the keys to wrong window.

  10. #9
    Ell0ll's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    62
    Reputation
    10
    Thanks
    4
    Quote Originally Posted by infidel_ View Post
    Try sending Function Keys using Autoit without making it a Raw Key.
    So just Send("F1") see if that works.

    As for PostMessage / SendMessage, like I said, declare the Key first as constant.
    Const VK_F1 As Integer = &H70
    Not working

    Quote Originally Posted by MikeRohsoft View Post
    for GTA it was enough to do a break, between calling up and down
    Code:
      keybd_event(0x57, xKey, 0, 0)
      Sleep(50)
      keybd_event(0x57, xKey, KEYEVENTF_KEYUP, 0)
    Not working

    Quote Originally Posted by infidel_ View Post
    Its also possible that he is sending the keys to wrong window.
    How ? i am able to send any other keys (A,B,C, etc) but not Function keys

    Do not believe me
    it is easy
    Any one can test it by downloading the game

    Link of game

Similar Threads

  1. [Help] VB6 codes sending Function Keys to my online games
    By joi in forum Visual Basic Programming
    Replies: 6
    Last Post: 10-09-2015, 01:58 AM
  2. [Request] Sending Keys in DirectX Game
    By WarOfRen in forum Visual Basic Programming
    Replies: 4
    Last Post: 08-09-2015, 03:16 PM
  3. [Help Request] Focusing And Sending Keys To Particular Game
    By Crostator in forum Visual Basic Programming
    Replies: 5
    Last Post: 03-01-2012, 03:24 AM
  4. [HELP]Send random keys?
    By Pixie in forum Visual Basic Programming
    Replies: 26
    Last Post: 10-25-2009, 11:54 AM
  5. [Sell] PC CD Keys for NEWEST Games
    By Tolumbar in forum Trade Accounts/Keys/Items
    Replies: 45
    Last Post: 03-19-2009, 09:55 AM

Tags for this Thread