Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] Emulate Keyboard for DirectX games

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
chainer
How do I cheat?
Reputation: 0

Joined: 01 Nov 2009
Posts: 2

PostPosted: Sun Nov 01, 2009 9:54 pm    Post subject: [C#] Emulate Keyboard for DirectX games Reply with quote

[C# / C Sharp] Keyboard Emulator

Hi, I'm writing a program which emulates keyboard events. Everything is aight until I wanna try them in programs which uses direct keyboard hooking (DirectX).

Code:

using System;
using System.Threading;

namespace whatever
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Thread.Sleep(2000);
            Keyboard.KeyDown(Keyboard.VK_W);
            Thread.Sleep(2000);
            Keyboard.KeyUp(Keyboard.VK_W);
        }
    }
}

Code:

using System.Runtime.InteropServices;

namespace whatever
{
    class Keyboard
    {
        public const int VK_W = 0x57;

        [DllImport("user32.dll")]
        public static extern void keybd_event
        (
            byte bVk,
            byte bScan,
            uint dwFlags,
            uint dwExtraInfo
        );

        public static void KeyDown(int key)
        {
            keybd_event((byte)key, 0, 0, 0);
        }

        public static void KeyUp(int key)
        {
            keybd_event((byte)key, 0, 2, 0);
        }
    }
}



It should hold "W" button for 2s. But DirectX games don't catch this keybd_event(). Neither SendInput().

I've read about hooking directInput but I cannot figure out how to create a fake keyboard keydown and keyup.

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);

                        // OKay now how to simulate keyDown and keyUp?

      }
   }
}


Any ideas or examples or whatever?
I think I don't need to create a program which injects another like I've read in "[TUT] DirectX9.0 Hooking via Detours + Custom Wrapper" because this program should generate 'global' keyboard events. Imagine a remote pc control via bluetooth device. So you can play your pc games with a mobile phone as a keyboard or whatever.

I've read about "Auto Macro Recorder Pro" with:
Quote:
What's the difference between "Auto Macro Recorder" and "Auto Macro Recorder Pro"?
Auto Macro Recorder send Keyboard and Mouse message to simulate them, because DOS environment and some games do not support these messages, so Auto Macro Recorder won't work for them.
Auto Macro Recorder Pro use Keyboard and Mouse Driver to replay the Keyboard and Mouse action, so even in DirectX game and DOS environment(window mode) , it works very well.

Do I really have to use keyboard driver ?

And I also saw how ppl wrote their joystick interpreter. So they were able yo play pc games with joystick. (And if I'm not wrong, they hooked joystick and emulated keyboard events for that) And that's what I need I think.

offtopic: wtf is this "Sorry, but you can't post url's yet" ;//
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Nov 01, 2009 11:37 pm    Post subject: Reply with quote

Many newer titles use the raw input API, check for WM_INPUT messages first.
DirectX game doesn't mean it has to use DirectInput
Back to top
View user's profile Send private message
chainer
How do I cheat?
Reputation: 0

Joined: 01 Nov 2009
Posts: 2

PostPosted: Mon Nov 02, 2009 4:17 am    Post subject: Reply with quote

slovach wrote:
Many newer titles use the raw input API, check for WM_INPUT messages first.
DirectX game doesn't mean it has to use DirectInput


I cannot use Send/PostMessage with WM_* because i have to know where to send it (focusing window and etc). The question is how to simulate global keystrokes.
Back to top
View user's profile Send private message
NINTENDO
Grandmaster Cheater Supreme
Reputation: 0

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Mon Nov 02, 2009 3:40 pm    Post subject: Reply with quote

do you need direct x sdk to use the framework??
_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites