| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		chainer How do I cheat?
  Reputation: 0
  Joined: 01 Nov 2009 Posts: 2
 
  | 
		
			
				 Posted: Sun Nov 01, 2009 9:54 pm    Post subject: [C#] Emulate Keyboard for DirectX games | 
				       | 
			 
			
				
  | 
			 
			
				[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 | 
		 | 
	
	
		  | 
	
	
		hcavolsdsadgadsg I'm a spammer
  Reputation: 26
  Joined: 11 Jun 2007 Posts: 5801
 
  | 
		
			
				 Posted: Sun Nov 01, 2009 11:37 pm    Post subject:  | 
				       | 
			 
			
				
  | 
			 
			
				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 | 
		 | 
	
	
		  | 
	
	
		chainer How do I cheat?
  Reputation: 0
  Joined: 01 Nov 2009 Posts: 2
 
  | 
		
			
				 Posted: Mon Nov 02, 2009 4:17 am    Post subject:  | 
				       | 
			 
			
				
  | 
			 
			
				 	  | 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 | 
		 | 
	
	
		  | 
	
	
		NINTENDO Grandmaster Cheater Supreme
  Reputation: 0
  Joined: 02 Nov 2007 Posts: 1371
 
  | 
		
			
				 Posted: Mon Nov 02, 2009 3:40 pm    Post subject:  | 
				       | 
			 
			
				
  | 
			 
			
				do you need direct x sdk to use the framework??
 _________________
 Intel over amd yes.  | 
			 
		  | 
	
	
		| Back to top | 
		 | 
	
	
		  | 
	
	
		 |