Send Function key to a game

09/11/2017 13:04 Ell0ll#1
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 [Only registered and activated users can see links. Click Here To Register...] 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 ?

Also : Why Windows 7 OSK and [Only registered and activated users can see links. Click Here To Register...] >>> can send Function keys to the game and there are some other free virtual keyboard can't send Function keys

>>> which code are using to make it work

I hope some can do it :)
09/17/2017 13:07 EasyFarm#2
Maybe your game is using dinput, so your way won't work like this ^^
09/18/2017 19:49 Ploxasarus#3
This is C# but should be helpful.


Code:
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private void Timer1_Tick(object sender, EventArgs e)
       
            const int WM_KEYDOWN = 0x100;
            const int WM_KEYUP = 0x101;

            string processName = "bout.dat";
            Process[] processList = Process.GetProcesses();

            foreach (Process p in processList)
            {
                if (p.ProcessName.Equals(processName))
                {
                    IntPtr hWnd = p.MainWindowHandle;
                    PostMessage(hWnd, WM_KEYDOWN, 0x77, 0x420001);
                    Thread.Sleep(2);
                    PostMessage(hWnd, WM_KEYUP, 0x77, 0x420001);
                }
            }
        }
09/19/2017 01:30 Ell0ll#4
Quote:
Originally Posted by Ploxasarus View Post
This is C# but should be helpful.


Code:
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private void Timer1_Tick(object sender, EventArgs e)
       
            const int WM_KEYDOWN = 0x100;
            const int WM_KEYUP = 0x101;

            string processName = "bout.dat";
            Process[] processList = Process.GetProcesses();

            foreach (Process p in processList)
            {
                if (p.ProcessName.Equals(processName))
                {
                    IntPtr hWnd = p.MainWindowHandle;
                    PostMessage(hWnd, WM_KEYDOWN, 0x77, 0x420001);
                    Thread.Sleep(2);
                    PostMessage(hWnd, WM_KEYUP, 0x77, 0x420001);
                }
            }
        }
i have tried it - it is not working

Quote:
Originally Posted by EasyFarm View Post
Maybe your game is using dinput, so your way won't work like this ^^
what is the solution for it ?

Game [Only registered and activated users can see links. Click Here To Register...]for any one want try it
09/20/2017 14:03 killzone#5
Look into VK_Keys. Its what I used automating directX games.

It appears, I already answered you on ****.
10/08/2017 20:04 Kurisu Makise#6
Quote:
Originally Posted by Xio. View Post
[Only registered and activated users can see links. Click Here To Register...]

That's what I'm using to send keystrokes to games. It's working. Don't tell me it isn't, I'm using it.
its not working for me .. >.<
10/09/2017 23:20 Kurisu Makise#7
Quote:
Originally Posted by Xio. View Post
run your app as admin... jesus fucking christ.
I ran it as admin but its not working! -.- satan fucking christ.
10/10/2017 19:08 Kurisu Makise#8
Quote:
Originally Posted by Xio. View Post
works for OP. Better go learn how to use google
just because i works for op it doesnt mean it runs for everyone. better go learn how to use your brain instead of google
10/10/2017 21:51 Syc#9
How about both of you calm down a bit?

@[Only registered and activated users can see links. Click Here To Register...] you should really describe your problem in detail and maybe even create your own thread. Nobody can help you if you don't explain what you are doing and what exactly is not working.
10/11/2017 16:50 Kurisu Makise#10
Quote:
Originally Posted by Xio. View Post
Fking germans..
Quote:
Originally Posted by Xio. View Post
works for OP. Better go learn how to use google
Quote:
Originally Posted by Xio. View Post
run your app as admin... jesus fucking christ.
Quote:
Originally Posted by Syc View Post
How about both of you calm down a bit?

@[Only registered and activated users can see links. Click Here To Register...] you should really describe your problem in detail and maybe even create your own thread. Nobody can help you if you don't explain what you are doing and what exactly is not working.
I will write a more detailed ticket to the support all right.

But you ignore this insults and racism ?
A moderator has to take action instead of just watching seriously.
10/11/2017 17:25 killzone#11
Calm your tits down people.
We have give him the solutions, its up to him to figure it out on his own.
I have answered him at M.p.g.h.net already with the same thread and further explained HOW to use my function.
10/11/2017 18:00 Kurisu Makise#12
Quote:
Originally Posted by Xio. View Post
The only insult was "Fkin germans" guess what? I'm german.

This is just typical behavior of germans. Complaining without giving any information that could even help remotely. I'm out. Good luck, not going to reply to you again, you need to grow up.

Look at your behavior before you complain about mine.


^ No information


^ No information, playing on my previous statement with an idiotic adaptation



^ No information, insulting, idiotic claim. If you use windows, this code works - if you don't and you try to copy paste this code, you're plain stupid. There is no way it doesn't work for you. Implement it correctly.

[Only registered and activated users can see links. Click Here To Register...]
your comment was racist anyway

also I wrote a ticket from the beginning but got no respone so I complained in this thread. guess you just dont have enough brain cells to understand that and try to cover it with insults
10/12/2017 23:02 cookie69#13
Interesting subject...Well, this game is using SetWindowsHookEx to hook the keyboard inputs and I think they are using this to "protect" the game against non-human inputs.