How to send a key to a directx window?

+2 Timmy Cafe · December 26, 2014
Hello everyone,

I have been trying to send key strokes to a directx application, but nothing seems to be working. I have even tried this bit of code:


INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.dwFlags = KEYEVENTF_UNICODE;
ip.ki.wScan = 'z';
ip.ki.wVk = 0;
ip.ki.dwExtraInfo = 0;
SendInput(1, &ip, sizeof(INPUT));


Where a lot of people were suggesting to use for sending keys to a directx application. Is there another way i can send keys to a directx application?

Post a Reply

Replies

Oldest  Newest  Rating
0 Timmy Cafe · December 26, 2014
@Krootushas Gesu  Post a message to a process?, sorry i am not very familiar with that. how would i use it?
0 Timmy Cafe · December 26, 2014
okay so i tried


PostMessage(hwnd, VkKeyScan('z'), 0, 0);

and



SendMessage(hwnd, VkKeyScan('z'), 0, 0);


this did not seem to work, so i called GetLastError and i receive this

762F11C0

not for sure what type of error it is
0 Timmy Cafe · December 26, 2014
@Krootushas Gesu  Wow i am such an idiot, i put GetLastError without the parentheses. When i do call it properly i get code 299 which is something wrong with my readprocess, but if i check for my SendMessage/PostMessage i don't get an error.
0 Timmy Cafe · December 26, 2014
@Krootushas Gesu  So i tired your
 PostMessage(hWnd,WM_KEYDOWN,VK_Z,VK_Z << 16);

  which i had to replace
VK_Z

with the
VkKeyScan('z')

because c++ does not have a VK_Z that i know of and it still did not work. I have also been going online trying to see what i can find i also tried:


PostMessage(hwmd, WM_KEYDOWN, VkKeyScanEx('z', hCurKeyboard), VkKeyScanEx('z', hCurKeyboard) << 16);

Which did not work either. I just don't know what i am doing wrong. BTW i really appreciate your help.
0 Timmy Cafe · December 27, 2014
@ Krootushas Gesu I called GetLastError function almost after every single line of code and the only code i get is the 299, could this error be calling because i am using a 32 bit version of VS c++ and my process i am trying to read i think is a 64-bit?
0 Timmy Cafe · December 27, 2014
Yes i am trying to read the memory

here is my code so far:




#include "stdafx.h"
#include<iostream>
#include <Windows.h>
#pragma comment(lib, "user32.lib")

using namespace std;

int main()
{

    HWND hWnd = FindWindow(0, L"Test");
    cout << GetLastError() << endl;

    if (hWnd == 0){
        cout << "ERROR: Could not find Test!" << endl;
    }
    else{
        cout << "Test Has Been Found." << endl;
        DWORD proc_id;
        GetWindowThreadProcessId(hWnd, &proc_id);

        cout << GetLastError() << endl;

        HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id);
        
        cout << GetLastError() << endl;
        if (!hProc){
            cout << "ERROR: Could not open Process!" << endl;
        }
        else{
            cout << GetLastError() << endl;
            cout << "Process opened." << endl;

            DWORD Pointer = 0x743A4740;
            DWORD Pointed;
            WORD Offset = 0x02D8;
            int Current = 0;

            cout << GetLastError() << endl;

            ReadProcessMemory(hProc, (LPCVOID)Pointer, &Pointed, 4, NULL);
            ReadProcessMemory(hProc, (LPCVOID)(Pointed + Offset), &Current, 4, NULL);
            
            cout << GetLastError() << endl;

            cout << "The current is " << Current << endl;
            PostMessage(hWnd, WM_KEYDOWN, VkKeyScan('z'), VkKeyScan('z') << 16);
            cout << GetLastError() << endl;
        }
    }

    cin.get();
    return 0;
}
0 Timmy Cafe · December 27, 2014
@ Krootushas Gesu   This is where i am also confused because the 0x743A4740 is the static address and the 0x02D8 is the offset, and for some reason it works some times but not all the time. When i first set up the program with that address, it worked but after that it does not. The address is not changing because if i take Cheat engine to check the address it is always the same.
0 Timmy Cafe · December 27, 2014
@Krootushas Gesu  Thank you! I fixed the error 299, now it is showing the appropriate number, but posting a message to the process is still not working.
0 Timmy Cafe · December 27, 2014
@ Krootushas Gesu  i got the keypress to go though, i used:



INPUT inp[2];
memset(inp, 0, sizeof(INPUT));
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.dwFlags = KEYEVENTF_SCANCODE;
inp[1] = inp[0];
inp[1].ki.dwFlags |= KEYEVENTF_KEYUP;      // keyup event

inp[0].ki.wScan = inp[1].ki.wScan = DIK_Z;      // inp[0] = keydown, inp[1] = keyup   
SendInput(2, inp, sizeof(INPUT));


But now i am having a problem with the code 299 again, i have a bit more information now.

I think the only reason why i was able to read the memory address was because i created a pointer in cheat engine, i then ran my code and i was able to read it. But once i closed my directx application and started it back up i could not read from the memory address until i created another pointer in cheat engine. So i tired this bit of code to see if i could read it:


int NewCurrent = 0;
int *ptr = reinterpret_cast<int*>(0x744A4740) + Offset;
ReadProcessMemory(hProc, (LPCVOID)ptr, &NewCurrent, 4, NULL);
cout << "The new current is " << NewCurrent << endl;


But nothing, and i even rearranged it many times moving the variables and redefining the pointer but nothing seems to be working.
0 Timmy Cafe · December 28, 2014
@Krootushas Gesu  Sorry for such a late response. i took a non-static address and tried to read it with this code


ReadProcessMemory(hProc, (LPCVOID)0x0AC7EC78, &NewCurrent, 4, NULL);


The 0x0AC7EC78 is the non-static address, and when i ran my program, it worked. But why cant i take the static address + the offset and get the same value?
  • 1

C++

107,044 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator